Massive lag spike on first Input.GetTouch with nothing on scene but one script (Android)
I was having problems with one of my projects every time I pressed for first time the screen. At first I thought it was caused by a script that disabled an UI Text but after one day of researching I couldn't find any answer so I decided to try recreating the problem in an empty scene. The results are that whenever I place a script that checks for Input.GetTouch(0), the first time I touch the screen, the game freezes for a fraction of a second even if the scene is completely empty. I took a look at the profiler and the script checking for the input was generating that lag spike. Note that after the first touch everything works smoothly and without any kind of lag spike.
This was the script used using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Scr_Input : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
Debug.Log("AAA");
}
}
}
}
And here's the profiler when I first touch the device's screen. The device I tested on is a Xiaomi MI A1 but I had this kind of problem before using a Samsung Galaxy S9.
Now you may notice that the lag spike is generated by the Debug.Log() code but It just doesn't make sense to me since in my other projects I don't have any Debug.Log() going on and there are other things happening before I touch the screen so there is no other reason for the lag spike that the Input.GetTouch(). Also, in the picture you may see smaller spikes, those are the touches after the first one, just to prove that it only happens on the first touch.
So if you know how to avoid this I would be really thankful since it's completely ruining the game experience because the first touch when you open the game will always generate a lag spike no matter the device.
UPDATE
Since I've read that Debug.Log() is expensive for perfomance I've changed it and placed a transform.position += new Vector3(1, 0, 0);
And as expected the lag spike isn't that massive but even though the first touch is still generating a bigger lag spike than the ones after that and if you consider a bigger script (as the ones in my other project), the lag spike gets way too big and you can tell that the game lags at the first touch.
Here's the Profiler
Answer by Visuallization · Jan 17, 2021 at 01:09 PM
Okay I found a solution which works for me. It is split into 2 parts:
1.Trigger a drag start via code. It might be the only part you need to "fix" this issue. This is how I did it:
void Start() { // Trigger drag start via code to prevent initial drag delay on android PointerEventData pointer = new PointerEventData(EventSystem.current); ExecuteEvents.Execute(gameObject, pointer, ExecuteEvents.beginDragHandler); }For maybe obvious reasons: gameObject should be the the object which should receive the drag.
2.Set the "Scripting Backend" to "IL2CPP" in the "Player Settings" for Android.
This worked for me without step 1 in an empty scroll rect scene. In a more complex scene I also had to do step 1.
Regarding IL2CPP: as far as I have understood it from various posts, IL2CPP uses ahead of time compilation (AOT) in contrast to the default mono scripting backend which uses just in time compilation (JIT). The result for me with IL2CPP turned on is a faster app and also a reduced apk size (saved around 10 MB, in my case). Downside is that building takes longer (as it compiles ahead of time) than it would take with Mono. So you might only want to do this when actually releasing your game (depending how long building actually takes and if you want to deal with it during development).
There are even more pros & cons, so for a more detailed information check this post: https://learn.unity.com/tutorial/memory-management-in-unity#5c7f8528edbc2a002053b59b
I hope this helps someone else!
Cheers
Thank you for the answer! I thought this question was already dead. I haven't tested your workaround but made something similar to fix it when I needed to (I simulated the first touch in the Start() function)
Answer by Visuallization · Jan 15, 2021 at 01:20 PM
I am actually experiencing the same issue on android even with the native unity scrollrect in an otherwise empty scene. Is there anyone out there who can help with this issue? Any unity folks/pros?