unity項(xiàng)目防止快速連續(xù)點(diǎn)擊按鈕UI

在unity項(xiàng)目中我們經(jīng)常遇到這種情況:當(dāng)我們點(diǎn)擊一個(gè)按鈕UI后會(huì)等待網(wǎng)絡(luò)響應(yīng)或者等待資源加載,這個(gè)時(shí)候我們不希望當(dāng)前界面的按鈕ui還可以繼續(xù)點(diǎn)擊,否則就會(huì)出現(xiàn)多個(gè)按鈕UI命令沖突等bug。通常這種情況都可以添加遮罩UI來解決(或者利用計(jì)時(shí)器,這里不做贅述),但是這里要分享的是另外一種方法,通過UI事件的監(jiān)聽腳本來實(shí)現(xiàn)點(diǎn)擊一個(gè)按鈕后讓所有按鈕UI都失效。

首先,轉(zhuǎn)載一篇雨松MoMo的文章:http://www.xuanyusong.com/archives/3325

(* 轉(zhuǎn)載請(qǐng)注明: 雨松MOMO <time>2014年10月27日 </time>于 雨松MOMO程序研究院 發(fā)表
)這篇文章方便理解ui按鈕的事件監(jiān)聽。

一、開始創(chuàng)建unity項(xiàng)目場(chǎng)景

image.png

image.png
GUI是為了測(cè)試打印。

二、創(chuàng)建兩個(gè)腳本EventTriggerListener、UIButtonTest

其中EventTriggerListener腳本和雨松MoMo文章里的基本一樣,只是修改和添加了幾行代碼:

//下面為修改的代碼
 public override void OnPointerClick(PointerEventData eventData)
    {
        
        if (onClick != null&& OnCallClick())
        {
            onClick(gameObject);
        }
    }

//下面為添加的代碼
    public static bool isButtonSuo = false;  //全局的靜態(tài)bool變量,用來表示ui按鈕是否加鎖了
    public bool isSpecialButton = false;  //表示該ui按鈕是否是點(diǎn)擊后把所有ui按鈕(除了解鎖按鈕)都鎖起來的按鈕(比如開始說的進(jìn)入游戲按鈕)
    public bool deblocking = false;  //表示該ui按鈕是不是解鎖按鈕(點(diǎn)擊后把所有按鈕的鎖打開)

    bool OnCallClick()  
    {
        if (deblocking)
        {
            isButtonSuo = false;
        }
        if (!enabled || isButtonSuo) return false;  
        isButtonSuo = isSpecialButton;

        return true;
    }

然后是另一個(gè)腳本UIButtonTest(名字隨意定義,這只是我這樣起的名字)

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

public class UIButtonTest : MonoBehaviour {

    public GameObject _suo;  //加鎖按鈕
    public GameObject _hello;  //“你好呀”
    public GameObject _hello1;  //“我不好”
    public GameObject _hello2;  //"哈哈哈"
    public GameObject _deblocking;  //“解鎖按鈕”
    public GUIText _guitext;  //GUI的text,為了方便看打印輸出

    private int index = 0;  //為了方便分辨打印輸出
    void Start () {
        EventTriggerListener.Get(_suo).onClick = OnclickSuo;
        EventTriggerListener.Get(_suo).isSpecialButton = true;

        EventTriggerListener.Get(_hello).onClick = OnClickHello;
        EventTriggerListener.Get(_hello1).onClick = OnClickHello;
        EventTriggerListener.Get(_hello2).onClick = OnClickHello;

        EventTriggerListener.Get(_deblocking).onClick = OnClickDelocking;
        EventTriggerListener.Get(_deblocking).deblocking = true;
    }

    void OnclickSuo(GameObject go)
    {
        _guitext.text += "--所有按鈕上鎖了(除了解鎖按鈕)--";
    }

    void OnClickHello(GameObject go)
    {
        _guitext.text = go.GetComponentInChildren<Text>().text + "++" + (++index);
    }

    void OnClickHello1(GameObject go)
    {
        _guitext.text = "你好呀!++" + (++index);
    }   

    void OnClickDelocking(GameObject go)
    {
        _guitext.text = "解鎖成功--";
        index = 0;
    }
}

然后把這UIButtonTest腳本放到場(chǎng)景的物體身上;
記得把場(chǎng)景中的ui按鈕和GUIText物體拖入到UIButtonTest腳本對(duì)應(yīng)的public的字段中

三、最后就可以運(yùn)行查看效果了

我的作品1.gif

來個(gè)大的gif圖:
我的作品3.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容