当前位置:   article > 正文

Unity 改造编辑器组件字段显示

Unity 改造编辑器组件字段显示

关于Unity中组件选择枚举的不同,其他属性跟着变的功能一直没有写,今天补上,

首先定义一个MaskScroll滚动的组件,这个组件支持水平和竖直方向上的滚动,还有加速减速滚动的功能

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public enum EMaskScrollStatus {
  5. NONE,
  6. /// <summary>
  7. /// 停止
  8. /// </summary>
  9. STOP,
  10. /// <summary>
  11. /// 加速滚动
  12. /// </summary>
  13. SCROLLING_ACCE,
  14. /// <summary>
  15. /// 减速滚动
  16. /// </summary>
  17. SCROLLING_DESC
  18. }
  19. public enum EMaskScrollDirection {
  20. /// <summary>
  21. /// 水平方向
  22. /// </summary>
  23. Horizontal,
  24. /// <summary>
  25. /// 竖直方向
  26. /// </summary>
  27. Vertical
  28. }
  29. public enum EMaskScrollUpDown {
  30. UP,
  31. DOWN
  32. }
  33. public enum EMaskScrollLeftRight {
  34. LEFT,
  35. RIGHT
  36. }
  37. public class MaskScroll : MonoBehaviour
  38. {
  39. [Header("滚动速度相关")]
  40. [Tooltip("加速到多大速度")]
  41. [SerializeField]
  42. private float acceSpeedMax = 10f;
  43. [Tooltip("减速到多小")]
  44. [SerializeField]
  45. private float descSpeedMin = 0f;
  46. [Space(5)]
  47. [Tooltip("滚动轴")]
  48. public EMaskScrollDirection scrollDir = EMaskScrollDirection.Vertical;
  49. [HideInInspector]
  50. public EMaskScrollUpDown scrollVertical = EMaskScrollUpDown.DOWN;
  51. [HideInInspector]
  52. public EMaskScrollLeftRight scrollHorizontal = EMaskScrollLeftRight.RIGHT;
  53. [SerializeField]
  54. [Tooltip("滚动缓冲区")]
  55. private float scrollBuffer = 30f;
  56. private EMaskScrollStatus scrollStatus = EMaskScrollStatus.NONE;
  57. private RectTransform contentsTransform = null;
  58. /// <summary>
  59. /// 滚动到哪一个元素
  60. /// </summary>
  61. private int targetScrollIndex = 0;
  62. public EMaskScrollStatus ScrollStatus {
  63. set {
  64. scrollStatus = value;
  65. }
  66. get {
  67. return scrollStatus;
  68. }
  69. }
  70. private void Awake() {
  71. contentsTransform = transform.Find("avatars") as RectTransform;
  72. }
  73. // Start is called before the first frame update
  74. void Start()
  75. {
  76. }
  77. public void StartScroll() {
  78. // 开始加速滚动
  79. scrollStatus = EMaskScrollStatus.SCROLLING_ACCE;
  80. targetScrollIndex = Random.Range(0,contentsTransform.childCount);
  81. }
  82. // Update is called once per frame
  83. void Update()
  84. {
  85. if(scrollStatus == EMaskScrollStatus.SCROLLING_ACCE || scrollStatus == EMaskScrollStatus.SCROLLING_DESC) {
  86. if(scrollStatus == EMaskScrollStatus.SCROLLING_ACCE) {
  87. } else {
  88. }
  89. }
  90. }
  91. }

写到这当时我就想能根据我选择的滚动轴动态去显示滚动方向呢,于是就有了以下的答案:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. [CustomEditor(typeof(MaskScroll))]
  5. public class MaskScrollEditor : Editor
  6. {
  7. public override void OnInspectorGUI()
  8. {
  9. DrawDefaultInspector();
  10. MaskScroll maskScroll = (MaskScroll)target;
  11. if(maskScroll.scrollDir == EMaskScrollDirection.Horizontal) {
  12. maskScroll.scrollHorizontal = (EMaskScrollLeftRight)EditorGUILayout.EnumPopup("滚动方向",maskScroll.scrollHorizontal);
  13. } else if (maskScroll.scrollDir == EMaskScrollDirection.Vertical)
  14. {
  15. maskScroll.scrollVertical = (EMaskScrollUpDown)EditorGUILayout.EnumPopup("滚动方向", maskScroll.scrollVertical);
  16. }
  17. }
  18. }

另外注意不要在该代码里面加入 base.OnInspectorGUI()否则组件会加入 

到编辑器两次的 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/806497
推荐阅读
相关标签
  

闽ICP备14008679号