多点触点计数
本文关键字:触点 多点 | 更新日期: 2023-09-27 18:13:13
帮助我用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);
}
}
我的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");
}
}
当一个手指或多个手指放在屏幕上时,if (Input.touchCount > 0)
总是true
。因为这是在Update函数中运行的,所以Counter()
将根据你的帧率每秒被调用几十次。
您还必须检查TouchPhase.Ended
或TouchPhase.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);
}
设置count = 0,但是在Update函数之外。所以在一次触摸之后,count总是为!0。