Unity3D 自定义格式化创建模板+编辑器命名

csharp 复制代码
        [MenuItem("Assets/Create/Format/Unit Script", false, 0)]
        public static void CreateUnitScript()
        {
            FileCreateHelper.CreateScriptWithTitle(@$"
using System;
using System.Collections;
using System.Collections.Generic;
using AirFramework;

namespace {FrameworkSettings.instance.defaultNamespace}
{{
    public class #NAME# : Unit
    {{
        protected override void OnDispose()
        {{
            
        }}
    }}
}}

");
        }
csharp 复制代码
public static void CreateScript(string source, string defaultName = "NewScript.cs", Texture2D icon = null)
        {
            //开始编辑名字的操作
            ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
            ScriptableObject.CreateInstance<CreateScriptAsset>(),
            $"{FileCreateHelper.GetSelectedPathOrFallback()}/{defaultName}",
            icon, source
           );
        }
csharp 复制代码
using System.IO;
using System.Text;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;

namespace AirFrameworkEditor
{
    //用于创建文本
    class CreateScriptAsset : EndNameEditAction
    {
        public override void Action(int instanceId, string pathName, string sourceCode)
        {
            UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, sourceCode);
            ProjectWindowUtil.ShowCreatedAsset(o);
        }

        internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
        {
            string fullPath = Path.GetFullPath(pathName);

            string text = resourceFile;

            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName).Split('.')[0];
            text = text.Replace("#NAME#", fileNameWithoutExtension.Replace(" ", string.Empty));

            bool encoderShouldEmitUTF8Identifier = true;
            bool throwOnInvalidBytes = false;
            UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes);
            bool append = false;
            StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding);
            streamWriter.Write(text);
            streamWriter.Close();

            AssetDatabase.ImportAsset(pathName);
            AssetDatabase.Refresh();
            return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
        }
    }
}
相关推荐
红黑色的圣西罗15 分钟前
C# List.Sort方法总结
开发语言·c#
一步一个foot-print2 小时前
[Unity Shader Base] RayMarching in Cloud Rendering
unity·游戏引擎
ithinking1102 小时前
kotlin 集成 unity
unity·android studio
夏霞4 小时前
c# ASP.NET Core SignalR 客户端配置自动重连次数
c#·.netcore
2501_930707784 小时前
使用C#代码在 Word 文档中查找并替换文本
开发语言·c#·word
wanhengidc5 小时前
云手机是真实手机吗
运维·服务器·游戏·智能手机·云计算
AnalogElectronic5 小时前
用AI写游戏4——Python实现飞机大战小游戏1
python·游戏·pygame
APP出海6 小时前
Google政策大更新:涉及金融(个人贷款),社交约会与游戏(未成年人相关),健康等所有类别App
android·游戏·金融·产品运营·产品经理
一个帅气昵称啊6 小时前
在.NET中使用RAG检索增强AI基于Qdrant的矢量化数据库
ai·性能优化·c#·.net·rag·qdrant