[转载]Unity根据条件控制Inspector面板中的属性显示

本文转载自:https://blog.csdn.net/u010133610/article/details/55670800

很多情况下,一个组件可调整的属性比较多,但是属性之间又有一定的联系,最简单的,当属性type是类型typeA时,属性width和height才会起作用。

这时候,为了界面的整洁和直观,如果type的属性不是typeA,那我们就没必要暴露出width和height属性。


默认的Inspector不能满足这个需求,这就需要用Editor的OnInspectorGUI方法重新绘制显示面板。


创建一个脚本类TestA.cs,并包含一个枚举类型TestType


    
    
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public enum TestType
  5. {
  6. typeA,
  7. typeB,
  8. }
  9. public class TestA : MonoBehaviour {
  10. public TestType type;
  11. public int width;
  12. public int height;
  13. // Use this for initialization
  14. void Start () {
  15. }
  16. }

然后在Editor文件夹下创建一个TestInspector.cs,继承自Editor


    
    
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. [ CustomEditor(typeof(TestA))]
  6. public class TestInspector : Editor
  7. {
  8. private SerializedObject obj;
  9. private TestA testA;
  10. private SerializedProperty type;
  11. private SerializedProperty width;
  12. private SerializedProperty height;
  13. void OnEnable()
  14. {
  15. obj = new SerializedObject(target);
  16. width = obj.FindProperty( "width");
  17. height = obj.FindProperty( "height");
  18. }
  19. public override void OnInspectorGUI()
  20. {
  21. testA = (TestA)target;
  22. testA.type = (TestType)EditorGUILayout.EnumPopup( "Type-", testA.type);
  23. if (testA.type == TestType.typeA)
  24. {
  25. EditorGUILayout.PropertyField(width);
  26. EditorGUILayout.PropertyField(height);
  27. }
  28. }
  29. }

注意引用UintyEditor和文件路径,只要上层路径的名字是Editor就可以,不比非得在根目录的Editor里。


代码比较简单,一目了然,就不讲解了。下面是运行结果:

扫描二维码关注公众号,回复: 2365757 查看本文章



            </div>

很多情况下,一个组件可调整的属性比较多,但是属性之间又有一定的联系,最简单的,当属性type是类型typeA时,属性width和height才会起作用。

这时候,为了界面的整洁和直观,如果type的属性不是typeA,那我们就没必要暴露出width和height属性。


默认的Inspector不能满足这个需求,这就需要用Editor的OnInspectorGUI方法重新绘制显示面板。


创建一个脚本类TestA.cs,并包含一个枚举类型TestType


  
  
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public enum TestType
  5. {
  6. typeA,
  7. typeB,
  8. }
  9. public class TestA : MonoBehaviour {
  10. public TestType type;
  11. public int width;
  12. public int height;
  13. // Use this for initialization
  14. void Start () {
  15. }
  16. }

然后在Editor文件夹下创建一个TestInspector.cs,继承自Editor


  
  
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. [ CustomEditor(typeof(TestA))]
  6. public class TestInspector : Editor
  7. {
  8. private SerializedObject obj;
  9. private TestA testA;
  10. private SerializedProperty type;
  11. private SerializedProperty width;
  12. private SerializedProperty height;
  13. void OnEnable()
  14. {
  15. obj = new SerializedObject(target);
  16. width = obj.FindProperty( "width");
  17. height = obj.FindProperty( "height");
  18. }
  19. public override void OnInspectorGUI()
  20. {
  21. testA = (TestA)target;
  22. testA.type = (TestType)EditorGUILayout.EnumPopup( "Type-", testA.type);
  23. if (testA.type == TestType.typeA)
  24. {
  25. EditorGUILayout.PropertyField(width);
  26. EditorGUILayout.PropertyField(height);
  27. }
  28. }
  29. }

注意引用UintyEditor和文件路径,只要上层路径的名字是Editor就可以,不比非得在根目录的Editor里。


代码比较简单,一目了然,就不讲解了。下面是运行结果:



            </div>

猜你喜欢

转载自blog.csdn.net/qq_16440237/article/details/81179011