playMaker

Author Topic: Deactivate a UI canvas with Playmaker  (Read 534 times)

mgervais

  • Playmaker Newbie
  • *
  • Posts: 19
    • View Profile
Deactivate a UI canvas with Playmaker
« 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

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15453
  • Official Playmaker Support
    • View Profile
Re: Deactivate a UI canvas with Playmaker
« Reply #1 on: May 18, 2020, 02:52:34 AM »
Hi,

 Good point, I'll do that action asap, I am trying to fix the repository package to make that action...

BYe,

 Jean

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6003
    • View Profile
    • jinxtergames
Re: Deactivate a UI canvas with Playmaker
« Reply #2 on: May 18, 2020, 08:10:21 AM »
Hi.
There is an action on the Ecosystem called 'Activate Component' it should be able to activate any component.

Christoph

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 72
    • View Profile
Re: Deactivate a UI canvas with Playmaker
« Reply #3 on: February 25, 2021, 11:00:04 PM »
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.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6003
    • View Profile
    • jinxtergames
Re: Deactivate a UI canvas with Playmaker
« Reply #4 on: February 26, 2021, 06:56:05 AM »
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 :

Code: [Select]
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.

Christoph

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 72
    • View Profile
Re: Deactivate a UI canvas with Playmaker
« Reply #5 on: February 27, 2021, 01:51:50 PM »
Works perfectly. Thanks djaydino.

What is cachedComponent though? And why doesn't it nowhere refer to the canvas component itself?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6003
    • View Profile
    • jinxtergames
Re: Deactivate a UI canvas with Playmaker
« Reply #6 on: February 27, 2021, 02:00:34 PM »
Hi.
Its handled by the ComponentAction class (see line 11).

Code: [Select]
    public class EnableCanvas : ComponentAction<Canvas>
if you use visual studio you can right click on the 'ComponentAction' and select peek/go to Definition.

Christoph

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 72
    • View Profile
Re: Deactivate a UI canvas with Playmaker
« Reply #7 on: February 27, 2021, 03:05:23 PM »
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!

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6003
    • View Profile
    • jinxtergames
Re: Deactivate a UI canvas with Playmaker
« Reply #8 on: February 27, 2021, 03:27:01 PM »
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)

Christoph

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 72
    • View Profile
Re: Deactivate a UI canvas with Playmaker
« Reply #9 on: February 27, 2021, 07:59:40 PM »
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.

Christoph

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 72
    • View Profile
Re: Deactivate a UI canvas with Playmaker
« Reply #10 on: March 06, 2021, 10:53:12 AM »
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?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6003
    • View Profile
    • jinxtergames
Re: Deactivate a UI canvas with Playmaker
« Reply #11 on: March 09, 2021, 06:22:14 AM »
Hi.
Can you show the setup.

Are you changing canvas target in runtime?

Christoph

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 72
    • View Profile
Re: Deactivate a UI canvas with Playmaker
« Reply #12 on: March 10, 2021, 02:27:46 PM »
hmmm no, I just added the action and filled out the needed things. Not that there are many options...



The first Set Property action always works flawless. When I replace it with the Enable Canvas action it sometimes works, sometimes not.