如何根据其他变量值在 Unity 检查器中隐藏变量
How to hide variables depending on other variables values In Unity inspector
如何根据 unity inspector 中的其他变量值隐藏变量。基本上想象一下:如果我有一个名为 "CanSprint" 的布尔值和一个名为 "SprintSpeed" 的浮点数,那么我想这样做,以便当布尔值为真时显示浮点数,但当布尔值为假时,漂浮隐藏。这只是为了更整洁一点。
谢谢!
您需要查看自定义编辑器脚本 (https://docs.unity3d.com/Manual/editor-CustomEditors.html),使用自定义编辑器脚本您可以随时显示变量。这是使用链接信息的布局:
[CustomEditor(typeof(MyBehaviourClass))]
public class MyEditorClass : Editor
{
public override void OnInspectorGUI()
{
// If we call base the default inspector will get drawn too.
// Remove this line if you don't want that to happen.
base.OnInspectorGUI();
MyBehaviourClass myBehaviour = target as MyBehaviourClass;
target.myBool = EditorGUILayout.Toggle("myBool", target.myBool);
if (target.myBool)
{
target.someFloat = EditorGUILayout.FloatField ("Some Float:", target.someFloat);
}
}
}
确保将此脚本粘贴到 'Editor' 文件夹中,将 'MyBehaviourClass' 更改为您的 class 类型,将 'someFloat' 更改为您的浮点数和 'myBool'到你的布尔变量。
将 SetBools 方法与 HideIf(bool 名称、bool 值、要隐藏的对象)一起使用
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Reflection;
public class ExampleClass : MonoBehaviour
{
public bool exampleBoolA = true;
public float varToHideA;
public bool exampleBoolB = false;
public float varToHideB;
}
[CustomEditor(typeof(ExampleClass)), CanEditMultipleObjects]
public class ExampleEditor : Editor
{
private List<boolStruct> structList;
public void OnEnable()
{
structList = new List<boolStruct>();
SetBools();
}
private void SetBools()
{
HideIf("exampleBoolA", false, "varToHideA");
HideIf("exampleBoolB", true, "varToHideB");
}
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;}
}
}
如何根据 unity inspector 中的其他变量值隐藏变量。基本上想象一下:如果我有一个名为 "CanSprint" 的布尔值和一个名为 "SprintSpeed" 的浮点数,那么我想这样做,以便当布尔值为真时显示浮点数,但当布尔值为假时,漂浮隐藏。这只是为了更整洁一点。 谢谢!
您需要查看自定义编辑器脚本 (https://docs.unity3d.com/Manual/editor-CustomEditors.html),使用自定义编辑器脚本您可以随时显示变量。这是使用链接信息的布局:
[CustomEditor(typeof(MyBehaviourClass))]
public class MyEditorClass : Editor
{
public override void OnInspectorGUI()
{
// If we call base the default inspector will get drawn too.
// Remove this line if you don't want that to happen.
base.OnInspectorGUI();
MyBehaviourClass myBehaviour = target as MyBehaviourClass;
target.myBool = EditorGUILayout.Toggle("myBool", target.myBool);
if (target.myBool)
{
target.someFloat = EditorGUILayout.FloatField ("Some Float:", target.someFloat);
}
}
}
确保将此脚本粘贴到 'Editor' 文件夹中,将 'MyBehaviourClass' 更改为您的 class 类型,将 'someFloat' 更改为您的浮点数和 'myBool'到你的布尔变量。
将 SetBools 方法与 HideIf(bool 名称、bool 值、要隐藏的对象)一起使用
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Reflection;
public class ExampleClass : MonoBehaviour
{
public bool exampleBoolA = true;
public float varToHideA;
public bool exampleBoolB = false;
public float varToHideB;
}
[CustomEditor(typeof(ExampleClass)), CanEditMultipleObjects]
public class ExampleEditor : Editor
{
private List<boolStruct> structList;
public void OnEnable()
{
structList = new List<boolStruct>();
SetBools();
}
private void SetBools()
{
HideIf("exampleBoolA", false, "varToHideA");
HideIf("exampleBoolB", true, "varToHideB");
}
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;}
}
}