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();
}
}
}