原来GetComponent<Button>().onClick.AddListener(delegate遍历添加的要清除

使用以下代码清除上次添加的监听事件:

GetComponent<Button>().onClick.RemoveAllListeners();

这将清除所有的点击监听事件。如果只想清除特定的一个监听事件,可以使用以下代码:

UnityAction listenerToRemove = // 在这里添加要移除的监听事件 GetComponent<Button>().onClick.RemoveListener(listenerToRemove);

需要将listenerToRemove替换为您要移除的特定监听事件。

using UnityEngine; using System.Collections; using UnityEngine.UI; using input.core; public class inputManager : MonoBehaviour { private InputField input; private engine en; private string[] result; private int page = 0; private int current = 0; private int surplus = 0; private Button DS1; private Button DS2; private Button DS3; private Button DS4; private Button DS5; private Button DS6; private Button DS7; private Button DS8; private Button pre; private Button nex; private Button clr; private Text[] displayWord = new Text[8]; private Text disp; void getDisplayObject() { input = GameObject.Find ("Input").GetComponent<InputField> (); DS1 = GameObject.FindGameObjectWithTag ("DS1").GetComponent<Button> (); DS2 = GameObject.FindGameObjectWithTag ("DS2").GetComponent<Button> (); DS3 = GameObject.FindGameObjectWithTag ("DS3").GetComponent<Button> (); DS4 = GameObject.FindGameObjectWithTag ("DS4").GetComponent<Button> (); DS5 = GameObject.FindGameObjectWithTag ("DS5").GetComponent<Button> (); DS6 = GameObject.FindGameObjectWithTag ("DS6").GetComponent<Button> (); DS7 = GameObject.FindGameObjectWithTag ("DS7").GetComponent<Button> (); DS8 = GameObject.FindGameObjectWithTag ("DS8").GetComponent<Button> (); pre = GameObject.FindGameObjectWithTag ("pre").GetComponent<Button> (); nex = GameObject.FindGameObjectWithTag ("nex").GetComponent<Button> (); nex = GameObject.FindGameObjectWithTag ("nex").GetComponent<Button> (); displayWord[0] = DS1.transform.Find ("Text").GetComponent<Text> (); displayWord[1] = DS2.transform.Find ("Text").GetComponent<Text> (); displayWord[2] = DS3.transform.Find ("Text").GetComponent<Text> (); displayWord[3] = DS4.transform.Find ("Text").GetComponent<Text> (); displayWord[4] = DS5.transform.Find ("Text").GetComponent<Text> (); displayWord[5] = DS6.transform.Find ("Text").GetComponent<Text> (); displayWord[6] = DS7.transform.Find ("Text").GetComponent<Text> (); displayWord[7] = DS8.transform.Find ("Text").GetComponent<Text> (); disp = GameObject.FindGameObjectWithTag ("display").GetComponent<Text> (); clr = GameObject.FindGameObjectWithTag ("clear").GetComponent<Button> (); } void blindButtonClick() { DS1.onClick.AddListener (delegate { buttonClick("DS1"); }); DS2.onClick.AddListener (delegate { buttonClick("DS2"); }); DS3.onClick.AddListener (delegate { buttonClick("DS3"); }); DS4.onClick.AddListener (delegate { buttonClick("DS4"); }); DS5.onClick.AddListener (delegate { buttonClick("DS5"); }); DS6.onClick.AddListener (delegate { buttonClick("DS6"); }); DS7.onClick.AddListener (delegate { buttonClick("DS7"); }); DS8.onClick.AddListener (delegate { buttonClick("DS8"); }); pre.onClick.AddListener (delegate { preword(); }); nex.onClick.AddListener (delegate { nextword(); }); clr.onClick.AddListener (delegate { disp.text = ""; }); } void buttonClick(string tag) { string tmp = null; switch (tag) { case "DS1": tmp = displayWord[0].text; break; case "DS2": tmp = displayWord[1].text; break; case "DS3": tmp = displayWord[2].text; break; case "DS4": tmp = displayWord[3].text; break; case "DS5": tmp = displayWord[4].text; break; case "DS6": tmp = displayWord[5].text; break; case "DS7": tmp = displayWord[6].text; break; case "DS8": tmp = displayWord[7].text; break; default: break; } disp.text = disp.text + tmp; input.text = ""; input.ActivateInputField (); } void nextword() { if (current == page) current = page; else current++; display (result); } void preword() { if (current == 0) current = 0; else current--; display (result); } void display(string[] str) { clear (); if (page == 0 && surplus != 0) { for (int i = 0; i < surplus; i++) { displayWord [i].text = str [i]; } } else if (page > 0) { if (current == page) { for (int i = 0; i < surplus; i++) { displayWord [i].text = str [page * 8 + i]; } } else { for (int i = 0; i < 8; i++) { displayWord [i].text = str [current * 8 + i]; } } } } void clear() { for (int i = 0; i < 8; i++) { displayWord [i].text = ""; } } // Use this for initialization void Start () { getDisplayObject (); blindButtonClick (); en = new engine (); input.onValueChanged.AddListener (delegate { valueChanged(); }); } // Update is called once per frame void Update () { } void valueChanged() { string str; en.search (input.text,out str); if (str != null) { result = null; result = str.Split (' '); current = 0; page = result.Length / 8; surplus = result.Length % 8; display (result); //Debug.Log (str + page + surplus); } else { //Debug.Log (""); clear(); } } } 使用以上代碼,當有文字輸入時,選項和結果翻譯從簡體轉換到繁體,使用 [Header("翻译API配置")] public bool enableTranslation = true; // 是否启用翻译 private const string TRANSLATE_API = "https://api.mymemory.translated.net/get"; private const string BACKUP_TRANSLATE_API = "https://translation-api.leanapp.cn/translate"; // 备用API api進行翻譯
最新发布
11-20
using System.Collections; using UnityEngine; using TMPro; public class DialogueManager : MonoBehaviour { public enum DilaogueState { before, win, lost } public static DialogueManager Instance; public TextMeshProUGUI tmpText; public float typingSpeed = 0.05f; private bool isTyping = false; private Coroutine typingCoroutine; private string currentSentence; void Awake() { Instance = this; } void Update() { // 添加跳过功能:按任意键跳过当前打字效果 if (Input.GetKeyDown(KeyCode.E) && isTyping) { SkipTyping(); } } // 启动对话并显示特定文本 public void StartDialogue(string sentence) { // 如果正在显示其他文本,先停止 if (isTyping) { StopCoroutine(typingCoroutine); } currentSentence = sentence; // 暂停玩家移动 FindObjectOfType<playercontrol>().canMove = false; // 锁定玩家输入 Cursor.lockState = CursorLockMode.None; Cursor.visible = true; // 显示对话框UI tmpText.gameObject.SetActive(true); // 启动打字效果协程 typingCoroutine = StartCoroutine(TypeSentence(currentSentence)); } IEnumerator TypeSentence(string sentence) { isTyping = true; tmpText.text = ""; // 逐字显示 foreach (char letter in sentence.ToCharArray()) { tmpText.text += letter; yield return new WaitForSeconds(typingSpeed); } isTyping = false; } // 跳过当前打字效果 public void SkipTyping() { if (isTyping) { StopCoroutine(typingCoroutine); tmpText.text = currentSentence; isTyping = false; } } public void EndDialogue() { // 如果还在打字,先停止打字 if (isTyping) { StopCoroutine(typingCoroutine); } // 恢复玩家移动 FindObjectOfType<playercontrol>().canMove = true; // 恢复输入锁定 Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; tmpText.gameObject.SetActive(false); } }根据我给的代码进行整合以及详细描述分析
07-31
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值