Forum rules - please read before posting.

Custom Action: Disable-Enable Your component (your custom script)

edited September 2015 in Extending the editor
I made this Enable/Disable custom action for my Object follow mouse component that I can switch on and off. You can customize it and enable/disable your component. Just change in this script Obj3DFollowCursor to your component name.

This custom action is based on Object:Visibility

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{

    [System.Serializable]
    public class EnableDisableCompon : Action
    {

        // Declare variables here

        public int parameterID = -1;
        public int constantID = 0;
        public bool affectChildren;
        public EnableState enableStateObj = 0;

        public GameObject objectToAffect;
        public bool newEnableState;

        public EnableDisableCompon ()
        {
            this.isDisplayed = true;
            category = ActionCategory.Object;
            title = "Enable-Disable Component";
            description = "This is a blank Action template.";
        }

        override public void AssignValues (List<ActionParameter> parameters)
        {
            objectToAffect = AssignFile (parameters, parameterID, constantID, objectToAffect);
        }


        override public float Run ()
        {
            bool state = false;
            if (enableStateObj == EnableState.Disabled)
            {
                state = true;
            }

            if (objectToAffect)
            {
                if (objectToAffect.GetComponent <Obj3DFollowCursor>())
                {
                    objectToAffect.GetComponent <Obj3DFollowCursor>().enabled = !state;
                }
                else if (objectToAffect.GetComponent <Obj3DFollowCursor>())
                {
                    objectToAffect.GetComponent <Obj3DFollowCursor>().enabled  = state;
                }

                if (affectChildren)
                {
                    foreach (Obj3DFollowCursor _ObjState in objectToAffect.GetComponentsInChildren <Obj3DFollowCursor>())
                    {
                        _ObjState.enabled = state;
                    }
                }

            }

            return 0f;

        }

        #if UNITY_EDITOR

        override public void ShowGUI (List<ActionParameter> parameters)
        {
            parameterID = Action.ChooseParameterGUI ("Object to affect:", parameters, parameterID, ParameterType.GameObject);
            if (parameterID >= 0)
            {
                constantID = 0;
                objectToAffect = null;
            }
            else
            {
  
             objectToAffect = (GameObject) EditorGUILayout.ObjectField
("Object to affect:", objectToAffect, typeof (GameObject), true);

                constantID = FieldToID (objectToAffect, constantID);
                objectToAffect = IDToField (objectToAffect, constantID, false);
            }

            enableStateObj = (EnableState) EditorGUILayout.EnumPopup ("Visibility:", enableStateObj);
            affectChildren = EditorGUILayout.Toggle ("Affect children?", affectChildren);

            AfterRunningOption ();
        }
        override public string SetLabel ()
        {
            string labelAdd = "";
            if (objectToAffect)
                labelAdd = " (" + objectToAffect.name + ")";
            return labelAdd;
        }
        #endif
    }
}

Don't forget to add EnableState declaration  to Enum.cs
just add this line at the bottom right after CheckVisState
 public enum EnableState { Enabled, Disabled };
, and that should do.

Comments

  • Great! this is exactly what I was looking for! thanks!
  • First of all thanks to SkyTree for posting this. I'm trying to get a commercial plugin, Amplify Bloom, to work with this, but when replacing  Obj3DFollowCursor with AmplifyBloom, Unity throws an error:  "error CS0118: `AmplifyBloom' is a `namespace' but a `type' was expected"

    And this is the only code I can pull from the plugin:

    using UnityEngine;
    namespace AmplifyBloom
    {
        [ExecuteInEditMode]
        [System.Serializable]
        [RequireComponent( typeof( Camera ) )]
        [AddComponentMenu( "Image Effects/Amplify Bloom")]
        public sealed class AmplifyBloomEffect : AmplifyBloomBase { }
    }
     If anyone can help it would be much appreciated.

    Thanks,
    Jason
  • Ask amplify Blooms developer's or check their API, you are probably not using the correct address. A namespace is just a name you use to group your scripts so they don't get confused with the code in other similarly named scripts or classes. If AmplifyBloom is a namespace it means the actual direction should be different, either that or you have to use the actual component's name. Still, I recommend you to ask in Amplify Bloom's forums. What you pretty much want to know how to do, is how to enable or disable Amplify bloom's component in whichever camera is the main camera, that's most probably a routine question over there, so they should probably be able to answer faster. Once you know how to achieve that, you can safely use it in an AC action.
  • Hello, How do I fix this

    The type or namespace name `enableState' could not be found. Are you missing a using directive or an assembly reference?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.