shaoxiadiablo/Assets/AFramework/Editor/Scripts/EditorScriptsCreator.cs

227 lines
7.3 KiB
C#
Raw Normal View History

2025-05-18 01:04:31 +08:00
// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2020-09-08
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "EditorScriptsCreator" company="Kimch"></copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using UnityEngine;
namespace KF.ScriptHelper
{
internal class CreateAssetAction
{
/// <summary>
/// 添加Assets/Create/My C#子菜单并创建MonoBehaviour脚本
/// </summary>
[MenuItem("Assets/Create/My C#", false, 60)]
public static void CreateNormalCS()
{
//参数为传递给CreateEventCSScriptAsset类action方法的参数
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
ScriptableObject.CreateInstance<CreateCSScriptAsset>(),
GetSelectPathOrFallback() + "/NewScript.cs", EditorGUIUtility.FindTexture("cs Script Icon"),
EditorScriptsConfig.Instance.templatePath + "81-C# Script-NewBehaviourScript.cs.txt");
}
[MenuItem("Assets/Create/My UI C#/Window", false, 61)]
public static void CerateUICS()
{
if (!Selection.activeObject)
{
return;
}
AutoCreatUIScript.CreateUIScripts();
}
/// <summary>
/// 添加Assets/Create/My C#子菜单并创建MonoBehaviour脚本
/// </summary>
[MenuItem("Assets/Create/My UI C#/Widget", false, 62)]
public static void CreateUIItemCS()
{
//参数为传递给CreateEventCSScriptAsset类action方法的参数
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
ScriptableObject.CreateInstance<CreateCSScriptAsset>(),
GetSelectPathOrFallback() + "/NewScript.cs", EditorGUIUtility.FindTexture("cs Script Icon"),
EditorScriptsConfig.Instance.templatePath + "81-C# Script-UIWidgetScript.cs.txt");
}
/// <summary>
/// 获取路径
/// </summary>
/// <returns></returns>
public static string GetSelectPathOrFallback()
{
string path = "Assets";
//遍历选中的资源以获得路径
//Selection.GetFiltered是过滤选择文件或文件夹下的物体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;
}
public string GetSelectUIPathOrFallback()
{
string path = "Assets";
//遍历选中的资源以获得路径
//Selection.GetFiltered是过滤选择文件或文件夹下的物体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;
}
}
//要创建模板文件必须继承EndNameEditAction重写action方法
internal class CreateCSScriptAsset : EndNameEditAction
{
public override void Action(int instanceId, string pathName, string resourceFile)
{
//创建资源
UnityEngine.Object obj = CreateScriptAssetFromTemplate(pathName, resourceFile);
ProjectWindowUtil.ShowCreatedAsset(obj);//高亮显示资源
}
internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
{
//获取要创建资源的绝对路径
string fullPath = Path.GetFullPath(pathName);
//读取本地的模板文件
StreamReader streamReader = new StreamReader(resourceFile);
string text = streamReader.ReadToEnd();
streamReader.Close();
//获取文件名,不含扩展名
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName);
while (fileNameWithoutExtension.Contains("."))
{
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileNameWithoutExtension);
}
text = ReplaceRule(text, fileNameWithoutExtension);
//写入配置文件
bool encoderShouldEmitUTF8Identifier = true; //参数指定是否提供 Unicode 字节顺序标记
bool throwOnInvalidBytes = false;//是否在检测到无效的编码时引发异常
bool append = false;
UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes);
StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding);
streamWriter.Write(text);
streamWriter.Close();
//刷新资源管理器
AssetDatabase.ImportAsset(pathName);
AssetDatabase.Refresh();
return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
}
//将模板类中的类名替换成你创建的文件名
internal static string ReplaceRule(string text, string scriptName)
{
//将模板类中的类名替换成你创建的文件名
text = Regex.Replace(text, "#SCRIPTNAME#", scriptName);
text = Regex.Replace(text, "#AUTHOR#", EditorScriptsConfig.Instance.author);
text = Regex.Replace(text, "#COMPANY#", EditorScriptsConfig.Instance.company);
text = Regex.Replace(text, "#NAMESPACE#", EditorScriptsConfig.Instance.nameSpace);
text = Regex.Replace(text, "#DATE#", DateTime.Now.ToString("yyyy-MM-dd"));
return text;
}
}
internal class AutoCreatUIScript
{
public static void CreateUIScripts()
{
var obj = Selection.activeObject;
if (!obj)
{
EditorUtility.DisplayDialog("错误提示", "请先选择Prefab", "确认");
}
string directory = AssetDatabase.GetAssetPath(obj);
if (Directory.Exists(directory))
{
var files = Directory.GetFiles(directory);
if (files.Length < 2)
{
GenerateWindow(Path.Combine(directory, obj.name + ".cs"), obj.name);
GenerateView(Path.Combine(directory, obj.name + ".View.cs"), obj.name);
}
}
}
public static void GenerateWindow(string generateFilePath, string behaviourName)
{
var sw = new StreamWriter(generateFilePath, false, Encoding.UTF8);
var tFile = Path.Combine(EditorScriptsConfig.Instance.templatePath, "81-C# Script-UIScript.cs.txt");
StreamReader streamReader = new StreamReader(tFile);
string text = streamReader.ReadToEnd();
streamReader.Close();
text = CreateCSScriptAsset.ReplaceRule(text, behaviourName);
sw.Write(text);
sw.Flush();
sw.Close();
CreateCSScriptAsset.CreateScriptAssetFromTemplate(generateFilePath, generateFilePath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
public static void GenerateView(string generateFilePath, string behaviourName)
{
var sw = new StreamWriter(generateFilePath, false, Encoding.UTF8);
var tFile = Path.Combine(EditorScriptsConfig.Instance.templatePath, "81-C# Script-UIScript.View.cs.txt");
StreamReader streamReader = new StreamReader(tFile);
string text = streamReader.ReadToEnd();
streamReader.Close();
text = CreateCSScriptAsset.ReplaceRule(text, behaviourName);
sw.Write(text);
sw.Flush();
sw.Close();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}