using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using System.IO;
using UnityEditor.ProjectWindowCallback;
public class AutoCreatScripts
{
//ProjectWindowUtil.StartNameEditingIfProjectWindowExists();
[MenuItem("Assets/Create/U3DEventFrame Lua Script", false, 80)]
public static void CreatNewLua()
{
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
ScriptableObject.CreateInstance<CraetLuaScriptAsset>(),
GetSelectedPathOrFallBack() + "/New Lua.lua",
null,
"Assets/MyFrame/Editor/LuaClass.lua"
);
}
[MenuItem("Assets/Create/U3DEventFrame C# Script",false,70)]
public static void CreatEventCs()
{
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
ScriptableObject.CreateInstance<CraetEventCSScriptAsset>(),
GetSelectedPathOrFallBack()+"/New Script.cs",
null,
"Assets/MyFrame/Editor/EventCSClass.cs" // 只需要在此文件夹下创建你需要的模板,命名与EventCSClass.cs一致就行
);
}
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 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));
}
}
class CraetEventCSScriptAsset : 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, "EventCSClass", 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));
}
}
}
Unity创建模板脚本
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.修改81-C# Script-NewBehaviourScript.cs.txt文件 打开unity安装目录对...
- 1.找到文件所在位置 2.找到脚本模板 3.右击Edit with Notepad++打开文本 4.修改脚本模板,...
- 在安装unity的目录中依次查找下面的路径Unity\Editor\Data\Resources\ScriptTe...