unity 编辑器 创建脚本模板 Create C# Script

unity Editor 扩展 右击创建 Lua脚本

    [MenuItem("Assets/Create/Lua Script")]

    public static void CreatNewLua()

    {

        ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,

            ScriptableObject.CreateInstance<CraetLuaScriptAsset>(),

            GetSelectedPath() ,

            null,

            "lua模板路徑(自己修改)"

            );

    }

    public static string GetSelectedPath()

    {

        Object[] obj = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel);

        string path = AssetDatabase.GetAssetPath(obj[0])+ "/New Lua.lua";

        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));

        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值