EDIT: Can’t be sure this is the problem any more.
Performance just seems to be generally unpredictable on Android. I can build, run, and get 20fps. Close the app, open it again, 20fps. Disconnect from PC, restart phone, open app, 20fps. Rebuild the EXACT SAME PROJECT WITH NO CHANGES and get 60fps every time I open the rebuilt app.
(Unity 2020.1.2f1)
This is the code I have on one object in the scene:
void Update()
{
for (int i = 0; i < Input.touchCount; ++i)
{
Touch t = Input.GetTouch(i);
Vector2 tpos = t.position;
tpos.x /= Screen.width;
tpos.y /= Screen.height;
if (t.position.x < Screen.width * 0.5f)
{
if (t.phase == TouchPhase.Began)
{
refx = tpos.x;
}
value = Mathf.Clamp((tpos.x - refx) * 7f, -1.0f, 1.0f);
}
}
}
If I disable that object, the framerate is a stable 60fps. If I enable it, it drops to 12-20fps.
By the way: ‘value’ is just being applied to the steering of a car (derived from the Unity tutorial on building a car)
If I disable the steering object and feed a constant number to the steering instead of ‘value’, everything is fine and smooth.
EDIT: Stranger and stranger. I tried this:
for (int j = 0; j < Input.touchCount; ++j) { }
int i = 0;
if (Input.touchCount > 0)
{
Touch t = Input.GetTouch(i);
Vector2 tpos = t.position;
tpos.x /= Screen.width;
tpos.y /= Screen.height;
if (t.position.x < Screen.width * 0.5f)
{
if (t.phase == TouchPhase.Began)
{
refx = tpos.x;
}
value = Mathf.Clamp((tpos.x - refx) * 7f, -1.0f, 1.0f);
}
}
This also runs at 12-20fps, BUT if I delete the ‘for’ line at the top (which does nothing!) the game goes back up to 60fps.
EDIT: It seems that polling Input.touchCount in a for loop is HUGELY expensive. If I cache the result and do a for-loop over that, there’s no slowdown. It can’t be the number of times it’s polled that’s the issue, because the slowdown occurs even when the number of touches is zero (which would mean it’s only polled once).