【问题标题】:Multitouch tap count多点触控点击数
【发布时间】:2016-10-16 17:47:06
【问题描述】:

帮助我用 OnMouseButton() 更改我的“ClickCounter.cs”,用于多点触控手机。我想显示我在显示屏上点击了多少次,但我的 var "count" 在 Update() 中的每一帧都递增。 我的鼠标代码 - “ClickCounter.cs”

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

public class ClickCounter : MonoBehaviour
{

    Text ScoreText; //var for my text

    void Start()
    {
        ScoreText = GameObject.Find("Score").GetComponent<Text>();
    }


    int count = 0;
    void OnMouseDown() //func count my click, but cant counting multitouch 
    {
       count++;
       ScoreText.text = "Score: " + count.ToString(); //text field with score (click count)
       GameObject.Find("Pride").GetComponent<Animator>().SetTrigger("Click"); //some animation
       Debug.Log(count);

    }
}

我的安卓代码

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

public class TouchCounter : MonoBehaviour
{
    Text scoreText;

    void Start()
    {
        scoreText = GameObject.Find("Score").GetComponent<Text>(); //same 
    }


    void Update ()
    {
            if (Input.touchCount > 0) Counter(); //touch check
    }

    int count = 0;
    void Counter() //
    {
            count++;
            scoreText.text = "Score: " + count.ToString();
            GameObject.Find("Pride").GetComponent<Animator>().SetTrigger("Click");
    }
}

【问题讨论】:

    标签: c# android unity3d unityscript unity5


    【解决方案1】:

    当一根或多根手指放在屏幕上时,if (Input.touchCount &gt; 0) 始终为true。由于这是在 Update 函数中运行的,因此 Counter() 每秒将被调用数十次,具体取决于您的帧速率。

    您还必须检查TouchPhase.EndedTouchPhase.Began

    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) Debug.Log("Tapped");
    

    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) Debug.Log("Tapped");
    

    应该这样做。

    即使这应该工作,还有另一个问题。它不适用于多个手指同时敲击。您必须遍历 Input.touchCount 才能使用多个手指完成这项工作。

    另一件事是水龙头应该有一个计时器。确定是否应将其视为点击的计时器。例如,将手指放在屏幕上超过一秒钟不应称为点击。下面的解决方案解决了所有这些问题。 timeOut 变量可用于设置玩家应将手指放在屏幕上多长时间才能被视为点击。

    默认情况下,超过0.5 秒的任何内容都不是点击。

    float[] fingerIdTimer = new float[5] { 0f, 0f, 0f, 0f, 0f }; //5 fingers max
    bool[] fingerIdValid = new bool[5] { true, true, true, true, true }; //One determine invalid, must be rest in TouchPhase.Ended
    const float timeOut = 0.5f; //Anything more than 0 and less than timeOut value is tap
    
    void Update()
    {
        //Loop over all finger touching the screen
        for (int i = 0; i < Input.touchCount; i++)
        {
            //Will only increment if finger is valid
            if (fingerIdValid[i])
            {
                fingerIdTimer[i] += Time.deltaTime;
            }
    
            //If we reach the time out value and finger is still valid reset the finger id
            if (fingerIdTimer[i] > timeOut && fingerIdValid[i])
            {
                fingerIdTimer[i] = 0f; //Reset Held Time
                fingerIdValid[i] = false; //Invalid
                OnTapFailed(i, fingerIdTimer[i]);
            }
    
            //After touch is released, Anything more than 0 and less than timerOut value is tap
            if (Input.GetTouch(i).phase == TouchPhase.Ended)
            {
                if (fingerIdTimer[i] > 0 && fingerIdTimer[i] < timeOut)
                {
                    OnTapSuccess(i, fingerIdTimer[i]);
                }
    
                fingerIdTimer[i] = 0f; //Reset Held Time when released
                fingerIdValid[i] = true; //Reset Invalid when released
            }
        }
    }
    
    int count = 0;
    
    //Tap was successful
    void OnTapSuccess(int fingerId, float heldTime)
    {
        count++; //Increment the tap count
    
        Debug.Log("Tapped Count: " + count + "\r\n"
            + "Finger ID: " + fingerId + "\r\n"
            + "Held Time: " + heldTime);
    
        //scoreText.text = "Score: " + count.ToString();
        //GameObject.Find("Pride").GetComponent<Animator>().SetTrigger("Click");
    }
    
    //Tap failed (Time out Occured)
    void OnTapFailed(int fingerId, float heldTime)
    {
        Debug.Log("Tap Failed: " + fingerId);
    }
    

    【讨论】:

    • 谢谢你,伙计!它工作得很好!我正在尝试“TouchPhase.Ended & TouchPhase.Began”,脚本正在运行,但我失去了多点触控能力。再次感谢
    【解决方案2】:

    您设置了 count = 0,但在 Update 函数之外。因此,在一次触摸之后,计数将始终为 !0。只需将此语句放入函数中即可。

    【讨论】:

    • 当我把这个 var 放入函数中时,我在退出时得到“count = 1”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2014-01-12
    相关资源
    最近更新 更多