Home > Software design >  How to hide a variable depending on the value of another variable without MonoBehaviour In Unity Ins
How to hide a variable depending on the value of another variable without MonoBehaviour In Unity Ins

Time:10-14

I have a class that I would like to hide the float "segmentEnd" depending on the value of the bool "isAStillFrame".

The reason I can not use MonoBehaviour is because I need the class "VideoSegment" to be in a serialized list in the script the VideoSegment is in. Is this possible? Right now changing the value of isAStillFrame does nothing.

Class to Modify

code

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

code

Editor Script that is in the Editor Folder

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

CodePudding user response:

If you do not inherit from MonoBehaviour or ScriptableObject (or in general from UnityEngine.Object) then a custom enter image description here

  • Related