Reputation: 17
I'm trying to implement touch controls on my current Unity project. I'm currently trying to execute this line of code, but I have seen that fps has dropped significantly and it executes lines about touch controls more than I expected.
for(int i=0;i<Input.touchCount;i++)
{
Debug.log("Touched");
}
I'm curios about that, is there more effective way to get touch inputs from user both for IOS and Android, thanks.
Upvotes: 0
Views: 119
Reputation: 51
I suppose you're using this in your Update function. You need to do it that way :
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
Debug.Log("Touched");
}
}
}
That way it will only be called one time for a touch, at its begining.
The way you're doing it is "while the screen is touched, print 'Touched' in log".
If you want to do it for many fingers you can do
void Update()
{
if (Input.touchCount > 0)
{
int countTouches = Input.touchCount;
for (int i = 0; i < countTouches; i++)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
Debug.Log("Touched");
}
}
}
}
Upvotes: 0