当前位置:   article > 正文

自定义UI界面inspector面板(Editor)以及自定义参数在inspector面板内显示效果(PropertyDrawer)_unity自定义inspector面板list

unity自定义inspector面板list

平时我们在搭建一个界面后都会给这个界面添加一个界面脚本,一般情况下都是普通的MonoBehaviour的。

此时脚本内定义的参数可以通过public或者特性[SerializeField]显示在inspector面板内。

但有时我们可能希望在面板上加一个按钮,然后能在编辑器模式下点击这个按钮就会执行脚本内的某个方法,此时就需要自定义这个面板。

代码如下示例:

普通界面脚本:

该脚本内有两个列表:templateDatas存储了实例的数据,templates存储了创建的实例对象

以及一些初始化的方法

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /// <summary>
  6. ////西部概况界面
  7. /// </summary>
  8. public class GeneralSituation : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 示例物体
  12. /// </summary>
  13. private Template template;
  14. /// <summary>
  15. /// 显示区域
  16. /// </summary>
  17. private Transform content;
  18. /// <summary>
  19. /// 实例列表
  20. /// </summary>
  21. private List<Template> templates = new List<Template>();
  22. /// <summary>
  23. /// 实例数据列表
  24. /// </summary>
  25. [SerializeField]
  26. private List<TemplateData> templateDatas = new List<TemplateData>();
  27. /// <summary>
  28. /// 初始化实例
  29. /// </summary>
  30. public void InitTemplates()
  31. {
  32. }
  33. /// <summary>
  34. /// 初始化
  35. /// </summary>
  36. public void Init()
  37. {
  38. if(template == null)
  39. {
  40. template = this.transform.Find("Template").GetComponent<Template>();
  41. }
  42. if(content == null)
  43. {
  44. content = this.transform.Find("显示列表/Viewport/Content");
  45. }
  46. }
  47. }

Template与TemplateData数据脚本:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. /// <summary>
  7. /// 概况界面实例
  8. /// </summary>
  9. public class Template : MonoBehaviour
  10. {
  11. /// <summary>
  12. /// 标签
  13. /// </summary>
  14. [SerializeField]
  15. private Text title;
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. [SerializeField]
  20. private Text value;
  21. /// <summary>
  22. /// 字体颜色
  23. /// </summary>
  24. [SerializeField]
  25. private Color color;
  26. /// <summary>
  27. /// 设置标签内容
  28. /// </summary>
  29. /// <param name="title"></param>
  30. public void SetTitle(string title)
  31. {
  32. this.title.text = title;
  33. this.title.color = color;
  34. }
  35. /// <summary>
  36. /// 设置值
  37. /// </summary>
  38. /// <param name="value"></param>
  39. public void SetValue(string value)
  40. {
  41. this.value.text = value;
  42. this.value.color = color;
  43. }
  44. /// <summary>
  45. /// 设置颜色
  46. /// </summary>
  47. /// <param name="color"></param>
  48. public void SetColor(Color color)
  49. {
  50. this.color = color;
  51. this.title.color = color;
  52. this.value.color = color;
  53. }
  54. /// <summary>
  55. /// 获取标签内容
  56. /// </summary>
  57. /// <returns></returns>
  58. public string GetTitle()
  59. {
  60. return this.title.text;
  61. }
  62. }
  63. /// <summary>
  64. /// 实例数据
  65. /// </summary>
  66. [Serializable]
  67. public class TemplateData
  68. {
  69. [SerializeField]
  70. public string title;
  71. [SerializeField]
  72. public string value;
  73. [SerializeField]
  74. public Color color;
  75. public TemplateData(string title, string value, Color color)
  76. {
  77. this.title = title;
  78. this.value = value;
  79. this.color = color;
  80. }
  81. }

界面编辑器脚本:

