【问题标题】:How to hide a variable depending on the value of another variable without MonoBehaviour In Unity Inspector如何在 Unity Inspector 中不使用 MonoBehaviour 的情况下根据另一个变量的值隐藏变量
【发布时间】:2022-10-14 15:19:55
【问题描述】:

我有一个类,我想根据布尔“isAStillFrame”的值隐藏浮动“segmentEnd”。

我不能使用 MonoBehaviour 的原因是因为我需要将类“VideoSegment”放在 VideoSegment 所在脚本的序列化列表中。这可能吗?现在改变 isAStillFrame 的值什么也没做。

要修改的类

code

public class VideoSegment
{
    public string segmentName;
    public bool isAStillFrame;
    public float segmentStart;
    public float segmentEnd;
    public List<VideoSegmentCue> segmentCues;
    public VideoSegmentAction endAction;
}

code

编辑器文件夹中的编辑器脚本

code

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

    [CustomEditor(typeof(VideoSegment)), CanEditMultipleObjects]
    public class SegmentedVideoPlayerEditor : Editor
    {
        private List<boolStruct> structList;

        public void OnEnable()
        {
            structList = new List<boolStruct>();
            SetBools();
        }

        private void SetBools()
        {
            HideIf("isAStillFrame", true, "segmentEnd");
        }

        private void HideIf(string boolName, bool boolValue, string fieldName)
        {
            boolStruct _boolStruct = new boolStruct()
            {
                targetBoolName = boolName,
                targetBoolValue = boolValue,
                targetVarName = fieldName,
            };
            structList.Add(_boolStruct);
        }
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            var obj = serializedObject.GetIterator();
            if (obj.NextVisible(true))
            {
                do
                {
                    bool visible = true;
                    foreach (var i in structList)
                    {
                        if (i.targetVarName == obj.name)
                        {
                            FieldInfo boolName = target.GetType().GetField(i.targetBoolName);
                            var boolValue = boolName.GetValue(target);
                            if (boolValue.ToString() != i.targetBoolValue.ToString())
                                visible = false;
                            else
                            {
                                visible = true;
                                break;
                            }
                        }
                    }
                    if (visible)
                        EditorGUILayout.PropertyField(obj, true);
                }
                while (obj.NextVisible(false));
            }
            serializedObject.ApplyModifiedProperties();
        }
        private struct boolStruct
        {
            public string targetBoolName { get; set; }
            public bool targetBoolValue { get; set; }
            public string targetVarName { get; set; }
        }
    }

code

