通过枚举类型自定义检查器喜欢 [英] Customizing inspector by Enum type likes

查看:146
本文介绍了通过枚举类型自定义检查器喜欢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个平台类,其中包含几种移动类型. TypeA在航点之间初始化时为AutoMove 当特定布尔值为true时,TypeB在重力作用下下落. 我通过选择自己的Enum类型来控制这些不同的行为.

There's a platform class that contains several movement types. TypeA is AutoMove at initiation between waypoints TypeB is Falling with gravity when a specific boolean is true. I'm controlling these different behaviors via selecting its own Enum type.

我现在想做的是只能在检查器上看到与运动类型匹配的变量,因为TypeA使用的路标系统完全被TypeB忽略,而检查器上所有未使用的变量只会使事情变得混乱和混乱.

What I'm trying to do now is to be able to only see those variables on inspector matching the movement types since TypeA uses waypoint system which TypeB completely ignores and all the unused variables remaining on inspector just gets things messy and chaotic.

我可以一次让他们自己上课,但是如果我坚持并尝试这样做:可以做到吗?我该怎么办?

I could just make them their own class at a time, but if I insist and try to do this: can this be done? How can I do it?

还要问的另一件事是,是否存在诸如mine这样的类,它在性能效率方面更胜一筹?要在自己的脚本上上课吗?还是只是不理会性能?如果没有明显的性能差异,我打算照常这样做.

Another thing to ask is if there's a class such as mine which is better in performance efficiency-wise? To have classes on their own script? Or that just don't bother with performance? If there's no significant performance difference I intend to do this as I was doing.

添加了带有指示的已签发检查员. 红色是枚举,用于选择要遵循的运动功能 1.是遵循航点系统和漫游的移动类型之一 2.是与PC接触跌落的动作类型之一 3.是仅在powerboolean为true时运动的运动类型之一.

EDIT : Added the issued inspector with indication. Red is the Enum selecting which movement function to follow 1. is one of the movement type that follows the waypoint system and roam 2. is one of the movement type that falls down on contact with PC 3. is one of the movement type that moves only if powered boolean is true

推荐答案

您将需要实现自己的Custom Inspector.关于此主题,有 Unity的官方教程.定制检查器使用GUI类,并且只有在满足条件的情况下,您才需要检查条件并绘制一个字段.

You will need to implement your own Custom Inspector. There is Unity's official tutorial on this topic. The custom inspector uses GUI classes, and you will need to check your condition and draw a field if and only if the condition is met.

这是我从根据枚举更改检查器变量.通过这段代码,您将学习如何从目标类中动态获取enum字段的值.

Here's an example code that I took from Change Inspector Variables Depending On Enum. From this code you will learn how to dynamically get a value of a enum field from your target class.

~/Assets/PropertyHolder.cs

using System.Collections;
using UnityEngine;


public class PropertyHolder : MonoBehaviour
{
    public enum Status { A, B, C };

    public Status state;

    public int valForAB;

    public int valForA;
    public int valForC;

    public bool controllable;

    void Start()
    {

    }

    void Update()
    {

    }
}

~/Assets/Editor/PropertyHolderEditor.cs

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof (PropertyHolder)), CanEditMultipleObjects]
public class PropertyHolderEditor : Editor
{

    public SerializedProperty
    state_Prop,
    valForAB_Prop,
    valForA_Prop,
    valForC_Prop,
    controllable_Prop;

    void OnEnable()
    {
        // Setup the SerializedProperties
        state_Prop = serializedObject.FindProperty("state");
        valForAB_Prop = serializedObject.FindProperty("valForAB");
        valForA_Prop = serializedObject.FindProperty("valForA");
        valForC_Prop = serializedObject.FindProperty("valForC");
        controllable_Prop = serializedObject.FindProperty("controllable");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        EditorGUILayout.PropertyField(state_Prop);

        PropertyHolder.Status st = (PropertyHolder.Status) state_Prop.enumValueIndex;

        switch (st)
        {
            case PropertyHolder.Status.A:
                EditorGUILayout.PropertyField(controllable_Prop, new GUIContent("controllable"));
                EditorGUILayout.IntSlider(valForA_Prop, 0, 10, new GUIContent("valForA"));
                EditorGUILayout.IntSlider(valForAB_Prop, 0, 100, new GUIContent("valForAB"));
                break;

            case PropertyHolder.Status.B:
                EditorGUILayout.PropertyField(controllable_Prop, new GUIContent("controllable"));
                EditorGUILayout.IntSlider(valForAB_Prop, 0, 100, new GUIContent("valForAB"));
                break;

            case PropertyHolder.Status.C:
                EditorGUILayout.PropertyField(controllable_Prop, new GUIContent("controllable"));
                EditorGUILayout.IntSlider(valForC_Prop, 0, 100, new GUIContent("valForC"));
                break;

        }

        serializedObject.ApplyModifiedProperties();
    }
}

这是检查bool&的方法仅在满足条件的情况下进行绘制,来自隐藏/显示属性在检查器中动态显示.修改布尔值检查部分,以比较使用上述代码找到的enum值.

And this is how to check bool & draw only if the condition is met, from Hide/Show properties dynamically in inspector. Modify the boolean checking part to compare the enum value you found using the above code.

public class MyScript : MonoBehaviour
{
    public bool flag;
    public int i = 1;
}

[CustomEditor(typeof (MyScript))]
public class MyScriptEditor : Editor
{
    void OnInspectorGUI()
    {
        var myScript = target as MyScript;

        myScript.flag = GUILayout.Toggle(myScript.flag, "Flag");

        if (myScript.flag)
            myScript.i = EditorGUILayout.IntSlider("I field:", myScript.i, 1, 100);

    }
}

将这两者结合起来,应该会得到想要的行为.

Combining these two you should get the behaviour you want.

这篇关于通过枚举类型自定义检查器喜欢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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