自定义窗口上的Unity自定义检查器 [英] Unity Custom Inspector on Custom Window

查看:163
本文介绍了自定义窗口上的Unity自定义检查器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义窗口,显示对象列表.每个对象都有一个自定义的检查器编辑器.

I have a custom window that show a list of objects. Each of this objects has a custom inspector editor.

是否可以在自定义窗口中显示自定义检查器?

Is possible to show custom inspector inside the custom window?

推荐答案

您不能强制Unity3D在检查器窗口之外的其他位置绘制自定义检查器.

You can't force Unity3D to draw your custom inspector somewhere else than inspector window.

此外,您可以使用 Editor.CreateEditor Editor设置为静态>方法. 由于要显示自定义检查器,因此应该可以从Window.OnGUI方法内部手动实例化它,并使用编辑器的公共OnInspectorGUI方法在窗口内绘制该编辑器.

Btw you can manually instatiate an Editor using Editor.CreateEditor method. Since you are displaying a custom inspector, than it should be possible to instantiate it manually from inside Window.OnGUI method, and use the public OnInspectorGUI method of the editor to draw the editor inside your window.

例如,如果您已将名为CustomScript的脚本附加到GameObject并具有名为CustomScriptEditor的相关Editor,并且假设您已从层次结构中选择了GameObject,则此代码将可视化自定义EditorWindow中的检查员:

For example, if you have attached a script called CustomScript to a GameObject and have a related Editor called CustomScriptEditor, supposing you have selected the GameObject from the hierarchy, this code visualize the custom inspector inside an EditorWindow:

using UnityEditor;
using UnityEngine;


public class TestWindow : EditorWindow
{

    [MenuItem ("Window/Editor Window Test")]
    static void Init () 
    {
        // Get existing open window or if none, make a new one:
        TestWindow window = (TestWindow)EditorWindow.GetWindow (typeof (TestWindow));
    }

    void OnGUI () {

        GameObject sel = Selection.activeGameObject;

        CustomScript targetComp = sel.GetComponent<CustomScript>();

        if (targetComp != null)
        {
            var editor = Editor.CreateEditor(targetComp);
            editor.OnInspectorGUI();            
        }

    }
}

这篇关于自定义窗口上的Unity自定义检查器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