【问题标题】:Unity Editor Option Popup? ( Unity Editor update Option )Unity 编辑器选项弹出窗口? (Unity Editor 更新选项)
【发布时间】:2020-01-05 08:10:02
【问题描述】:

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

!!滑块!! 如果选择了滑块,我希望在编辑器中提供以下选项。 (最小值、最大值、默认值)

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

z.b:

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

问题是,我如何设计更多的选项并将它们带走? 统一版。 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;

}

【问题讨论】:

  • 问题是什么?另外,请将图片上传到问题中,而不是提供链接。
  • 问题是,我如何设计更多的选项并将它们带走?
  • 所以你想实现好图像中显示的内容正确吗?那是经过编辑的图像吗

标签: c# unity3d unity-editor


【解决方案1】:

您必须为您的类型提供自己的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 枚举值。

【讨论】:

  • 请注意,在OnInspectorGUI的开头,您还应该调用serializedObject.Update();并删除if (GUI.changed)检查
  • 您也可以省略new GUIContent(...),因为PropertyField 将自动使用相应的字段名称。如果您想显示自定义的不同文本,您只需传入new GUIContent
  • @derHugo 感谢您的提示,GUIContentUpdate() 听起来很合理。我基于NVidia Flex 资产添加了if (GUI.changed),其中属性的设置器在更改时确实调用了繁重的C 函数。我认为使用普通的二传手应该不会造成伤害。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
相关资源
最近更新 更多