Unity 改造编辑器组件字段显示
关于Unity中组件选择枚举的不同,其他属性跟着变的功能一直没有写,今天补上,
(图片来源网络,侵删)
首先定义一个MaskScroll滚动的组件,这个组件支持水平和竖直方向上的滚动,还有加速减速滚动的功能
using System.Collections; using System.Collections.Generic; using UnityEngine; public enum EMaskScrollStatus { NONE, /// /// 停止 /// STOP, /// /// 加速滚动 /// SCROLLING_ACCE, /// /// 减速滚动 /// SCROLLING_DESC } public enum EMaskScrollDirection { /// /// 水平方向 /// Horizontal, /// /// 竖直方向 /// Vertical } public enum EMaskScrollUpDown { UP, DOWN } public enum EMaskScrollLeftRight { LEFT, RIGHT } public class MaskScroll : MonoBehaviour { [Header("滚动速度相关")] [Tooltip("加速到多大速度")] [SerializeField] private float acceSpeedMax = 10f; [Tooltip("减速到多小")] [SerializeField] private float descSpeedMin = 0f; [Space(5)] [Tooltip("滚动轴")] public EMaskScrollDirection scrollDir = EMaskScrollDirection.Vertical; [HideInInspector] public EMaskScrollUpDown scrollVertical = EMaskScrollUpDown.DOWN; [HideInInspector] public EMaskScrollLeftRight scrollHorizontal = EMaskScrollLeftRight.RIGHT; [SerializeField] [Tooltip("滚动缓冲区")] private float scrollBuffer = 30f; private EMaskScrollStatus scrollStatus = EMaskScrollStatus.NONE; private RectTransform contentsTransform = null; /// /// 滚动到哪一个元素 /// private int targetScrollIndex = 0; public EMaskScrollStatus ScrollStatus { set { scrollStatus = value; } get { return scrollStatus; } } private void Awake() { contentsTransform = transform.Find("avatars") as RectTransform; } // Start is called before the first frame update void Start() { } public void StartScroll() { // 开始加速滚动 scrollStatus = EMaskScrollStatus.SCROLLING_ACCE; targetScrollIndex = Random.Range(0,contentsTransform.childCount); } // Update is called once per frame void Update() { if(scrollStatus == EMaskScrollStatus.SCROLLING_ACCE || scrollStatus == EMaskScrollStatus.SCROLLING_DESC) { if(scrollStatus == EMaskScrollStatus.SCROLLING_ACCE) { } else { } } } }
写到这当时我就想能根据我选择的滚动轴动态去显示滚动方向呢,于是就有了以下的答案:
using System.Collections; using System.Collections.Generic; using UnityEditor; [CustomEditor(typeof(MaskScroll))] public class MaskScrollEditor : Editor { public override void OnInspectorGUI() { DrawDefaultInspector(); MaskScroll maskScroll = (MaskScroll)target; if(maskScroll.scrollDir == EMaskScrollDirection.Horizontal) { maskScroll.scrollHorizontal = (EMaskScrollLeftRight)EditorGUILayout.EnumPopup("滚动方向",maskScroll.scrollHorizontal); } else if (maskScroll.scrollDir == EMaskScrollDirection.Vertical) { maskScroll.scrollVertical = (EMaskScrollUpDown)EditorGUILayout.EnumPopup("滚动方向", maskScroll.scrollVertical); } } }
另外注意不要在该代码里面加入 base.OnInspectorGUI()否则组件会加入
到编辑器两次的
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。