选择对象——EditorGUIUtility.ShowObjectPicker()

作者:追风剑情 发布于:2017-12-25 21:45 分类:Unity3d

TestInspector.cs


using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(Test))]
public class TestInspector : Editor {

    Test test;
    bool toggleGroup;

    void OnEnable()
    {
        test = target as Test;
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        serializedObject.Update();
        EditorGUI.BeginChangeCheck();

        string commandName = Event.current.commandName;

        toggleGroup = EditorGUILayout.BeginToggleGroup("Group", toggleGroup);
        if (toggleGroup)
        {
            toggleGroup = false;
            //为控件获取一个唯一ID
            int controlID = EditorGUIUtility.GetControlID(FocusType.Passive);
            //显示对象选择窗口
            EditorGUIUtility.ShowObjectPicker<GameObject>(null, true, "", controlID);
            GameObject selectObj = EditorGUIUtility.GetObjectPickerObject() as GameObject;
            Debug.Log(selectObj);
        }
        EditorGUILayout.EndToggleGroup();

        if (commandName == "ObjectSelectorUpdated")
        {
            Debug.Log(commandName);
        }
        else if (commandName == "ObjectSelectorClosed")
        {
            Debug.Log(commandName);
        }

        if (EditorGUI.EndChangeCheck())
            serializedObject.ApplyModifiedProperties();
    }
}


效果

1111.png

2222.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号