Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: mgervais on May 17, 2020, 11:45:25 AM
-
Hi guys,
I'm trying to find a way to deactivate a UI canvas with a Playmaker action. The "Activate Gameobject" action only works with game objects. I was wondering if any of you is aware of an action that could have a similar function for UI Components or even the whole canvas.
The C# script for this would be:
void DisableCanvas() {
CanvasObject.SetActive(false);
}
Thanks for your help
Mike
-
Hi,
Good point, I'll do that action asap, I am trying to fix the repository package to make that action...
BYe,
Jean
-
Hi.
There is an action on the Ecosystem (https://hutonggames.fogbugz.com/default.asp?W1181) called 'Activate Component' it should be able to activate any component.
-
I'd like to have that action myself as well. Didn't find anything and tried to write it myself but failed at it. haha.
The activate component action recommended above is weird, because they seem to do different things (destroying) and well use complete different code which probably is not as performant for canvas.
As far as I understood it would just be one line:
CanvasObject.GetComponent<Canvas> ().enabled = false;
but I can't make it recognize the object.
Anyways, a clean and professional written action would be best for this since this is the recommended and official way by Unity to enable and disable a canvas. It basically avoids to redraw the calls if I understood correctly.
Being it the recommended way by Unity itself to use UIs like that, it should actually be part of Playmaker itself I think.
-
Hi.
You can try this one (attachment below) and have a look to the script.
Your line could work as well, but the problem is the 'CanvasObject'
you need to target the object, so your script should look like this :
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.UI)]
[Tooltip("Activate / Deactivate Canvas Component")]
public class ActivateCanvas2 : FsmStateAction
{
[RequiredField]
[Tooltip("The GameObject that owns the Component.")]
[CheckForComponent(typeof(Canvas))]
public FsmOwnerDefault gameObject;
[Tooltip("Activate / Deactivate the Canvas")]
public FsmBool activate;
public override void Reset()
{
gameObject = null;
activate = null;
}
public override void OnEnter()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;
go.GetComponent<Canvas>().enabled = activate.Value;
}
}
}
You can see the difference from the action i send.
-
Works perfectly. Thanks djaydino.
What is cachedComponent though? And why doesn't it nowhere refer to the canvas component itself?
-
Hi.
Its handled by the ComponentAction class (see line 11).
public class EnableCanvas : ComponentAction<Canvas>
if you use visual studio you can right click on the 'ComponentAction' and select peek/go to Definition.
-
Dang that's too much C# for me there ;D
But anyways, thanks a lot for this action! Should definitely get uploaded to the Ecosystem.
Cheers!
-
Hi.
I learned C# thanks to playmaker :)
its a good think to look to other actions and see how they work, then try to make some custom actions :)
The ComponentAction class will get the canvas and some other data and you can access them by using cachedComponent or cachedGameObject (if you need to reference a gameobject directly)
-
Yeah, some small copy paste stuff I can do. Sometimes it works, sometimes not. I'm definitely never sure if I did it in a 'clean' way. But I hope slowly to be able to write more and more actions.
Thanks again.
-
Hi @djaydino, quick update to this new action, for some reason sometimes it doesn't work. Not sure what the problem is but I basically just replaced a set component action with this one and now I have sometimes the UI not deactivated when it should. So there seems to be a bug. Possible?
-
Hi.
Can you show the setup.
Are you changing canvas target in runtime?
-
hmmm no, I just added the action and filled out the needed things. Not that there are many options...
(https://i.imgur.com/9xxyDPd.png)
The first Set Property action always works flawless. When I replace it with the Enable Canvas action it sometimes works, sometimes not.