自学内容网 自学内容网

Unity的Button组件进行扩展

废话不多说,在Untiy中,如果想要对Button等组件进行扩展的话,那么不仅仅只需要将新增的属性设置为public或者增加SerializeField字段就行了的,同时需要对Inspector的GUI面板进行修改,以下直接展示代码:

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;
using UnityEditor;

[CustomEditor(typeof(ButtonExtension))]
public class ButtonExtensionEditor : UnityEditor.UI.ButtonEditor    
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        ButtonExtension myScript = (ButtonExtension)target;
        myScript.singleClickEnabled = GUILayout.Toggle(myScript.singleClickEnabled, "单击");//创建一个打开/关闭的开关按钮。
        myScript.doubleClickEnabled = GUILayout.Toggle(myScript.doubleClickEnabled, "双击");
        if (myScript.doubleClickEnabled)
        {
            myScript.doubleClickTime = EditorGUILayout.FloatField("双击时间", myScript.doubleClickTime);
        }
        myScript.longPressEnabled = GUILayout.Toggle(myScript.longPressEnabled, "长按");
        if (myScript.longPressEnabled)
        {
            myScript.minPressTime = EditorGUILayout.FloatField("最小按住时间", myScript.minPressTime);
        }
        base.OnInspectorGUI();
    }
}

public class ButtonExtension : Button
{
    #region 单机双击相关
 
    [SerializeField]public bool singleClickEnabled = true;
    public bool doubleClickEnabled = false;
    public float doubleClickTime = 0.3f;
    
    private float lastClickTime = float.NegativeInfinity;
    private int clickCount = 0;
    
    [FormerlySerializedAs("onDoubleClick")]
    [SerializeField]
    private ButtonClickedEvent doubleClickEvent = new ();
    
    public ButtonClickedEvent onDoubleClick
    {
        get => doubleClickEvent;
        set => doubleClickEvent = value;
    }
 
    public override void OnPointerClick(PointerEventData eventData)
    {
        if(!IsActive() && !interactable) 
            return;
 
        if (singleClickEnabled)
        {
            UISystemProfilerApi.AddMarker("Button.onClick", this);
            onClick?.Invoke();
            Debug.LogError("单击");   
        }
 
        if (doubleClickEnabled)
        {
            clickCount++;
            if (clickCount >= 2)
            {
                if (Time.realtimeSinceStartup - lastClickTime < doubleClickTime)
                {
                    UISystemProfilerApi.AddMarker("Button.onDoubleClick", this);
                    onDoubleClick?.Invoke();
                    Debug.LogError("双击");
                    lastClickTime = float.NegativeInfinity;
                    clickCount = 0;   
                }
                else
                {
                    clickCount = 1;
                    lastClickTime = Time.unscaledTime;
                }
            }
            else
            {
                lastClickTime = Time.unscaledTime;
            }
        }
    }
 
    #endregion
 
    #region 长按相关
 
    public bool longPressEnabled = false;
 
    private float lastPressTime = 0;
    
    public float minPressTime = 0.5f;
 
    private bool isPressing = false;
    
    private bool hasInvokedLongPress = false;
 
    [FormerlySerializedAs("onLongPress")]
    [SerializeField]
    private ButtonClickedEvent longPressEvent = new();
    
    public ButtonClickedEvent onLongPress
    {
        get => longPressEvent;
        set => longPressEvent = value;
    }
    
    public override void OnPointerDown(PointerEventData eventData)
    {
        base.OnPointerDown(eventData);
        if (longPressEnabled)
        {
            hasInvokedLongPress = false;
            isPressing = true;
            lastPressTime = Time.unscaledTime;   
        }
    }
 
    public override void OnPointerUp(PointerEventData eventData)
    {
        base.OnPointerUp(eventData);
        isPressing = false;
        hasInvokedLongPress = false;
    }
 
    private void Update()
    {
        DealLongPress();
    }
    
    private void DealLongPress()
    {
        if(hasInvokedLongPress) return;
        if (isPressing)
        {
            if (Time.unscaledTime - lastPressTime >= minPressTime)
            {
                UISystemProfilerApi.AddMarker("Button.onLongPress", this);
                onLongPress?.Invoke();
                hasInvokedLongPress = true;
                Debug.LogError("执行长按事件");
            }
        }
    }
 
    #endregion
}
 

参考博客:Unity 编辑器-UGUI拓展Button,一个和原Button一样按钮⭐_unity editor button-CSDN博客

[Unity案例]Button的双击和长按_unity button 长按检测-CSDN博客


原文地址:https://blog.csdn.net/alizhuana/article/details/142280706

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!