Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Mun-Yeong-Seok · Oct 01, 2017 at 04:31 PM · c#scripting probleminspectorenumedit

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?

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image YoucefB · Oct 01, 2017 at 09:09 PM 0
Share

This can be done using an Editor script, or a PropertyDrawer.

avatar image llentinantl · Oct 01, 2017 at 09:13 PM 0
Share

I believe enum is compiled value, and editor works with already compiled scripts

avatar image Mun-Yeong-Seok llentinantl · Oct 01, 2017 at 09:57 PM 0
Share

So, maybe it's impossible? If I write and save the Enum code, will it automatically compile at that moment?

avatar image YoucefB Mun-Yeong-Seok · Oct 01, 2017 at 10:27 PM 0
Share

i made you an exemple on how to do it, hope it helps :)

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

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);
         }
     }
 }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mun-Yeong-Seok · Oct 02, 2017 at 02:15 AM 0
Share

wow nice. create a new script file... It is an unthinkable way.

alt text

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!

123.png (21.8 kB)
avatar image
1

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...
         }
         
     }
 }
 

 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mun-Yeong-Seok · Oct 01, 2017 at 07:58 PM 0
Share

I wonder that there is a way to add Enum items on the Inspector Window(not in script code).

avatar image
1

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] : "";
     }
 }


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mun-Yeong-Seok · Oct 01, 2017 at 11:45 PM 0
Share

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?

avatar image Mun-Yeong-Seok Mun-Yeong-Seok · Oct 01, 2017 at 11:53 PM 0
Share

Oh, I created the getDays class separately. It was resolved.

avatar image haruna9x · Oct 02, 2017 at 12:10 AM 1
Share

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.

avatar image
0

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
   }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

421 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges