Search Unity

Enum Watch For Changes

Discussion in 'Scripting' started by The-Little-Guy, Mar 1, 2015.

  1. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    I have the following Editor code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. [CustomEditor(typeof(GameEvent))]
    6. public class EasyCommandEditor : Editor {
    7.     override public void OnInspectorGUI(){
    8.         DrawDefaultInspector();
    9.         GameEvent targ = target as GameEvent;
    10.  
    11.         GameEvent.GameEvents newValue = (GameEvent.GameEvents)EditorGUILayout.EnumPopup(targ.gameEvent);
    12.     }
    13. }
    Instead of drawing the enum to the Inspector, is it possible to just get the value when it changes?
     
  2. jnoffke

    jnoffke

    Joined:
    Nov 26, 2013
    Posts:
    31
    You can use OnValidate function thats called when changes are made in the inspector.
     
  3. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    245
    Last edited: Mar 1, 2015
  4. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    @Juice-Tin Thanks! That I think will do what I want!

    I would like to do this instead of a switch:

    Code (CSharp):
    1. if(_evt != (GameEvent.GameEvents)evt.intValue){
    2.  
    3. }
    But it doesn't seem to work because the two values are always the same. Any idea of a way to accomplish this?
     
    Last edited: Mar 2, 2015
  5. jnoffke

    jnoffke

    Joined:
    Nov 26, 2013
    Posts:
    31
    If you want to check for a change on a Serialized property you can use the EditorGUI.BeginChangeCheck and EditorGUI.EndChangeCheck function calls.

    Code (CSharp):
    1. SerializedProperty _evt = serializedObject.FindProperty("NameOfVariable");
    2.  
    3. EditorGUI.BeginChangeCheck();
    4. EditorGUILayout.PropertyField(_evt);
    5.  
    6. if(EditorGUI.EndChangeCheck())
    7. {
    8. if(_evt.intValue == EnumValue)
    9. //specific code here
    10. else if(_evt.intValue == AnotherEnumValue)
    11. //code here
    12. }