Help with my enumpopup.
Got this code in my normal script
public class Info : MonoBehaviour {
public enum Category {Asteroid, Galaxy, Planet, Satellite, Star} // My list.
public Category category;} // This creates my Popup in my normal inspector.
In my custom inspector I have this.
Info myTarget = (Info)target;
myTarget.Category = (int)(MyEnumType)EditorGUILayout.EnumPopup("Category:", (MyEnumType)Enum.GetValues(typeof(MyEnumType)).GetValue(myTarget.Category));
I tried to convert someones answer to mine, what goes into MyEnumType, for me its just an enum, what about (int) is that for the index or should it be something else? I believe the rest I have correct.
Can someone please help me fix this.
P.S. when I code check, I get an error saying $$anonymous$$yEnumType could not be found. I am so lost on this one, do you have to declare $$anonymous$$yEnumType?
Answer by d112570 · Apr 21, 2014 at 11:15 AM
myTarget.category = (Category)EditorGUILayout.EnumPopup("Category",myTarget.category);
If someone explained it correctly I would of not have to search the web for 4 days, it was quite annoying. But this code worked that I finally translated. No site seemed to have had an answer, and none worked for me. Here I show how it should be.
First part is this (Info Script). Move the enum before class
public enum Category {Asteroid,Galaxy,Planet,Satellite,Star}
public class Info : MonoBehaviour {
public Category category;} // This creates my Popup in my normal inspector.
Second part is this. (Custom Editor Script)
using UnityEditor;
[CustomEditor(typeof(Info))]
public class InfoEditor : Editor
{
public override void OnInspectorGUI()
{
Info myTarget = (Info)target;
myTarget.category = (Category)EditorGUILayout.EnumPopup("Category",myTarget.category);
}}
Easy as that, I seen answers that did't work with tons of more code and it didn't work. :-( Maybe not compatible with Unity 4.3.
For the newbies, the Editor script goes in the Project/Editor folder and drag the class script onto the relevant gameObject. The inpector will be overwritten from the class script and will use the editor script ins$$anonymous$$d. But if you want to see the class script as a test or something, add this code somewhere after onInspectorGUI();
base.OnInspectorGUI();