【问题讨论】:

    标签: c# unity3d inspector


    【解决方案1】:

    如果您不是从MonoBehaviour 或ScriptableObject(或通常从UnityEngine.Object)继承,那么自定义Editor 是错误的工具。

    Editor 用于实现整个督察对于某个可选择的资产类型。

    您更愿意使用的是自定义PropertyDrawer,它可能看起来像例如

    // First of all your class needs to be serializable
    // otherwise it won't show up in the Inspector at all
    [Serializable]
    public class VideoSegment
    {
        public string segmentName;
        public bool isAStillFrame;
        public float segmentStart;
        public float segmentEnd;
        public List<VideoSegmentCue> segmentCues;
        public VideoSegmentAction endAction;
    }
    

    接着

    [CustomPropertyDrawer(typeof(VideoSegment))]
    public class VideoSegmentDrawer : PropertyDrawer
    {
        // First you have to define how much vertical space your custom drawer should reserve 
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            // I simply assume you want to fold your type so in this case
            // it would be only the foldout label
            if (!property.isExpanded)
            {
                return EditorGUIUtility.singleLineHeight;
            }
    
            // link up the properties
            var segmentName = property.FindPropertyRelative("segmentName");
            var isAStillFrame = property.FindPropertyRelative("isAStillFrame");
            var segmentStart = property.FindPropertyRelative("segmentStart");
            var segmentEnd = property.FindPropertyRelative("segmentEnd");
            var segmentCues = property.FindPropertyRelative("segmentCues");
            var endAction = property.FindPropertyRelative("endAction");
    
            // assuming we later on use the default drawers for all properties
            // we an also just use the default heights for them
            return EditorGUIUtility.singleLineHeight
                + EditorGUI.GetPropertyHeight(segmentName)
                + EditorGUI.GetPropertyHeight(isAStillFrame)
                + EditorGUI.GetPropertyHeight(segmentStart)
                // if your bool is checked we want to skip this field later
                // so we also don't need the space for it
                + (isAStillFrame.boolValue ? 0 : EditorGUI.GetPropertyHeight(segmentEnd))
                + EditorGUI.GetPropertyHeight(segmentCues)
                + EditorGUI.GetPropertyHeight(endAction);
        }
    
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            // Using BeginProperty / EndProperty on the parent property means that
            // prefab override logic works on the entire property.
            EditorGUI.BeginProperty(position, label, property);
    
            // calculate rect for the foldout label
            var labelRect = position;
            labelRect.height = EditorGUIUtility.singleLineHeight;
            position.y += EditorGUIUtility.singleLineHeight;
    
            // Draw foldout label
            property.isExpanded = EditorGUI.Foldout(labelRect, property.isExpanded, label);
            
            // if expanded draw the rest
            if (property.isExpanded)
            {
                // again link up poperties
                var segmentName = property.FindPropertyRelative("segmentName");
                var isAStillFrame = property.FindPropertyRelative("isAStillFrame");
                var segmentStart = property.FindPropertyRelative("segmentStart");
                var segmentEnd = property.FindPropertyRelative("segmentEnd");
                var segmentCues = property.FindPropertyRelative("segmentCues");
                var endAction = property.FindPropertyRelative("endAction");
     
                // indentation - I was lazy to look it up so just used an arbitrary value
                position.x += 5;
                position.width -= 5;
    
                // Calculate rect and draw segmentName
                var segmentNameRect = position;
                segmentNameRect.height = EditorGUI.GetPropertyHeight(segmentName);
                position.y += segmentNameRect.height;
                EditorGUI.PropertyField(segmentNameRect, segmentName);
    
                // Calculate rect and draw isAStillFrame
                var isAStillFrameRect = position;
                isAStillFrameRect.height = EditorGUI.GetPropertyHeight(isAStillFrame);
                position.y += isAStillFrameRect.height;
                EditorGUI.PropertyField(isAStillFrameRect, isAStillFrame);
    
                // Calculate rect and draw segmentStart
                var segmentStartRect = position;
                segmentStartRect.height = EditorGUI.GetPropertyHeight(segmentStart);
                position.y += segmentStartRect.height;
                EditorGUI.PropertyField(segmentStartRect, segmentStart);
    
                // skip if your bool is set
                if (!isAStillFrame.boolValue)
                {
                    // Calculate rect and draw segmentEnd
                    var segmentEndRect = position;
                    segmentEndRect.height = EditorGUI.GetPropertyHeight(segmentEnd);
                    position.y += segmentEndRect.height;
                    EditorGUI.PropertyField(segmentEndRect, segmentEnd);
                }
    
                // Calculate rect and draw segmentCues
                var segmentCuesRect = position;
                segmentCuesRect.height = EditorGUI.GetPropertyHeight(segmentCues);
                position.y += segmentCuesRect.height;
                EditorGUI.PropertyField(segmentCuesRect, segmentCues);
    
                // Calculate rect and draw endAction
                var endActionRect = position;
                endActionRect.height = EditorGUI.GetPropertyHeight(endAction);
                position.y += endActionRect.height;
                EditorGUI.PropertyField(endActionRect, endAction);
            }
    
            EditorGUI.EndProperty();
        }
    }
    

    这看起来像

    注意:我没有你的类型 VideoSegmentCue 和 VideoSegmentAction 所以我只是使用 Vector3 进行演示;)

    public class Test : MonoBehaviour
    {
        public VideoSegment singleSegment;
    
        public List<VideoSegment> listOfSegments;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      相关资源
      最近更新 更多