十一、Unity编辑器开发之Toggle | 您所在的位置:网站首页 › togglegroup › 十一、Unity编辑器开发之Toggle |
当我们需要切换一个bool值的时候通常需要用到Toggle开关,Toggle的绘制需要用到GUILayout.Toggle或EditorGUILayout.Toggle方法。 using UnityEngine; using UnityEditor; [CustomEditor(typeof(Test))] public class TestEditor : Editor { private bool boolValue; public override void OnInspectorGUI() { GUILayout.Toggle(boolValue, "开关"); } }上述代码表示绘制一个Toggle来表示boolValue的值。 如果需要点击Toggle开关时来切换boolValue的值,需要boolValue接收GUILayout的返回值: using UnityEngine; using UnityEditor; [CustomEditor(typeof(Test))] public class TestEditor : Editor { private bool boolValue; public override void OnInspectorGUI() { boolValue = GUILayout.Toggle(boolValue, "开关"); } }通常我们会使用一个新的bool值来接收返回值,假如值不一样时才进行赋值: using UnityEngine; using UnityEditor; [CustomEditor(typeof(Test))] public class TestEditor : Editor { private bool boolValue; public override void OnInspectorGUI() { bool newValue = GUILayout.Toggle(boolValue, "开关"); if (newValue != boolValue) { boolValue = newValue; } } }这样可以在赋值之前使用Undo类来记录我们的操作,当我们希望撤销操作时可以使用Ctrl+Z来撤销操作,当然Undo类的使用不在此进行介绍。 下面是GUILayout绘制Toggle的所有方法、重载: public static bool Toggle (bool value, GUIContent content, GUIStyle style, params GUILayoutOption[] options); public static bool Toggle (bool value, string text, GUIStyle style, params GUILayoutOption[] options); public static bool Toggle (bool value, Texture image, GUIStyle style, params GUILayoutOption[] options); public static bool Toggle (bool value, GUIContent content, params GUILayoutOption[] options); public static bool Toggle (bool value, string text, params GUILayoutOption[] options); public static bool Toggle (bool value, Texture image, params GUILayoutOption[] options);下面是EditorGUILayout绘制Toggle的所有方法、重载: public static bool Toggle (GUIContent label, bool value, GUIStyle style, params GUILayoutOption[] options); public static bool Toggle (string label, bool value, GUIStyle style, params GUILayoutOption[] options); public static bool Toggle (bool value, GUIStyle style, params GUILayoutOption[] options); public static bool Toggle (GUIContent label, bool value, params GUILayoutOption[] options); public static bool Toggle (string label, bool value, params GUILayoutOption[] options); public static bool Toggle (bool value, params GUILayoutOption[] options); public static bool ToggleLeft (GUIContent label, bool value, GUIStyle labelStyle, params GUILayoutOption[] options); public static bool ToggleLeft (string label, bool value, GUIStyle labelStyle, params GUILayoutOption[] options); public static bool ToggleLeft (GUIContent label, bool value, params GUILayoutOption[] options); public static bool ToggleLeft (string label, bool value, params GUILayoutOption[] options); |
CopyRight 2018-2019 实验室设备网 版权所有 |