Unity Project面板右键菜单创建lua文件
发表于2018-10-19
Unity Project面板右键Create按键不能创建lua文件,而且VS对Lua和Shader支持的都不是特别好,每次都得在外面创建一个txt,然后改扩展名变成lua文件,好蛋疼,有木有,为了方便大家,下面就给大家分享下个可以Project面板右键菜单创建lua文件的方法。
代码如下:
using UnityEngine; using System.Collections; using UnityEditor.ProjectWindowCallback; using System.IO; using UnityEditor; public class CreateLua { [MenuItem("Assets/Create/Lua Script",false,80)] public static void CreateNewLua() { ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<CreateScriptAssetAction>(), GetSelectedPathOrFallback() + "/New Lua.lua", null, "Assets/LuaFramework/Editor/Template/lua.lua"); } 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 CreateScriptAssetAction:EndNameEditAction { public override void Action(int instanceId, string pathName, string resourceFile) { //创建资源 UnityEngine.Object obj = CreateAssetFromTemplate(pathName, resourceFile); //高亮显示该资源 ProjectWindowUtil.ShowCreatedAsset(obj); } internal static UnityEngine.Object CreateAssetFromTemplate(string pahtName, string resourceFile) { //获取要创建的资源的绝对路径 string fullName = Path.GetFullPath(pahtName); //读取本地模板文件 StreamReader reader = new StreamReader(resourceFile); string content = reader.ReadToEnd(); reader.Close(); //获取资源的文件名 // string fileName = Path.GetFileNameWithoutExtension(pahtName); //替换默认的文件名 content = content.Replace("#TIME", System.DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss dddd")); //写入新文件 StreamWriter writer = new StreamWriter(fullName, false, System.Text.Encoding.UTF8); writer.Write(content); writer.Close(); //刷新本地资源 AssetDatabase.ImportAsset(pahtName); AssetDatabase.Refresh(); return AssetDatabase.LoadAssetAtPath(pahtName, typeof(UnityEngine.Object)); } }
那么怎么放置这个文件呢,如下图
Template中的lua文件是模板,lua文件中你可以添加各种声明哦,记得这个lua文件编码格式最后是utf-8,要不会遇到神奇的问题,不告诉你,自己去试试吧,哈哈!
来自:https://blog.csdn.net/a958832776/article/details/70807862