Unity实现点击按钮时自动设置自身interactable为false(无需逐个定义)
无需逐个定义按钮实现点击后禁用的方案
我希望避免玩家重复点击按钮,因此需要在玩家点击按钮时将其
interactable属性设置为false。但由于不同场景中按钮数量不固定,逐个定义按钮对象效率低下,请问如何无需逐个定义按钮就能实现该功能?
你提供的原始代码如下:
public string password = "1234" ; public GameObject circle; public int circleCounter; public int correctCounter; public void Update() { circleSpawner(); } public void circleSpawner() { if (Input.GetMouseButtonDown(0) && circleCounter < password.Length) { Vector3 mousePosition = Input.mousePosition; mousePosition.z = 10; GameObject newObject = Instantiate(circle, mousePosition, Quaternion.identity,transform); newObject.transform.SetSiblingIndex(0); circleCounter++; } } public void correctPasswordClick() { correctCounter++; if (correctCounter == password.Length) { Debug.Log("correct password"); } }
给你几个实用的方案,不用逐个定义按钮就能实现需求:
方案一:点击事件直接传递按钮自身引用
修改密码验证方法,让它接收点击的按钮作为参数,点击时直接禁用该按钮:
using UnityEngine.UI; // 必须引用UI命名空间 public string password = "1234"; public GameObject circle; public int circleCounter; public int correctCounter; public void Update() { circleSpawner(); } public void circleSpawner() { if (Input.GetMouseButtonDown(0) && circleCounter < password.Length) { Vector3 mousePosition = Input.mousePosition; mousePosition.z = 10; GameObject newObject = Instantiate(circle, mousePosition, Quaternion.identity, transform); newObject.transform.SetSiblingIndex(0); circleCounter++; } } // 修改方法,接收Button类型的参数 public void correctPasswordClick(Button clickedButton) { // 点击后立刻禁用当前按钮 clickedButton.interactable = false; correctCounter++; if (correctCounter == password.Length) { Debug.Log("密码正确"); } }
操作步骤:在Unity编辑器里,给每个密码按钮的OnClick事件添加这个方法,然后把按钮自身拖到方法的参数输入框里即可。不管有多少按钮,只要这么设置,点击后都会自动禁用。
方案二:给按钮加通用禁用脚本
写一个独立的小脚本,挂到所有需要禁用的按钮上,自动处理点击禁用逻辑:
创建DisableOnClick.cs脚本:
using UnityEngine.UI; using UnityEngine; public class DisableOnClick : MonoBehaviour { private Button targetBtn; void Awake() { targetBtn = GetComponent<Button>(); // 自动绑定点击事件 targetBtn.onClick.AddListener(DisableButton); } void DisableButton() { targetBtn.interactable = false; // 如果需要同时触发密码验证,在这里调用你的验证方法 // 比如找到你的密码脚本对象: FindObjectOfType<YourPasswordScriptName>().correctPasswordClick(); } }
操作步骤:把这个脚本拖到任意需要禁用的按钮上就行,按钮点击后会自动禁用,同时还能触发你的密码验证逻辑(记得把YourPasswordScriptName改成你实际的脚本类名)。
方案三:批量获取场景按钮统一绑定
如果场景里的按钮是静态存在的,可以在脚本启动时自动找到所有按钮,批量绑定禁用逻辑:
using UnityEngine.UI; using UnityEngine; using System.Collections.Generic; public string password = "1234"; public GameObject circle; public int circleCounter; public int correctCounter; private List<Button> allPasswordButtons; void Start() { // 获取场景中所有Button组件,如果有特定标签可以用标签筛选(比如给密码按钮加"PasswordBtn"标签) allPasswordButtons = new List<Button>(FindObjectsOfType<Button>()); // 按标签筛选的写法:allPasswordButtons = new List<Button>(GameObject.FindGameObjectsWithTag("PasswordBtn")).ConvertAll(obj => obj.GetComponent<Button>()); // 给每个按钮绑定点击事件 foreach (Button btn in allPasswordButtons) { btn.onClick.AddListener(() => correctPasswordClick(btn)); } } public void Update() { circleSpawner(); } public void circleSpawner() { if (Input.GetMouseButtonDown(0) && circleCounter < password.Length) { Vector3 mousePosition = Input.mousePosition; mousePosition.z = 10; GameObject newObject = Instantiate(circle, mousePosition, Quaternion.identity, transform); newObject.transform.SetSiblingIndex(0); circleCounter++; } } public void correctPasswordClick(Button clickedButton) { clickedButton.interactable = false; correctCounter++; if (correctCounter == password.Length) { Debug.Log("密码正确"); } }
如果是动态生成的按钮,可以在circleSpawner方法里生成按钮后,直接获取Button组件并绑定事件:
// 在circleSpawner里生成按钮后添加这段 Button newBtn = newObject.GetComponent<Button>(); if (newBtn != null) { newBtn.onClick.AddListener(() => correctPasswordClick(newBtn)); }
内容的提问来源于stack exchange,提问作者Fahrettin Özkan
相关产品推荐
相关产品推荐

