Unity Editor扩展

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

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity Editor扩展是指通过编写代码来扩展Unity编辑器的功能。通过创建自定义的编辑器窗口、工具栏按钮、菜单项和面板等,开发者可以为自己的项目添加一些定制化功能,以提高开发效率和用户体验。 Unity提供了一套API来实现编辑器扩展,开发者可以利用这些API去创建自定义的编辑器界面。首先,我们需要创建一个继承自EditorWindow或Editor类的脚本,然后在这个脚本中实现我们想要的功能。比如,我们可以在自定义的编辑器窗口中创建一些GUI元素,如按钮、文本框、下拉菜单等,用于控制场景中的对象、调整参数或执行特定的操作。 另外,Unity还提供了一些常用的工具类,如SerializedObject、SerializedProperty等,以便开发者可以访问和修改Unity对象的属性。使用这些工具类,我们可以在编辑器扩展中实现对象的序列化、反序列化和检查等功能。 在开发过程中,Unity的编辑器扩展还可以与自定义的脚本进行交互。通过注册自定义的菜单项、工具栏按钮或快捷键,我们可以在编辑器中快速调用脚本的功能,并在自定义界面中显示脚本的运行结果。 总结来说,Unity Editor扩展是一种强大的工具,它可以帮助开发者提高开发效率和用户体验。通过编写代码,我们可以创建出各种各样的自定义编辑器界面,以满足不同项目的需求。无论是增加交互性、优化工作流程还是增加特定的功能,Unity的编辑器扩展都能提供灵活和强大的解决方案。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值