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));
        }
    }
}
相关推荐
春卷同学1 天前
篮球游戏 - Electron for 鸿蒙PC项目实战案例
游戏·electron·harmonyos
yue0081 天前
C# Environment类的介绍
开发语言·c#·environment
i橡皮擦1 天前
使用gamedig 查询恐龙岛TheIsle游戏服务器
运维·服务器·游戏·steam·恐龙岛·the isle
c#上位机1 天前
halcon图像去噪—高斯滤波
c#·上位机·halcon·机器视觉
春卷同学1 天前
滑雪游戏 - Electron for 鸿蒙PC项目实战案例
游戏·electron·harmonyos
神奇的板烧1 天前
Java泛型不变性引发的类型转换问题及解决方案
java·c#
刻刻帝的海角1 天前
创建一个简单的记忆翻牌游戏
游戏
Aevget1 天前
界面控件开发包DevExpress v25.1.7更新上线——修复一些小bug
c#·wpf·winform·devexpress·ui开发·用户界面
兔儿资源1 天前
游戏包站GM手游
游戏
春卷同学1 天前
基于Electron开发的跨平台鸿蒙PC剪刀石头布游戏应用
游戏·electron·harmonyos