如何在unity检查器中隐藏依赖于其他变量值的变量。基本上想象一下:如果我有一个名为"CanSprint“的布尔值和一个浮点数"SprintSpeed”,所以我想让它成为这样,当布尔值为真时,浮点数就会显示出来,但当布尔值为假时,浮点数就会隐藏。这只是为了更整洁一点。谢谢!
发布于 2017-11-22 17:30:04
您需要查看自定义编辑器脚本(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);
}
}
}确保将此脚本放在“编辑器”文件夹中,将“MyBehaviourClass”更改为您的类类型,将“someFloat”更改为浮点型,将“myBool”更改为布尔变量。
发布于 2022-01-07 06:47:17
对HideIf(布尔名、布尔值、要隐藏的对象)使用SetBools方法
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;}
}
}https://stackoverflow.com/questions/47430785
复制相似问题