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
0
Question by DustyScreen · Apr 13, 2016 at 08:17 PM · editor-scriptingenum

Editorscript: Generate enum from string[]

I'm wondering if it's possible to Generate an enum in an Editorscript?

I have a method in an Editorscript that dynamically creates a string[]. Let's say holds the strings "Foo", "Goo" and "Hoo". Is it possible, from that array, to generate an enum, like the following:

 public enum MyEnum
 {
 Foo,
 Goo,
 Hoo
 }

I guess Unity will have to recompile after that. When recompiling is done, the enum should be ready to be used in PlayMode.

Is that by any means possible? Any input is most welcome :-)

Comment
Add comment
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

4 Replies

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

Answer by DustyScreen · May 10, 2016 at 10:11 AM

Though @TimHeijden's code works like a charm (and got me in the right direction) I ended up doing it like this:

 #if UNITY_EDITOR
 using UnityEditor;
 using System.IO;
 
 public class GenerateEnum
 {
     [MenuItem( "Tools/GenerateEnum" )]
     public static void Go()
     {
         string enumName = "MyEnum";
         string[] enumEntries = { "Foo", "Goo", "Hoo" };
         string filePathAndName = "Assets/Scripts/Enums/" + enumName + ".cs"; //The folder Scripts/Enums/ is expected to exist
 
         using ( StreamWriter streamWriter = new StreamWriter( filePathAndName ) )
         {
             streamWriter.WriteLine( "public enum " + enumName );
             streamWriter.WriteLine( "{" );
             for( int i = 0; i < enumEntries.Length; i++ )
             {
                 streamWriter.WriteLine( "\t" + enumEntries[i] + "," );
             }
             streamWriter.WriteLine( "}" );
         }
         AssetDatabase.Refresh();
     }
 }
 #endif
Comment
Add comment · Show 2 · 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 MarcBrout · Nov 06, 2018 at 03:39 PM 0
Share

Do you get autocompletion on the created Enum in visual studio with this ?

avatar image Cargold · May 04, 2020 at 06:31 AM 0
Share

Wow!!! Thanks!!! 우와!!! 감사합니다!!!

avatar image
2

Answer by TimHeijden · Apr 13, 2016 at 10:15 PM

It's actually quite simple if you want to do this in an editor script. Like any text file, you can use a StringBuilder to create the contents:

 string enumNam = "MyEnum";
 string[] enumEntries = new string[] { "Foo", "Goo", "Hoo" };
 
 StringBuilder str = new StringBuilder();
 str.AppendFormat(@"public enum {0}\n{\n", enumName);
 for (int i = 0; i < enumEntries.Length; i++)
 {
      str.AppendFormat(@"{0},\n", enumEntries[i]);
 }
 str.AppendLine(@"}");

Then, use some UnityEditor classes to get the asset path. For example:

 string path = AssetDatabase.GetAssetPath(serializedObject.targetObject);
 path = path.Substring(0, path.Length - Path.GetExtension(path).Length) + ".cs";
 File.WriteAllText(path, str.ToString());
 AssetDatabase.ImportAsset(path);

And that should be it! (note: depending on what kind of editor script you want to write, you may need to use a different way to get the paths, but there are loads of ways to that easily found with google.

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
-1

Answer by larex39 · May 07, 2020 at 07:18 AM

Here is youtube tutorial how to show List as Enum in inspector

https://www.youtube.com/watch?v=ThcSHbVh7xc![alt text

alt text


gif.gif (253.9 kB)
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 PixelFireXY · Jul 23, 2021 at 02:22 PM 0
Share

Thank you for the suggestion, please fix the link by removing "![alt text" :)

avatar image
0

Answer by lclemens · Jul 10, 2021 at 12:37 AM

Another thing you may want to do is ensure the strings don't contain any invalid characters or keywords that would break in C#. So you can combine some of the other answers here with something like this:

 private static HashSet<string> csharpKeywords = new HashSet<string> {
     "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked",
     "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else",
     "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for",
     "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock",
     "long", "namespace", "new", "null", "object", "operator", "out", "override", "params",
     "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed",
     "short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw",
     "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using",
     "virtual", "void", "volatile", "while"
 };

 // given a string, attempts to make the string compatible with an enum
 static string MakeStringEnumCompatible(string text)
 {
     if (text.Length <= 0) { return "INVALID_ENUM_NAME"; }
     string ret = "";

     // first char must be a letter or an underscore
     if (char.IsLetter(text[0]) || (text[0] == '_')) { ret += text[0]; }

     // strip out anything that's not a digit or underscore
     for (int i = 1; i < text.Length; ++i) {
         if (char.IsLetterOrDigit(text[i]) || (text[i] == '_')) { ret += text[i]; }
     }
     if (ret.Length <= 0) { return "INVALID_ENUM_NAME"; }

     // all the keywords are lowercase, so if we just change the first letter to uppercase,
     // then there will be no conflict
     if (csharpKeywords.Contains(ret)) { ret = char.ToUpper(ret[0]) + ret.Substring(1); }
     
     return ret;
 }
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

49 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

Related Questions

How to create enum out of array of string? 2 Answers

Limiting options in Unity Editor enum popup box based on prior selection 0 Answers

Problem with second enum selection inspector 0 Answers

How to create bitmask flags per scene that are available in the editor? 0 Answers

UIElements EnumField data binding 0 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