1、MenuItem
using UnityEngine;
using System.Collections;
using UnityEditor;
public class TestScriptableObject : ScriptableObject {
[MenuItem("CustomTools/AddChild")]
static void MenuAddChild(){
Transform[] transforms = Selection.GetTransforms(SelectionMode.Unfiltered);
foreach(var tf in transforms){
GameObject newGo = new GameObject("child");
newGo.transform.parent = tf;
}
}
}
2、ScriptableWizard
using UnityEngine;
using System.Collections;
using UnityEditor;
public class TestScriptableWizard : ScriptableWizard {
public string componentType = null;
/// <summary>
/// 当没有任何GameObject被选中的时候,将菜单disable(注意,这个函数名可以随意取)
/// </summary>
/// <returns></returns>
[MenuItem("CustomTools/CreateWindow", true)]
static bool CreateWindowDisabled()
{
return Selection.activeTransform;
}
/// <summary>
/// 创建编辑窗口(注意,这个函数名可以随意取)
/// </summary>
[MenuItem("CustomTools/CreateWindow")]
static void CreateWindow()
{
// 定制窗口标题和按钮,其中第二个参数是Create按钮,第三个则属于other按钮
// 如果不想使用other按钮,则可调用DisplayWizard的两参数版本
ScriptableWizard.DisplayWizard<TestScriptableWizard>(
"Add or remove components recursivly",
"Add", "Remove");
}
/// <summary>
/// 窗口创建或窗口内容更改时调用
/// </summary>
void OnWizardUpdate()
{
helpString = "Note: Duplicates are not created";
if (string.IsNullOrEmpty(componentType))
{
errorString = "Please enter component class name";
isValid = false;
}
else
{
errorString = "";
isValid = true;
}
}
/// <summary>
/// 点击Add按钮(即Create按钮)调用
/// </summary>
void OnWizardCreate()
{
int c = 0;
Transform[] ts = Selection.GetTransforms(SelectionMode.Deep);
foreach (Transform t in ts)
{
if (t.gameObject.GetComponent(componentType) == null)
{
if (UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(t.gameObject, "Assets/Editor/CustomEditor/TestScriptableWizard.cs (62,9)", componentType) == null)
{
Debug.LogWarning("Component of type " + componentType + " does not exist");
return;
}
c++;
}
}
Debug.Log("Added " + c + " components of type " + componentType);
}
/// <summary>
/// 点击Remove(即other按钮)调用
/// </summary>
void OnWizardOtherButton()
{
int c = 0;
Transform[] ts = Selection.GetTransforms(SelectionMode.Deep);
foreach (Transform t in ts)
{
if (t.GetComponent(componentType) != null)
{
DestroyImmediate(t.GetComponent(componentType));
c++;
}
}
Debug.Log("Removed " + c + " components of type " + componentType);
Close();
}
}
3、EditorWindow
using UnityEngine;
using System.Collections;
using UnityEditor;
public enum WindowType{type1, type2}
public class TestEditorWindow : EditorWindow {
public string[] listToolbarName = {"ToolsBar0", "ToolsBar1"};
private WindowType windowType;
private bool bCheckBox0;
private Transform transformWork;
private int iOneChoice;
private int iTwoChoice;
private bool bCheckBox1;
[MenuItem("CustomTools/CustomWindow")]
static void CreatWindow(){
TestEditorWindow win = EditorWindow.GetWindow<TestEditorWindow>();
win.Show();
}
/// <summary>
/// 注意一定是OnGUI,不是Update
/// </summary>
void OnGUI(){
windowType = (WindowType)GUILayout.Toolbar((int)windowType, listToolbarName);
ShowWitchWindow(windowType);
}
void ShowWitchWindow(WindowType windowType){
switch(windowType){
case WindowType.type1:
ShowWindow0(windowType);
break;
case WindowType.type2:
break;
default:
break;
}
}
void ShowWindow0(WindowType windowType){
GUILayout.Label(windowType.ToString(), EditorStyles.boldLabel);
//单选框
GUILayout.BeginHorizontal();
bCheckBox0 = GUILayout.Toggle(bCheckBox0, "CheckBox0");
bCheckBox1 = GUILayout.Toggle(bCheckBox1, "CheckBox1");
GUILayout.EndHorizontal();
EditorGUILayout.Space();
//Transform - Object
GUILayout.BeginHorizontal();
GUILayout.Label("Transform : ");
transformWork = (Transform)EditorGUILayout.ObjectField(transformWork,typeof(Transform));
GUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.BeginVertical();
GUILayout.Label("ONE : ");
iOneChoice = GUILayout.SelectionGrid(iOneChoice, new string[]{"00", "01", "02"},1);
EditorGUILayout.EndVertical();
EditorGUILayout.BeginVertical();
GUILayout.Label("TWO : ");
iTwoChoice = GUILayout.SelectionGrid(iTwoChoice, new string[]{"10", "11", "12"},1);
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
}
}
4、Editor
using UnityEngine;
using System.Collections;
using System;
public class TestInspector : MonoBehaviour {
}
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(TestInspector))]
public class TestInspectorEditor : Editor {
private bool bCheckBox0;
private bool bCheckBox1;
public override void OnInspectorGUI () {
GUILayout.BeginHorizontal();
bCheckBox0 = GUILayout.Toggle(bCheckBox0, "CheckBox0");
bCheckBox1 = GUILayout.Toggle(bCheckBox1, "CheckBox1");
GUILayout.EndHorizontal();
}
}