Unity编辑器选项弹出窗口? (Unity编辑器更新选项)

时间:2019-09-02 09:20:13

标签: c# unity3d unity-editor

问题是我有很多只发生一次的不同选择。 我有一个下拉菜单,其中有两个选项。 (滑块,复选框)

!!滑子!! 如果选择了滑块,我想在编辑器中提供以下选项。 (MinValue,MaxValue,defaultValue)

!!复选框! 如果选中了复选框,则我希望编辑器中具有以下选项。 (检查)

z.b:ttps://drive.google.com/file/d/1Bwf1NukaFKoc5EHji_9YG5BoADN3RJzP/view?usp=sharing

我已经尝试影响编辑器窗口。 (https://docs.unity3d.com/ScriptReference/EditorGUILayout.FadeGroupScope.html) 但是由于我只想扩展一个选项,而又不想创建一个新窗口,所以这个选项并不是很有帮助。

问题是,如何设计更多选项并将其删除? Unity版本。 2018.4.8f1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;




public class uitest : MonoBehaviour
{
    public enum TestType { Slider, CheckBox };
    public TestType test = TestType.Slider;
    //[SerializeField]
    private bool Check = false;
    //[SerializeField]
    private int MinValue = 0;
    //[SerializeField]
    private int MaxValue = 3;
    //[SerializeField]
    private int defaultValue = 2;

}

1 个答案:

答案 0 :(得分:2)

您必须为您的类型提供自己的Editor。将其作为uitesteditor.cs放置在Assets中某个名为Editor的目录下。

使用public成员(或访问者),无需太多Unity伏都教即可完成此代码。

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(uitest))]
[CanEditMultipleObjects]
public class uitesteditor : Editor
{
    static string[] customProperties = new string[] { "Check", "MinValue", "MaxValue", "defaultValue" };
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        // Draw common properties (by excluding all custom ones)
        // Could be skipped if there is none such.
        DrawPropertiesExcluding(serializedObject, customProperties);

        var uitarget = target as uitest;
        // Custom UI based on selected enum
        switch (uitarget.test)
        {
            case uitest.TestType.CheckBox:
                uitarget.Check = EditorGUILayout.Toggle("Check", uitarget.Check);
                break;
            case uitest.TestType.Slider:
                uitarget.MinValue = EditorGUILayout.IntField("Min value", uitarget.MinValue);
                uitarget.MaxValue = EditorGUILayout.IntField("Max value", uitarget.MaxValue);
                uitarget.defaultValue = EditorGUILayout.IntField("Default value", uitarget.defaultValue);
                break;
        }

        // Needed only by DrawPropertiesExcluding
        serializedObject.ApplyModifiedProperties();
    }
}

如果要使用SerializeField在私有字段上进行操作,则需要更多样板代码。我们使用SerializedProperty实例来访问private序列化的字段,因此代码看起来不太可读。

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(uitest))]
[CanEditMultipleObjects]
public class uitesteditor : Editor
{
    static string[] customProperties = new string[] { "test", "Check", "MinValue", "MaxValue", "defaultValue" };
    SerializedProperty test, check, MaxValue, MinValue, defaultValue;
    private void OnEnable()
    {
        test = serializedObject.FindProperty("test");
        check = serializedObject.FindProperty("Check");
        MinValue = serializedObject.FindProperty("MinValue");
        MaxValue = serializedObject.FindProperty("MaxValue");
        defaultValue = serializedObject.FindProperty("defaultValue");
    }
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        // Draw common properties (by excluding all custom ones)
        DrawPropertiesExcluding(serializedObject, customProperties);
                    EditorGUILayout.PropertyField(test);
        switch ((uitest.TestType)test.intValue)
        {
            case uitest.TestType.CheckBox:
                EditorGUILayout.PropertyField(check);
                break;
            case uitest.TestType.Slider:
                EditorGUILayout.PropertyField(MinValue);
                EditorGUILayout.PropertyField(MaxValue);
                EditorGUILayout.PropertyField(defaultValue);
                break;
        }

        serializedObject.ApplyModifiedProperties();
    }
}

注意:我已经添加了CanEditMultipleObjects标签,但是您需要自己决定是否需要它。渲染的检查器GUI将使用第一个选定对象的test枚举值。