编辑器内部自定义脚本存储位置展示

创建自己的自定义脚本

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using UnityEngine;
internal class CustomUnityEditor : EditorWindow
{
private static EditorWindow _window;
private static List<Dictionary<string, string>> _paths;
[MenuItem("Assets/Create/自定义脚本(多选)", false, 0)]
private static void CreateCustomScripts()
{
_paths = GetSelectedDirPath();
_window = GetWindow<CustomUnityEditor>("创建脚本(多选)");
_window.Show();
}
[MenuItem("Assets/Create/自定义脚本(单选)", false, 1)]
private static void CreateCustomScript()
{
_paths = new List<Dictionary<string, string>>();
var paths = GetSelectedDirPath();
if (paths.Count != 0)
{
_paths.Add(paths[paths.Count - 1]);
CreateScriptFile();
}
else
EditorUtility.DisplayDialog("提示", "请选择文件夹", "确定");
}
private static List<Dictionary<string, string>> GetSelectedDirPath()
{
var selections =
Selection.GetFiltered(typeof(Object), SelectionMode.Assets);
return (from obj in selections
let path = AssetDatabase.GetAssetPath(obj)
where Directory.Exists(path)
select new Dictionary<string, string>
{
{ "name", "NewScript" },
{ "path", AssetDatabase.GetAssetPath(obj) }
}).ToList();
}
private static void CreateScriptFile()
{
foreach (var pathInfo in _paths.Where(pathInfo => pathInfo["name"] != ""))
{
const int instanceId = 0;
var endAction = CreateInstance<NameByEnterOrUnfocus>();
var pathName = $"{pathInfo["path"]}/{pathInfo["name"]}.cs";
const string resourceFile = "Assets/Editor/ScriptTemplates/ScriptTemplate.cs.txt";
#if false
* 参数1:instanceId 已编辑资源的实例 ID。
* 参数2:endAction 监听编辑名称的类的实例化
* 参数3:pathName 创建的文件路径(包括文件名)
* 参数4:icon 图标
* 参数5:resourceFile 模板路径
endAction 直接使用 new NameByEnterOrUnfocus() 出现以下警告:
NameByEnterOrUnfocus must be instantiated using the ScriptableObject.CreateInstance method instead of new NameByEnterOrUnfocus.
必须使用ScriptableObject实例化NameByEnterOrUnfocus。CreateInstance方法,而不是新的NameByEnterOrUnfocus。
#endif
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(instanceId, endAction, pathName, null,
resourceFile);
}
}
private void OnGUI()
{
GUILayout.Label("⚠ 不填写文件名称不会创建脚本", new GUIStyle { normal = { textColor = Color.red } });
foreach (var pathInfo in _paths)
pathInfo["name"] = EditorGUILayout.TextField(pathInfo["path"], pathInfo["name"]);
if (GUILayout.Button("确定"))
{
CreateScriptFile();
_window.Close();
}
}
}
internal class NameByEnterOrUnfocus : EndNameEditAction
{
public override void Action(int instanceId, string pathName, string resourceFile)
{
var obj = CreateScript(pathName, resourceFile);
ProjectWindowUtil.ShowCreatedAsset(obj);
}
private static Object CreateScript(string pathName, string resourceFile)
{
var streamReader = new StreamReader(resourceFile);
var templateText = streamReader.ReadToEnd();
streamReader.Close();
var fileName = Path.GetFileNameWithoutExtension(pathName);
var scriptText = Regex.Replace(templateText, "#NAME#", fileName);
var streamWriter = new StreamWriter(pathName);
streamWriter.Write(scriptText);
streamWriter.Close();
AssetDatabase.ImportAsset(pathName);
return AssetDatabase.LoadAssetAtPath(pathName, typeof(Object));
}
}