多点触控点击次数

Multitouch tap count

帮我用 OnMouseButton() 改变我的 "ClickCounter.cs" 移动设备 phone 多点触控。我想显示我在显示器上点击了多少次,但我的 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);

    }
}

我的代码 android

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");
    }
}

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

当一根或多根手指放在屏幕上时,if (Input.touchCount > 0)总是true。由于这是更新函数中的 运行,根据您的帧速率,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);
}