Search Unity

Assigning Enum value in Inspector through custom script

Discussion in 'Scripting' started by laurelhach, Oct 30, 2014.

  1. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Hi Guys,

    I have a big enum list exposed in the inspector on one of my prefab.
    Scrolling through the enum list in the inspector is quite a long and fastidious process (click on the small arrow at the bottom of the list and wait to reach the correct value).

    So I decided to create a custom script that can assign the enum value to any selected object by entering a string. The tool works to assign the value (the enum list is updated correctly). But, unfortunately, when I play in the editor, the value resets to the last value that was set manually. It seems that using my custom script to assign the enum value doesn't work.

    So my question is the following:
    - Can we speed up the scrolling through the enum list (Does the wheel work for most of you? It doesn't for me for the enum list).
    OR
    - How can I make sure that when I change an enum value through a custom script, it gets saved in the editor.

    Thanks in advance.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    What is this custom script you're using to change it?

    Is it an editor script?

    Are you changing the value on the serializedObject or on the object itself?

    If it's the object itself, are you calling 'Update' on the serializedObject in question so that it too receives the data?
     
  3. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    It is part of a custom tool used in an editor window.
    So yes it's an editor script.

    I am changing the value on the prefab itself in the scene (not at run time - just to be clear)
    I select it, press a button and it assigns the value.
    I don't call Update ... (Actually... I am not sure what you are talking about :( )
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    so all your things in scene actually have 2 objects that are part of it

    1) the actual Object in scene or in the folder hierarchy
    2) the serialized representation of that object

    In your editor script if you say:

    Code (csharp):
    1.  
    2. this.serializedObject
    3.  
    That's a reference to the serialized object.

    There are two methods on the serializedObject in question here.

    SerializedObject.ApplyModifiedProperties
    http://docs.unity3d.com/ScriptReference/SerializedObject.ApplyModifiedProperties.html

    If you altered the SerializedObject, this applies them to the object in scene.

    SerializedObject.Update
    http://docs.unity3d.com/ScriptReference/SerializedObject.Update.html

    If you altered the in scene object, this applies those changes to the serializedObject.

    In your code try saying:
    Code (csharp):
    1.  
    2. this.serializedObject.Update();
    3.  
    After you've changed the enum value.
     
    laurelhach likes this.
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    The value resets because you are not marking as dirty (probably)
     
  6. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Hp,

    Setting this in the editor script after I modified the enum list :
    Code (csharp):
    1.  EditorUtility.SetDirty(go);
    doesn't work.

    Lord, I am going to try your version and let you know if it works.

    Thanks for the feedback.
     
  7. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Lord,

    Thanks for your help, it works. It was not that simple for me but I found out how to make it work.
    I am posting part of the code I use to change my enum, in case someone wants to take a look.

    Code (csharp):
    1.  
    2.         enumToEnter = GUILayout.TextArea(enumToEnter);
    3.         string tmpEnumName;
    4.         if(GUILayout.Button("Assign EnumTo Trap to Selection")){
    5.             if(enumToEnter != ""){
    6.                 GameObject go = Selection.activeGameObject;
    7.                 SerializedObject so = new SerializedObject(go.GetComponent<LevelTransition>());
    8.                 if(go != null){
    9.                     for(int x=0;x< Enum.GetValues(typeof(LevelTransition.LevelTransitionTo)).Length;x++){
    10.                         tmpEnumName = Enum.GetName(typeof(LevelTransition.LevelTransitionTo), x);
    11.                         if( tmpEnumName.Contains("TrapHole" + enumToEnter + "a")){
    12.  
    13.                             so.FindProperty("levelTransitionTo").enumValueIndex = x+1;
    14.                             so.FindProperty("levelTransitionFrom").enumValueIndex = x;
    15.                             so.ApplyModifiedProperties();
    16.                          
    17.                             Debug.LogWarning("Trap is updated!");
    18.                             break;
    19.                         }
    20.                     }
    21.                 }
    22.                 else
    23.                     Debug.LogError("Please select an object");
    24.             }
    25.             else
    26.                 Debug.LogError("There is no enumToEnter entered in the field.");
    27.         }
    28.