重写Unity Button按钮

功能介绍:防止连点功能,自定义添加点击新事件。

using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NewButton : Button
{
    bool AllowClick;
    float Times = 0;
    protected override void Awake()
    {
        onClick.AddListener(OnClick);
    }

    private void OnClick()
    {
        this.transform.GetComponent<Image>().raycastTarget = false;
        AllowClick = false;
    }
    private void Update()
    {
        if (!AllowClick)
        {
            Times += Time.deltaTime;
            if (Times>1)
            {
                Times = 0;
                AllowClick = true;
                Debug.LogError(this.transform.name);
                this.transform.GetComponent<Image>().raycastTarget = true;
            }
        }
    }
    protected override void DoStateTransition(SelectionState state, bool instant)
    {
        
        base.DoStateTransition(state, instant);
        switch (state)
        {
            case SelectionState.Disabled:
               // Debug.LogError("Button失效!");
                break;

            case SelectionState.Highlighted:

               // Debug.LogError("鼠标移到button上!");

                break;

            case SelectionState.Normal:

               // Debug.LogError("鼠标离开Button!");
                break;

            case SelectionState.Pressed:
               // Debug.LogError("鼠标按下Button!");
                break;

            default:

                break;
        }
}
}

当按钮点击下去的时候,系统默认是选中按钮状态,是为了方便按键盘可以选择按钮,如果不需要的话,在这个时候就需要将Navigation改变None
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章