选中Project目录中你需要创建脚本的文件夹,点击右键,然后选择创建Lua脚本。
创建Lua脚本后,将脚本文件改为rename mode,修改脚本名字。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
public class CreateLuaScriptEditor : Editor {
static double renameTime = 0;
[MenuItem("Assets/Create/Lua Script")]
public static void CreateLua()
{
UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel);
string path = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/" + AssetDatabase.GetAssetPath(arr[0]);
DateTime dt = DateTime.Now;
string context = string.Format("--Author : {0}\n--Date : {1}", GetUserName(), dt.ToShortDateString().ToString());
string fileName = "LuaScript.lua";
if (File.Exists(path + "/" + fileName))
{
fileName = "LuaScript 1.lua";
}
File.AppendAllText(path + "/" + fileName, context);
AssetDatabase.Refresh();
string s = AssetDatabase.GetAssetPath(arr[0]) + "/" + fileName;
Selection.activeObject = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
renameTime = EditorApplication.timeSinceStartup + 0.1d;
EditorApplication.update += EngageRenameMode;
}
// 重命名
public static void EngageRenameMode()
{
if (EditorApplication.timeSinceStartup >= renameTime)
{
EditorApplication.update -= EngageRenameMode;
EditorApplication.ExecuteMenuItem("Window/Project"); // focus on the project window
EditorWindow.focusedWindow.SendEvent(new Event() { keyCode = KeyCode.F2, type = EventType.KeyDown });
}
}
public static string GetUserName()
{
try
{
return Environment.UserName;
}
catch
{
return "unknown";
}
}
}