如何在Unity检查器中根据其他变量值隐藏变量 [英] How to hide variables depending on other variables values In Unity inspector
问题描述
我如何根据统一检查器中的其他变量值隐藏变量.基本上可以想象一下:如果我有一个名为"CanSprint"的布尔型变量和一个名为"SprintSpeed"的浮点型变量,那么我要创建一个这样的变量,以便当布尔型变量为true时,浮点数显示出来,但是当布尔型变量为false时,浮点数隐藏起来.这只是有点整洁. 谢谢!
how do i hide variables depending on other variables values in the unity inspector. Basically imagine this: if i had a bool called "CanSprint" and a float "SprintSpeed" so i want to make it so that when the bool is true, the float is showing, but when the bool is false, the float hides. This is just to be a little bit neater. Thanks!
推荐答案
您需要研究自定义编辑器脚本( https://docs.unity3d.com/Manual/editor-CustomEditors.html ),使用自定义编辑器脚本可以随时显示变量.这是使用来自链接的信息的布局:
You need to look into custom editor scripts (https://docs.unity3d.com/Manual/editor-CustomEditors.html), using a custom editor script you can show variables whenever you like. Here's a layout using the information from the links:
[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"更改为您的类类型,将"someFloat"更改为您的float,将"myBool"更改为您的布尔变量.
Be sure to stick this script in the 'Editor' folder, change 'MyBehaviourClass' to your class type, change 'someFloat' to your float and 'myBool' to your boolean variable.
这篇关于如何在Unity检查器中根据其他变量值隐藏变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!