Unity编辑器扩展_给已经生成的脚本添加和修改命名空间

 

ScriptableWizard类介绍:

ScriptableWizard类的公有变量都会显示在窗口面板上。

using UnityEditor;
public class Fan_类名 : ScriptableWizard
{
    // 在菜单栏中会看到新增的MyTools,其下有一项名为"工具名称",点击后打开工具界面
    [MenuItem("MyTools/工具名称")]
    static void CreateWizard ()
    {
        //                              工具界面名称                    右按钮名称  左按钮名称
        // 确定按钮:点击后执行OnWizardCreate后关闭本工具界面
        // 取消按钮:点击后执行OnWizardOtherButton
        ScriptableWizard.DisplayWizard("界面窗口名称", typeof(Fan_类名),"确定","取消");
    }

    // 点击确定按钮调用
    void OnWizardCreate ()
    {
    }
    
    // 点击取消按钮调用
    void OnWizardOtherButton ()
    {
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;

/// <summary>
/// 给已经生成的脚本添加和修改命名空间
/// </summary>
public class AddNamespaceToGeneratedscripts_editor : ScriptableWizard
{

    //ScriptableWizard类的公有变量都会显示在窗口面板上
    public string folder = "Assets/";
    public string namespaceName;

    static float windowWidth = 500f;
   static float windowHeight = 500f;


    void OnEnable()
    {
        if (Selection.activeObject != null)
        {
            string dirPath = AssetDatabase.GetAssetOrScenePath(Selection.activeObject);
            if (File.Exists(dirPath))
            {
                dirPath = dirPath.Substring(0, dirPath.LastIndexOf("/"));
            }
            folder = dirPath;
        }
    }

    [MenuItem("编辑器扩展/Add Namespace", false, 10)]
    static void CreateWizard()
    {
        AddNamespaceToGeneratedscripts_editor editor = ScriptableWizard.DisplayWizard<AddNamespaceToGeneratedscripts_editor>("Add Namespace", "添加命名空间","取消");
        editor.minSize = new Vector2(windowWidth, windowHeight);
    }

    /// <summary>
    /// 点击取消按钮时调用
    /// </summary>
    private void OnWizardOtherButton()
    {
        Debug.Log("取消");
    }

    /// <summary>
    /// 点击确定按钮时调用,这里的确定按钮就是"添加命名空间"
    /// </summary>
    public void OnWizardCreate()
    {
        //save settting

        if (!string.IsNullOrEmpty(folder) && !string.IsNullOrEmpty(namespaceName))
        {

            List<string> filesPaths = new List<string>();
            filesPaths.AddRange(
                Directory.GetFiles(Path.GetFullPath(".") + Path.DirectorySeparatorChar + folder, "*.cs", SearchOption.AllDirectories)
            );
            Dictionary<string, bool> scripts = new Dictionary<string, bool>();

            int counter = -1;
            foreach (string filePath in filesPaths)
            {

                scripts[filePath] = true;

                EditorUtility.DisplayProgressBar("Add Namespace", filePath, counter / (float)filesPaths.Count);
                counter++;

                string contents = File.ReadAllText(filePath);

                string result = "";
                bool havsNS = contents.Contains("namespace ");
                string t = havsNS ? "" : "\t";

                using (TextReader reader = new StringReader(contents))
                {
                    int index = 0;
                    bool addedNS = false;
                    while (reader.Peek() != -1)
                    {
                        string line = reader.ReadLine();

                        if (line.IndexOf("using") > -1 || line.Contains("#"))
                        {
                            result += line + "\n";
                        }
                        else if (!addedNS && !havsNS)
                        {
                            result += "\nnamespace " + namespaceName + " {";
                            addedNS = true;
                            result += t + line + "\n";
                        }
                        else
                        {
                            if (havsNS && line.Contains("namespace "))
                            {
                                if (line.Contains("{"))
                                {
                                    result += "namespace " + namespaceName + " {\n";
                                }
                                else
                                {
                                    result += "namespace " + namespaceName + "\n";
                                }
                            }
                            else
                            {
                                result += t + line + "\n";
                            }
                        }
                        ++index;
                    }
                    reader.Close();
                }
                if (!havsNS)
                {
                    result += "}";
                }
                File.WriteAllText(filePath, result);
            }



            //处理加了命名空间后出现方法miss
            filesPaths.AddRange(
                Directory.GetFiles(Path.GetFullPath(".") + Path.DirectorySeparatorChar + folder, "*.unnity", SearchOption.AllDirectories)
            );
            filesPaths.AddRange(
                Directory.GetFiles(Path.GetFullPath(".") + Path.DirectorySeparatorChar + folder, "*.prefab", SearchOption.AllDirectories)
            );


            counter = -1;
            foreach (string filePath in filesPaths)
            {
                EditorUtility.DisplayProgressBar("Modify Script Ref", filePath, counter / (float)filesPaths.Count);
                counter++;

                string contents = File.ReadAllText(filePath);

                string result = "";
                using (TextReader reader = new StringReader(contents))
                {
                    int index = 0;
                    bool addedNS = false;
                    while (reader.Peek() != -1)
                    {
                        string line = reader.ReadLine();

                        if (line.IndexOf("m_ObjectArgumentAssemblyTypeName:") > -1 && !line.Contains(namespaceName))
                        {

                            string scriptName = line.Split(':')[1].Split(',')[0].Trim();
                            if (scripts.ContainsKey(scriptName))
                            {
                                line = line.Replace(scriptName, "namespaceName." + scriptName);
                            }

                            result += line + "\n";
                        }
                        else
                        {
                            result += line + "\n";
                        }
                        ++index;
                    }
                    reader.Close();
                }

                File.WriteAllText(filePath, result);
            }


            EditorUtility.ClearProgressBar();
            AssetDatabase.Refresh();
        }
    }
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值