该脚本继承Editor,加上特性[CustomEditor(typeof(具体界面类))],然后实现OnInspectorGUI()方法即可自定义inspector显示面板,效果图如下

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. /// <summary>
  6. ////西部概况界面编辑器
  7. /// </summary>
  8. [CustomEditor(typeof(GeneralSituation))]
  9. public class GeneralSituationEditor : Editor
  10. {
  11. GeneralSituation ui;
  12. GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(100f) };
  13. public override void OnInspectorGUI()
  14. {
  15. base.OnInspectorGUI();
  16. ui = (GeneralSituation)this.target;
  17. ui.Init();
  18. GUILayout.BeginVertical();
  19. //若设置了参数的显示效果,此处可注释掉
  20. for (int i = 0; i < ui.templateDatas.Count; i++)
  21. {
  22. GUILayout.BeginHorizontal();
  23. EditorGUILayout.LabelField("标题:", new GUILayoutOption[] { GUILayout.Width(30f) });
  24. ui.templateDatas[i].title = EditorGUILayout.TextField(ui.templateDatas[i].title, options);
  25. EditorGUILayout.LabelField("值:", new GUILayoutOption[] { GUILayout.Width(30f) });
  26. ui.templateDatas[i].value = EditorGUILayout.TextField(ui.templateDatas[i].value, options);
  27. EditorGUILayout.LabelField("颜色:", new GUILayoutOption[] { GUILayout.Width(30f) });
  28. ui.templateDatas[i].color = EditorGUILayout.ColorField(ui.templateDatas[i].color, options);
  29. if (GUILayout.Button("-", new GUILayoutOption[] { GUILayout.Width(20f) }))
  30. {
  31. ui.templateDatas.Remove(ui.templateDatas[i]);
  32. }
  33. GUILayout.EndHorizontal();
  34. }
  35. if (GUILayout.Button("+", new GUILayoutOption[] { GUILayout.Width(20f) }))
  36. {
  37. ui.templateDatas.Add(new TemplateData("", "", Color.white));
  38. }
  39. //可注释
  40. if (GUILayout.Button("确定", new GUILayoutOption[] { GUILayout.Width(100f) }))
  41. {
  42. ui.InitTemplates();
  43. }
  44. GUILayout.EndVertical();
  45. }
  46. }

效果图:

 若要修改参数在inspector面板显示,需要新建一个脚本继承PropertyDrawer,然后添加特性[CustomPropertyDrawer(typeof(参数类名))],然后实现OnGUI()方法,示例如下

TemplateDataDrawer:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. /// <summary>
  6. /// 实例数据编辑器
  7. /// </summary>
  8. [CustomPropertyDrawer(typeof(TemplateData))]
  9. public class TemplateDataDrawer : PropertyDrawer
  10. {
  11. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  12. {
  13. EditorGUI.BeginProperty(position, label, property);
  14. //FocusType.Passive 使用Tab键切换时不会被选中,FocusType.Keyboard 使用Tab键切换时会被选中
  15. position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  16. //不让indentLevel层级影响到同一行的绘制,因为PropertyDrawer在很多地方都有可能被用到,可能出现嵌套使用
  17. var indent = EditorGUI.indentLevel;
  18. EditorGUI.indentLevel = 0;
  19. //定义各数据显示区域位置与大小
  20. var titleRect = new Rect(position.x, position.y,100, position.height);
  21. var valueRect = new Rect(position.x + 105, position.y, 50, position.height);
  22. var colorRect = new Rect(position.x + 160, position.y, 50, position.height);
  23. //设置参数对应位置与大小
  24. EditorGUI.PropertyField(titleRect, property.FindPropertyRelative("title"), GUIContent.none);
  25. EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("value"), GUIContent.none);
  26. EditorGUI.PropertyField(colorRect, property.FindPropertyRelative("color"), GUIContent.none);
  27. EditorGUI.indentLevel = indent;
  28. EditorGUI.EndProperty();
  29. }
  30. }

效果如图:

两者结合即可实现inspector面板的自定义效果 ,当然也可以分开使用

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/113266
推荐阅读
相关标签
  

闽ICP备14008679号