美文网首页
Unity创建模板脚本

Unity创建模板脚本

作者: UnityChan | 来源:发表于2019-10-31 13:46 被阅读0次
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    using UnityEditor;
    using System.IO;
    using UnityEditor.ProjectWindowCallback;
    
    public class AutoCreatScripts 
    {
        //ProjectWindowUtil.StartNameEditingIfProjectWindowExists();
    
        [MenuItem("Assets/Create/U3DEventFrame Lua Script", false, 80)]
        public static void CreatNewLua()
        {
            ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
                ScriptableObject.CreateInstance<CraetLuaScriptAsset>(),
                GetSelectedPathOrFallBack() + "/New Lua.lua",
                null,
                "Assets/MyFrame/Editor/LuaClass.lua"
                );
    
        }
    
        [MenuItem("Assets/Create/U3DEventFrame C# Script",false,70)]
        public static void CreatEventCs()
        {
            ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
                ScriptableObject.CreateInstance<CraetEventCSScriptAsset>(),
                GetSelectedPathOrFallBack()+"/New Script.cs",
                null,
                "Assets/MyFrame/Editor/EventCSClass.cs"         // 只需要在此文件夹下创建你需要的模板,命名与EventCSClass.cs一致就行
                );
    
        }
    
        public static string GetSelectedPathOrFallBack()
        {
            string path = "Assets";
            foreach (UnityEngine.Object obj  in Selection.GetFiltered(typeof(UnityEngine.Object),SelectionMode.Assets))
            {
                path = AssetDatabase.GetAssetPath(obj);
                if (!string.IsNullOrEmpty(path) && File.Exists(path))
                {
                    path = Path.GetDirectoryName(path);
                    break;
                }
            }
            return path;
        }
    
    
        class CraetLuaScriptAsset : EndNameEditAction
        {
            public override void Action(int instanceId, string pathName, string resourceFile)
            {
                UnityEngine.Object o = CreatScriptAssetFormTemplate(pathName, resourceFile);
                ProjectWindowUtil.ShowCreatedAsset(o);
            }
    
    
            internal static UnityEngine.Object CreatScriptAssetFormTemplate(string pathName, string resourcesFile)
            {
                string fullName = Path.GetFullPath(pathName);
                StreamReader streamReader = new StreamReader(resourcesFile);
                string text = streamReader.ReadToEnd();
                streamReader.Close();
                string fileNameWithOutExtension = Path.GetFileNameWithoutExtension(pathName);
    
                Debug.Log("text == "+text);
                text = Regex.Replace(text,"LuaClass", fileNameWithOutExtension);
    
                bool encoderShouldEmitUTF8Identifier = true;
                bool throwOnInvalidBytes = false;
                UTF8Encoding uTF8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes);
                bool append = false;
    
                StreamWriter streamWriter = new StreamWriter(fullName,append, uTF8Encoding);
                streamWriter.Write(text);
                streamWriter.Close();
                AssetDatabase.ImportAsset(pathName);
                return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
            }
        }
    
    
        class CraetEventCSScriptAsset : EndNameEditAction
        {
            public override void Action(int instanceId, string pathName, string resourceFile)
            {
                UnityEngine.Object o = CreatScriptAssetFormTemplate(pathName, resourceFile);
                ProjectWindowUtil.ShowCreatedAsset(o);
            }
    
    
            internal static UnityEngine.Object CreatScriptAssetFormTemplate(string pathName, string resourcesFile)
            {
                string fullName = Path.GetFullPath(pathName);
                StreamReader streamReader = new StreamReader(resourcesFile);
                string text = streamReader.ReadToEnd();
                streamReader.Close();
                string fileNameWithOutExtension = Path.GetFileNameWithoutExtension(pathName);
    
                Debug.Log("text == " + text);
                text = Regex.Replace(text, "EventCSClass", fileNameWithOutExtension);
    
                bool encoderShouldEmitUTF8Identifier = true;
                bool throwOnInvalidBytes = false;
                UTF8Encoding uTF8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes);
                bool append = false;
    
                StreamWriter streamWriter = new StreamWriter(fullName, append, uTF8Encoding);
                streamWriter.Write(text);
                streamWriter.Close();
                AssetDatabase.ImportAsset(pathName);
                return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
            }
        }
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:Unity创建模板脚本

          本文链接:https://www.haomeiwen.com/subject/vyftbctx.html