关于Unity中组件选择枚举的不同,其他属性跟着变的功能一直没有写,今天补上,
首先定义一个MaskScroll滚动的组件,这个组件支持水平和竖直方向上的滚动,还有加速减速滚动的功能
cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum EMaskScrollStatus {
NONE,
/// <summary>
/// 停止
/// </summary>
STOP,
/// <summary>
/// 加速滚动
/// </summary>
SCROLLING_ACCE,
/// <summary>
/// 减速滚动
/// </summary>
SCROLLING_DESC
}
public enum EMaskScrollDirection {
/// <summary>
/// 水平方向
/// </summary>
Horizontal,
/// <summary>
/// 竖直方向
/// </summary>
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;
/// <summary>
/// 滚动到哪一个元素
/// </summary>
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 {
}
}
}
}
写到这当时我就想能根据我选择的滚动轴动态去显示滚动方向呢,于是就有了以下的答案:
cs
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()否则组件会加入
到编辑器两次的