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