playMaker

Author Topic: Toggle Gameobject  (Read 2485 times)

christianstrang

  • Playmaker Newbie
  • *
  • Posts: 16
Toggle Gameobject
« on: July 13, 2016, 08:27:32 AM »
Hi guys,
just wanted to get my feet wet with a very simple custom action. I created a "toggle gameobject" action, which basically uses the "Activate Gameobject" action and throws everything away adds the toggle part:

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using HutongGames.PlayMaker;
using UnityEngine;

namespace com.simpleplaystudio.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.GameObject)]
    [HutongGames.PlayMaker.Tooltip("Activates/deactivates a Game Object. Use this to hide/show areas, or enable/disable many Behaviours at once.")]
    public class ToggleGameObject : FsmStateAction
    {
        [RequiredField]
        [UnityEngine.Tooltip("The GameObject to toggle.")]
        public FsmOwnerDefault gameObject;
       
        // store the game object that we activated on enter
        // so we can de-activate it on exit.
        GameObject activatedGameObject;

        public override void Reset()
        {
            gameObject = null;
        }

        public override void OnEnter()
        {
            DoActivateGameObject();
            Finish();
        }

        public override void OnUpdate()
        {
            DoActivateGameObject();
        }
       
        void DoActivateGameObject()
        {
            var go = Fsm.GetOwnerDefaultTarget(gameObject);
           
            if (go == null)
            {
                return;
            }

            if (go.activeSelf)
                go.SetActive(false);
            else
                go.SetActive(true);
           
            activatedGameObject = go;
        }
    }
}

I have a couple of questions about this:
  • Does an action like this already exist? (couldn't find anything)
  • Did I make major mistakes in the action? (I basically removed all the code that targeted unity 3.4 and 3.5)
  • Might this be useful to anyone or is it too basic?
« Last Edit: July 13, 2016, 09:37:20 AM by christianstrang »

terri

  • Sr. Member
  • ****
  • Posts: 386
    • terrivellmann.tumblr.com
Re: Toggle Gameobject
« Reply #1 on: July 14, 2016, 07:38:54 AM »
Does your version work for you?
I'd go about it more like this

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.GameObject)]
    [HutongGames.PlayMaker.Tooltip("Activates/deactivates a Game Object. Use this to hide/show areas, or enable/disable many Behaviours at once.")]
    public class ToggleGameObject : FsmStateAction
    {
        [RequiredField]
        [UnityEngine.Tooltip("The GameObject to toggle.")]
        public FsmOwnerDefault gameObject;
public bool resetOnExit;

        public override void Reset()
        {
            gameObject = null;
    resetOnExit = true;
        }

        public override void OnEnter()
        {
            DoActivateGameObject();
            Finish();
        }

public override void OnExit()
        {
            if(resetOnExit==true){
DoActivateGameObject();
}
        }

        void DoActivateGameObject()
        {
            var go = Fsm.GetOwnerDefaultTarget(gameObject);
           
            if (go == null)
            {
                return;
            }

            if (go.activeSelf)
                go.SetActive(false);
            else
                go.SetActive(true);
        }
    }
}