代码生成脚本

public class CreatStateTypeScripts
{
    public static void CreatScripts()
    {
        foreach (StateType item in System.Enum.GetValues(typeof(StateType)))
        {
            string savePath = "/Scripts/AnimatorScripts/States/";

            string classname = item.ToString();

            StringBuilder sb = new StringBuilder();
            sb.Append("/*------------------------------------------------------------\n");
            sb.Append("--------------------Generate By Utilities.--------------------\n");
            sb.Append("------------------------------------------------------------*/\n");
            sb.Append("public class ").Append(classname).Append(" : ").Append("StateBase").Append("\n");
            sb.Append("{\n");
            sb.Append("    public override void OnEnter()");
            sb.Append("\n");
            sb.Append("    {\n");
            sb.Append("        m_Aniamtor.SetBool(\"");
            sb.Append(classname);
            sb.Append("\", true);");
            sb.Append("\n");
            sb.Append("    }\n");

            sb.Append("    public override void OnExit()");
            sb.Append("\n");
            sb.Append("    {\n");
            sb.Append("        m_Aniamtor.SetBool(\"");
            sb.Append(classname);
            sb.Append("\", false);");
            sb.Append("\n");
            sb.Append("    }\n");

            sb.Append("    public override void OnUpdate()");
            sb.Append("\n");
            sb.Append("    {\n");
            sb.Append("        base.OnUpdate();");
            sb.Append("\n");
            sb.Append("    }\n");

            sb.Append("}");
            string dirPath = Application.dataPath + savePath;
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
            string path = dirPath + classname + ".cs";
            File.Open(path, FileMode.OpenOrCreate).Dispose();
            File.WriteAllText(path, sb.ToString(), Encoding.UTF8);
            Debug.Log(string.Format("输出文件:{0}.cs", classname));
        }
        AssetDatabase.Refresh();
    }
}
    void OnGUI()
    {
        EditorGUILayout.Space();
        GUI.color = Color.yellow;
        if (GUILayout.Button("生成StateType脚本"))
        {
            //TODO:根据枚举名称生成脚本
            CreatStateTypeScripts.CreatScripts();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值