本論文では、テンプレートファイルのテンプレートを提供し、.lua.txt形式で.lua xluaを作成します。
:メインの参照以来(例えばLuaの)ジェーンの本-Unityテクノロジー拡張スクリプトテンプレートerick_book
我々は拡張を追加したように、元のブロガーの記事では、xluaに関数を使用するために、最近の必要性のためにLUAファイルを、作成するためのテンプレートをサポートし、xlua接尾.lua.txt。
、彼は単に上に移動するためのコードを見てまあ、いわゆるホイールを再作成しないでください、それを共有するために、ここで、簡単な変更のポイントを作りました。
主にそれは、GitHubの上で利用できるようにプロジェクトディレクトリに直接移動するには、一瞬のために2つのスクリプトが含まれてhttps://github.com/zhimo1997/CustomTools
使用します。
1.luaテンプレートファイル
自分自身を定義する必要があり、その後、ディレクトリがに割り当てることができます。
プライベート静的読み取り専用文字列LuaScriptTemplatePath = "資産/ 1-LuaTemplate /エディタ/ LuaTemplate.txt";
2.luaファイルのアイコン
これはしてもしなくてもよい、Unityはデフォルトのアイコンを提供しますこと。
プライベート静的読み取り専用文字列LuaScriptIconPath = "資産/ツール/ ScriptTemplates / lua_icon64.png";
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using Microsoft.VisualBasic;
public static class LuaScriptCreater
{
private static readonly string LuaScriptIconPath = "Assets/Tools/ScriptTemplates/lua_icon64.png";
private static readonly string LuaScriptTemplatePath = "Assets/1-LuaTemplate/Editor/LuaTemplate.txt";
[MenuItem("Assets/Create/Lua Script", false, 81)]
private static void CreateLuaScript()
{
CreateLuaTemplate(".lua");
}
[MenuItem("Assets/Create/xLua Script", false, 81)]
private static void CreateXLuaScript()
{
CreateLuaTemplate(".txt");
}
private static void CreateLuaTemplate(string ext){
if (EditorApplication.isCompiling || EditorApplication.isPlaying)
{
EditorUtility.DisplayDialog("警告", "无法在游戏运行时或代码编译时创建lua脚本", "确定");
return;
}
Texture2D icon = AssetDatabase.LoadAssetAtPath<Texture2D>(LuaScriptIconPath);
string scriptDirPath = PathUtil.GetSelectionAssetDirPath();
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
ScriptableObject.CreateInstance<CreateLuaScriptAction>(),
scriptDirPath + "/NewLuaScript"+ext, icon,
LuaScriptTemplatePath);
}
}
internal class CreateLuaScriptAction : EndNameEditAction
{
public override void Action(int instanceId, string pathName, string resourceFile)
{
string content = File.ReadAllText(PathUtil.GetDiskPath(resourceFile));
string fileName = Path.GetFileNameWithoutExtension(pathName);
content = content.Replace("#NAME#", fileName);
string fullName = PathUtil.GetDiskPath(pathName);
File.WriteAllText(fullName, content);
AssetDatabase.ImportAsset(pathName);
string ext=Path.GetExtension(pathName);
if(ext==".txt"){
string oldPathName=pathName;
string newPathName=Path.ChangeExtension(pathName,".lua");
newPathName+=".txt";
if(File.Exists(PathUtil.GetDiskPath(oldPathName))){
File.Move(PathUtil.GetDiskPath(oldPathName),PathUtil.GetDiskPath(newPathName));
pathName=newPathName;
}
}
Object obj = AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
ProjectWindowUtil.ShowCreatedAsset(obj);
AssetDatabase.Refresh();
}
}
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
public static class PathUtil
{
public static string GetAssetPath(string dirPath)
{
dirPath = dirPath.Replace("\\", "/");
if (dirPath.StartsWith(Application.dataPath))
{
return "Assets" + dirPath.Replace(Application.dataPath, "");
}
return string.Empty;
}
public static string GetDiskPath(string assetPath)
{
if (string.IsNullOrEmpty(assetPath))
{
return string.Empty;
}
assetPath = assetPath.Replace("\\", "/");
if (!assetPath.StartsWith("Assets"))
{
return string.Empty;
}
return Application.dataPath + assetPath.Substring(assetPath.IndexOf("Assets") + 6);
}
public static string GetSelectionAssetDirPath()
{
string path = "Assets";
foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
{
if(obj == null)
{
continue;
}
path = AssetDatabase.GetAssetPath(obj);
if(Path.HasExtension(path))
{
path = Path.GetDirectoryName(path);
}
break;
}
return path;
}
}