Using Enums In Custom Inspector Scripts

Hi everyone, I’ve found number of posts the threads regarding this subject, and I and I have tried to implement the answers from those posts. But after a full day I’m still at square one and cant get an Enum to work within a custom inspector script.

This is my Enum Class: - It is not fixed within another class so all classes have access to it.

public enum TileTypes {

 Normal = 0,

 Turn = 1,

 Mirror = 2,

 Invert = 3,

 Switch = 4

};

This is the Object Class I am trying to write an inspector for:

using UnityEngine;

using System.Collections;


public class Tile : MonoBehaviour {


 public TileTypes tileType;


}

And this is my custom inspector class:

using UnityEngine;

using UnityEditor;

using System.Collections;


[CustomEditor(typeof(Tile))]

[CanEditMultipleObjects]

public class Tile_Editor : Editor {


public override void OnInspectorGUI ()

{


 Tile myScript = (Tile) target;


 myScript.tileType= (TileTypes )EditorGUILayout.EnumPopup ("Tile Type", myScript.tileType);


}

That is the closest I have got to getting it working, and it allows me to change the enum value within the inspector. But when I play the game the value reverts back to its default value. I am aware of seized field, But can’t find an example of how to make one for an Enum field “At least not one that works for me”.

This was my attempt at serializing an Enum:

using UnityEngine;

using UnityEditor;

using System.Collections;


[CustomEditor(typeof(Tile))]

[CanEditMultipleObjects]

public class Tile_Editor : Editor {


 SerializedProperty typeProp;


 private void OnEnable(){

 typeProp = serializedObject.FindProperty("tileType");

 }


 public override void OnInspectorGUI (){


 serializedObject.Update ();


 typeProp = (TileTypes) EditorGUILayout.EnumPopup(TileTypes);


 serializedObject.ApplyModifiedProperties ();

 }


}

I really struggle with writing Custom GUI Inspectors, but if I could just get this working it would hugely reduce my development time.

Thank guys.

First of all, this is better suited for the Extensions & OnGUI forum, you can find more information there on custom editors.

When you have a field that is an enum, you don’t really need to use EnumPopup. Just use a PropertyField and unity will draw it as a drop down by default.

Replace:

with

EditorGUILayout.PropertyField(typeProp);
3 Likes

Sorry about using the incorrect form.
It works.

Thanks so much.

No need to apologize. I only wanted to point out that you can use that forum as a good resource when trying to build editor extensions.

I am also dealing with enums while editing an inspector what i have is that my enum (someList) lies on another script which is a class called point point does not derive from monobehavior and is more like a struct and i have created a variable of someList in that class now i make an array of type point in another script called pointDetection now in point detection script what i am trying to do is enable those variables of type point (array can be viewed in inspector) with respect to that enum in that point variable. I can explain further if need be Thanks

1 Like

I didn’t have any success with these methods the one that worked best was by Skalev but I could interact with the Enum,
so I looked into the unity documentation and made this and it works perfectly.

override public void OnInspectorGUI()
{
var randomGunGenerator = target as RandomGunGenerator;
randomGunGenerator.gunType = (RandomGunGenerator.GunTypes)EditorGUILayout.EnumPopup(“Type Of Gun:”, randomGunGenerator.gunType);

}