U_根据脚本模板创建脚本
脚本内容:
public class MenuTool {
private static string path = "Assets/TemplateClass.txt";
[MenuItem("Assets/创建模板类")]
public static void GanerateTemplateClass() {
// 获取选中的文件名称
var obj = Selection.objects;
for (var i = 0; i < obj.Length; i++) {
var o = obj[i];
// 获取路径
var path = AssetDatabase.GetAssetPath(o.GetInstanceID());
// 获取完整文件名
var fileName = Path.GetFileName(path);
// 筛选出文件名称后面作为类替换
fileName = fileName.Substring(0, fileName.IndexOf('.'));
if (path.EndsWith(".txt")) {
// 字节流读取
var streamReader = new StreamReader(path);
var log = streamReader.ReadToEnd();
streamReader.Close();
// 替换类名
log = Regex.Replace(log, "#ClassName#", fileName);
var createPath = $"Assets/{fileName}.cs";
// 写入文件
var streamWriter = new StreamWriter(createPath, false, new UTF8Encoding(true, false));
streamWriter.Write(log);
streamWriter.Close();
AssetDatabase.ImportAsset(createPath);
Debug.Log(log);
}
}
}
}v
示例 txt 文件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class #ClassName# : MonoBehaviour {
void Start() {
}
void Update() {
}
}
演示: