Skip to content

Instantly share code, notes, and snippets.

@th3d0g
Created January 27, 2022 17:06
Show Gist options
  • Save th3d0g/c0be475cbb5d7582859eff2560cadfcc to your computer and use it in GitHub Desktop.
Save th3d0g/c0be475cbb5d7582859eff2560cadfcc to your computer and use it in GitHub Desktop.
Generate enum from editor
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment