Skip to content

Instantly share code, notes, and snippets.

@Gucchi0512
Last active November 12, 2022 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gucchi0512/6dd2b74ba5ea0f9c9f671d6fcacc115f to your computer and use it in GitHub Desktop.
Save Gucchi0512/6dd2b74ba5ea0f9c9f671d6fcacc115f to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Compilation;
using System.IO;
namespace app
{
public class EnumDataScriptGenerator : EditorWindow
{
#region Definition
private readonly string ScriptCommonBlock = @"
/// <summary>
/// Auto generated by EnumDataScriptGenerator.
/// </summary>
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace app
{
#CLASS
}
";
private readonly string ScriptBlock = @"
public enum #CLASSNAME : uint
{
#CODE
}";
private readonly string UnitBlock = @"
/// <summary>
/// #DISPLAYNAME
/// </summary>
[EnumDisplayName(""#DISPLAYNAME"")]
#VARIABLENAME = #HASHU,
";
private readonly string DictionarySetBlock = @"
{#HASH, ""#DISPLAYNAME""},
";
private readonly string FolderPath = "Assets/Scripts/EnumData/DataDefinitionScript";
private readonly string ResourceFolderPath = "ScriptableObject/EnumData";
#endregion
#region Property
private string SelectedItem
{
get
{
if (_AssetsName.Count <= _SelectedIndex)
{
return "";
}
return _AssetsName[_SelectedIndex];
}
}
#endregion
#region Field
private EnumData _Data = null;
private List<string> _AssetsName = new List<string>();
private int _SelectedIndex = 0;
#endregion
#region Method
[MenuItem("EnumData/ScriptGenerator")]
private static void Open()
{
EditorWindow.GetWindow<EnumDataScriptGenerator>("EnumDataScriptGenerator");
}
private void OnGUI()
{
if (GUILayout.Button("ファイル更新"))
{
CollectAssets();
}
_SelectedIndex = EditorGUILayout.Popup(_SelectedIndex, _AssetsName.ToArray());
if (GUILayout.Button("スクリプトファイル生成"))
{
if (LoadAsset() == false)
{
return;
}
Generate();
}
}
private void CollectAssets()
{
_AssetsName.Clear();
var path = "Assets/" + "Resources/" + ResourceFolderPath;
var absPath = UtilApp.ConvertAssetsPath2AbsPath(path);
var fileNames = Directory.GetFiles(absPath);
foreach (var name in fileNames)
{
if (name.Contains(".meta"))
{
continue;
}
var newName = name.Replace(absPath + "\\", "").Replace(".asset", "");
_AssetsName.Add(newName);
}
}
private bool LoadAsset()
{
var path = ResourceFolderPath + "/" + SelectedItem;
var resource = Resources.Load(path);
if (resource is EnumData data)
{
_Data = data;
Debug.Log($"[EnumData][ScriptGenerator]: {_Data}を読み込みました");
return true;
}
Debug.LogError($"[EnumData][ScriptGenerator]: {path}の読み込みに失敗しました");
return false;
}
private void Generate()
{
var script = "";
script += ScriptCommonBlock;
var code = "";
foreach (var unit in _Data.Units)
{
var value = UnitBlock;
value = value.Replace("#DISPLAYNAME", unit.DisplayName);
value = value.Replace("#VARIABLENAME", unit.VariableName);
var hash = UtilApp.GetHash(unit.VariableName).ToString();
value = value.Replace("#HASH", hash);
code += value;
}
var classBlock = "";
classBlock += ScriptBlock;
classBlock = classBlock.Replace("#CLASSNAME", _Data.DataClassName);
classBlock = classBlock.Replace("#CODE", code);
script = script.Replace("#CLASS", classBlock);
var fileName = "/" + _Data.DataClassName + ".cs";
var path = UtilApp.ConvertAssetsPath2AbsPath(FolderPath + fileName);
using (StreamWriter sw = new StreamWriter(path, false))
{
sw.WriteLine(script);
Debug.Log($"[EnumData][ScriptGenerator]: {path}を出力しました");
}
CompilationPipeline.RequestScriptCompilation();
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment