创建Unity代码模板

创建代码模板

废话不多说先上代码

public static class EditorUtilities
{
        private static Texture2D scriptIcon = (EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D);

        [MenuItem("Assets/Create/Code C# Script", false, 89)]
        private static void CreateCode()
        {
            string[] guids = AssetDatabase.FindAssets("CodeTemplate.cs");
            if (guids.Length == 0)
            {
                Debug.LogWarning("CodeTemplate.cs.txt not found in asset database");
                return;
            }
            string path = AssetDatabase.GUIDToAssetPath(guids[0]);
            CreateFromTemplate("NewCode.cs", path);
        }



        public static void CreateFromTemplate(string initialName, string templatePath)
        {
            ProjectWindowUtil.StartNameEditingIfProjectWindowExists(
                0,
                ScriptableObject.CreateInstance<DoCreateCodeFile>(),
                initialName,
                scriptIcon,
                templatePath
            );
        }

        public class DoCreateCodeFile : UnityEditor.ProjectWindowCallback.EndNameEditAction
        {
            public override void Action(int instanceId, string pathName, string resourceFile)
            {
                Object o = CreateScript(pathName, resourceFile);
                ProjectWindowUtil.ShowCreatedAsset(o);
            }
        }

        internal static UnityEngine.Object CreateScript(string pathName, string templatePath)
        {
            string className = Path.GetFileNameWithoutExtension(pathName).Replace(" ", string.Empty);
            string templateText = string.Empty;

            UTF8Encoding encoding = new UTF8Encoding(true, false);

            if (File.Exists(templatePath))
            {
                StreamReader reader = new StreamReader(templatePath);
                templateText = reader.ReadToEnd();
                reader.Close();

                templateText = templateText.Replace("#SCRIPTNAME#", className);
                templateText = templateText.Replace("#NOTRIM#", string.Empty);

                StreamWriter writer = new StreamWriter(Path.GetFullPath(pathName), false, encoding);
                writer.Write(templateText);
                writer.Close();

                AssetDatabase.ImportAsset(pathName);
                return AssetDatabase.LoadAssetAtPath(pathName, typeof(Object));
            }
            else
            {
                Debug.LogError(string.Format("The template file was not found: {0}", templatePath));
                return null;
            }
        }
}

然后(CodeTemplate.cs.txt)放在Resources/ScriptTemplates文件夹下

模板的类容参考如下:

/*
作者:一骑孤烟
*/

using System.Collections.Generic;
using UnityEngine;

public class #SCRIPTNAME#
{
    private void Init() 
    {
        #NOTRIM#
    }
}

如果你嫌弃上述方法太麻烦好的介绍一个跟简单的方法

将上述的模板按照如下格式命名

序号-在Create中的目录-新建文件的默认名字

89-Code C# Script-NewCode.cs.txt

注意如果在Create下需要分目录的话,用__链接,该方法是全局的及针对Unity编辑器的设置,使用第一种方法可以在需要的项目中使用。

猜你喜欢

转载自blog.csdn.net/qq_35443068/article/details/79989641