帮我改变带有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");
}
}
答案 0 :(得分:2)
当一个或多个手指放在屏幕上时,if (Input.touchCount > 0)
始终为true
。由于这是在更新功能中运行的,因此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);
}
答案 1 :(得分:0)
你设置count = 0,但在Update函数之外。所以一次触摸后,计数将永远是!0。只需将此声明放入函数中即可。