Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: christianstrang on July 13, 2016, 08:27:32 AM

Title: Toggle Gameobject
Post by: christianstrang 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:
Title: Re: Toggle Gameobject
Post by: terri 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);
        }
    }
}