Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by DEvalen200 · Sep 02, 2019 at 04:19 AM · androidinputtouchlageventsystem

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.

alt text

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 alt text

3.png (175.2 kB)
2.png (178.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DEvalen200 · Jan 18, 2021 at 02:23 AM 0
Share

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)

avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

352 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Having an issue with touch input lag on certain Android devices, any help? 1 Answer

I want to move my cube on right or left when I touch screen for Android devices; Please help me which script I have to use 0 Answers

Touch radius always returns zero on Android 0 Answers

UNITY 2D ANDROID DEVICE TOUCH PROBLEM 0 Answers

Android| Camera scrolling 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges