【发布时间】:2018-12-31 05:10:58
【问题描述】:
我是新来的,我正在 UNITY 开始我的冒险之旅。我有双击事件的问题。我想在我的商店买东西或卖东西。当我早些时候在统一(public Button button;)上分配一个按钮时,它可以工作。但是当我尝试对 Start 和 Update 方法上的按钮进行此更改时:
void Start () {
button = GameObject.Find(EventSystem.current.currentSelectedGameObject.name).GetComponent<Button>();
button.onClick.AddListener(ButtonListner);
}
void Update()
{
button = GameObject.Find(EventSystem.current.currentSelectedGameObject.name).GetComponent<Button>();
}
private void ButtonListner()
{
counter++;
if (counter == 1)
{
StartCoroutine("doubleClickEvent");
}
}
IEnumerator doubleClickEvent()
{
yield return new WaitForSeconds(clickTimer);
if (counter > 1)
{...}
不幸的是,方法 doubleClickEvent() 不起作用... 我该怎么办?问候;)
【问题讨论】:
-
为什么它不起作用?是没有被调用还是没有达到预期的效果?
-
问题在于,在
Update方法中,您正在重新分配按钮,但没有使用AddListener向其添加单击事件处理程序。如果您更改按钮,我想您需要删除按钮上的侦听器,分配新按钮,然后将侦听器添加到新按钮。 -
克里斯谢谢你帮助我。但我现在不同了。当我开始在商店中选择商品之前,我一直在使用红色 nullRefferenceExeption。这可以通过某种方式解决吗?
标签: c# unity3d double-click