- Home /
Can I add an enum value in the inspector?
public enum Days { Sat, Sun, Mon, Tue, Wed, Thu, Fri }
There is an enum type like the above. And I want to add the 'Sat ~ Fri' value in the Inspector. So, in the beginning, the enum value is empty as follows.
public enum Days { }
Of course, if i add an Enum value in the Inspector should be usable in other scripts Immediately. Is it possible?
This can be done using an Editor script, or a PropertyDrawer.
I believe enum is compiled value, and editor works with already compiled scripts
So, maybe it's impossible? If I write and save the Enum code, will it automatically compile at that moment?
i made you an exemple on how to do it, hope it helps :)
Answer by haruna9x · Oct 02, 2017 at 01:16 AM
You can use these three simple scripts to create your Enum.
EdiorMethods.cs in Editor folder.
public class EdiorMethods : Editor
{
const string extension = ".cs";
public static void WriteToEnum<T>(string path, string name, ICollection<T> data)
{
using (StreamWriter file = File.CreateText(path + name + extension))
{
file.WriteLine("public enum " + name + " \n{");
int i = 0;
foreach (var line in data)
{
string lineRep = line.ToString().Replace(" ", string.Empty);
if (!string.IsNullOrEmpty(lineRep))
{
file.WriteLine(string.Format("\t{0} = {1},",
lineRep, i));
i++;
}
}
file.WriteLine("\n}");
}
AssetDatabase.ImportAsset(path + name + extension);
}
}
MyScript.cs
public class MyScript : MonoBehaviour
{
public List<string> days; //contain the names of the enum
}
TestWriter.cs in Editor folder.
[CustomEditor(typeof(MyScript))]
public class TestWriter : Editor
{
MyScript myScrip;
string filePath = "Assets/";
string fileName = "TestMyEnum";
private void OnEnable()
{
myScrip = (MyScript)target;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
filePath = EditorGUILayout.TextField("Path", filePath);
fileName = EditorGUILayout.TextField("Name", fileName);
if(GUILayout.Button("Save"))
{
EdiorMethods.WriteToEnum(filePath, fileName, myScrip.days);
}
}
}
wow nice. create a new script file... It is an unthinkable way.
Other devs have shown good ways, but this seems to be the best fit. This is also nice utility of Enum script creation. Thank you all!
Answer by bolkay · Oct 01, 2017 at 07:51 PM
@Mun-Yeong-Seok Make your question more clearer. If you want to add some functionalities to each item in the enum, consider using switch statement.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnumTest : MonoBehaviour {
public enum MyDays {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
};
//show in inspector
public MyDays days;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
switch (days) {
case (MyDays.Sunday):
Debug.Log ("Today is Sunday");
break;
case(MyDays.Monday):
Debug.Log ("Today is Monday");
break;
//etc...
}
}
}
I wonder that there is a way to add Enum items on the Inspector Window(not in script code).
Answer by YoucefB · Oct 01, 2017 at 10:26 PM
Here is how to do it using a PropertyDrawer:
using System.Collections.Generic;
public class MyScript : MonoBehaviour{
[getDays("dayIndex")]
public string day; // here will store the name of the day (string)
[HideInInspector] public int dayIndex; // here store the index of the chosen day
public List<string> ListOfDays = new List<string>{"Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"}; // you can add or remove days from the inspector
}
Make a new script and put it inside a folder with the name "Editor"
//Put this in an editor script , inside a folder with the name "Editor"
public class getDays : PropertyAttribute{
public string dayIndex;
public getDays(string dayIndex){
this.dayIndex = dayIndex;
}
}
[CustomPropertyDrawer(typeof(getDays))]
public class getDaysDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
getDays att = attribute as getDays;
string propertyPath = property.propertyPath;
string dayIndex = propertyPath.Replace (property.name, att.dayIndex);
SerializedProperty sourceProperty = property.serializedObject.FindProperty (dayIndex);
MyScript myscript = (property.serializedObject.targetObject) as MyScript ;
if (myscript.ListOfDays == null)
return;
List <string> days = new List<string>{"None"};
for (int x = 0; x < myscript.ListOfDays.Count; x++) {
days.Add (x + "-" + myscript.ListOfDays[x]);
}
EditorGUI.PrefixLabel (position, new GUIContent("Day: "));
position.x += Screen.width/4;
position.width -= Screen.width/4;
sourceProperty.intValue =Mathf.Clamp(sourceProperty.intValue,0,days.Count-1);
sourceProperty.intValue = EditorGUI.Popup (position,sourceProperty.intValue,days.ToArray());
property.stringValue = sourceProperty.intValue>0 ? myscript.ListOfDays [sourceProperty.intValue-1] : "";
}
}
Is $$anonymous$$yScript.cs located outside the Editor folder?
Assets/Script/Test Script/$$anonymous$$yScript.cs(7,3): error CS0246: The type or namespace name `getDays' could not be found. Are you missing an assembly reference?
I get the above error, is there something I missed?
Oh, I created the getDays class separately. It was resolved.
This is not an enum. You are breaking his data structure. Just edit a List inside the Inspector, then write a script that writes data to the Enum script.
Answer by MaxGuernseyIII · Oct 01, 2017 at 11:07 PM
If you can control or proxy the enum (which you always can) and you are lazy (like me), you can do something like this...
[Flags]
public enum Days
{
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
Saturday = 32,
Sunday = 64,
FridayAndSaturday = Friday | Saturday
}
Your answer
Follow this Question
Related Questions
Automatically create enum based on class children types? (C#) 1 Answer
Accessible collection of Type 1 Answer
C# Edit enum values in inspector 1 Answer
C# script as variable 1 Answer
Use a enum from another script 2 Answers