重寫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
在這裏插入圖片描述

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