【问题标题】:Unity Double Click eventUnity 双击事件
【发布时间】: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


【解决方案1】:

我注意到的第一件事是:button = GameObject.Find(EventSystem.current.currentSelectedGameObject.name).GetComponent&lt;Button&gt;();

EventSystem.current.currentSelectedGameObject 属性在任何时候都可以是null,尤其是在第一帧中,这意味着在Start 函数中使用它不是一个好主意。找到Button GameObject,然后从中获取Button 组件:

Button button;

void Start()
{
    button = GameObject.Find("YourButtonName").GetComponent<Button>();
    button.onClick.AddListener(ButtonListner);
}

"YourButtonName" 替换为您的Button 游戏对象的名称。


你甚至不需要做大部分你做过的事情。您可以通过OnPointerClick 函数使用PointerEventData.clickCount 获得双击或点击计数。您必须实现 IPointerClickHandler 接口才能使其工作。

只需附加到Button GameObject:

public class ClickCountDetector : MonoBehaviour, IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        int clickCount = eventData.clickCount;

        if (clickCount == 1)
            OnSingleClick();
        else if (clickCount == 2)
            OnDoubleClick();
        else if (clickCount > 2)
            OnMultiClick();
    }

    void OnSingleClick()
    {
        Debug.Log("Single Clicked");
    }

    void OnDoubleClick()
    {
        Debug.Log("Double Clicked");
    }

    void OnMultiClick()
    {
        Debug.Log("MultiClick Clicked");
    }
}

【讨论】:

  • The EventSystem.current.currentSelectedGameObject property can be null at anytime 不说了,想必EventSystem.current.currentSelectedGameObject就是后面GameObject.Find(...)返回的对象……
  • 它适用于桌面,但不适用于移动设备。我要双击
【解决方案2】:

您可以在没有协程的情况下检测双击,如下所示:

// Choose the time you want between clicks to consider it a double click
float doubleClickTime = .2f, lastClickTime;    

void Update()
{
    // Checking left mouse button click, you could choose the input you want here
    if (Input.GetMouseButtonDown(0))
    {
        float timeSinceLastClick = Time.time - lastClickTime;

        if (timeSinceLastClick <= doubleClickTime)
            Debug.Log("Double click");
        else
            Debug.Log("Normal click");

        lastClickTime = Time.time;
    }
}

【讨论】:

    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    相关资源
    最近更新 更多