Search Unity

Lag spike from SystemEvent with the first touch.

Discussion in 'UGUI & TextMesh Pro' started by Spidyy, Jun 26, 2015.

  1. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    Hey there.

    I'm using Unity 4.6.6 for Android and iOS. I have a Physics Raycaster and Graphics Raycaster on my scene and implemented IDragHandler on multiple objects. There is at most a dozen of those objects, not more.

    On low end devices, each time I try a drag for the first time, a lag spike occur. This lag spike would be without importance if it wouldn't make my collision skip multiple frames, thus having the object I drag jump over each other instead of properly colliding. :(

    Does someone have a hack of some sort or know the reason of this behaviour?

    I saw multiple post on the forums without any real answers.
     
  2. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    Bump! This really is problematic for me.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    have you tried using the profiler to determine what is causing the problem specifically? without knowing what you've actually done it might be a bit hard to guess the issue.
     
  4. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    In 4.6.1 I saw lag spikes on touch on Android devices. Did not debug beyond that. Do remember that it was worse on first touch but also happened ~10 minutes into gameplay. The lag was coordinated with touch.
     
  5. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    The profile don't indicate more than just EventSystem.Update() call. The rest is invisible.
     
  6. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    Bumping back this, because we still don't know why it does it and how to fix it. :(
     
  7. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    Bump again, I don't know what to do with this. I don't want to rewrite all my input management scripts as we are close to the release. :(
     
  8. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    When you say low end devices how low are we talking? Like is for some reason using the physics raycaster and it creating/ shooting the ray causing the issue?
     
  9. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    I can't say if it comes from the raycasting itself. The effect occur only the first I start a swipe gesture, freezing the screen for a good half second, then it stays clear for the entire lifetime of the app. It feels more like the system have to initialize something on the first raycast event.

    The devices I used to test it are :
    - HUAWEI P6-U06, under android 4.4.2 (quite visible)
    - Samsung Galaxy S4 Mini
    - iPhone 4 (less visible)
     
  10. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Are you for some reason updating the list of objects where you are dragging from by deleting them and creating it back again?

    If that is the case it is most definitely the issue, I had the same problem since I was using a sorted item list and every time I needed to drag out or drop an item I had to sort again. Deleting the entire item collection and creating it back was a heavy operation for the mobile device I was testing, it created a spike in the Garbage Collector (I was using Unity 5.1.2f3) and was making a huge lag when dragging an item out, you didn't even receive feedback for a couple of seconds, I was using a dozen objects exactly.

    My solution was to parent the GameObjects to a disabled CacheTransform and then search the objects in the cache first when filling the list again instead of trying to create them. If you have way too many objects I would recommend to use a coroutine that handle this over several frames, like setting a threshold amount of objects per frame, but for my case it was not necessary.

    Let me know if this was your issue.
    Best regards,
    Lermy
     
  11. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    I only use the events to move the game objects around the world. Of course using rigidbody. If it was what you though, it would freeze not only the first time but each time I move them.
     
  12. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Hmm i'm not sure and i'm not really seeing much of a spike on my side. I dont have any devices that low to test it on
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Can you poke the EventSystem by calling it's various methods to get it to initialise earlier while loading?

    Proceed with caution, that sort of behaviour really is a hack.
     
  14. johnpine

    johnpine

    Joined:
    Sep 19, 2015
    Posts:
    26
    Confirmed, I found this one yesterday. Was wondering why i get frame drops ( 100-200ms lags ) on Android only.
    And my phone is not the low end, Samsung Note 4. Looks like it is definitely to do something with the EventSystem. haven't checked the source code for it for possible reason but this is really annoying.
    Even the simplest scene, clean, make a button, make o rotating 3d box on the screen so you can see the lag when you click the screen the first time. After the first click the consequent clicks wont stall. But after say 10 seconds, its back.
    Probably due some garbage collection done so I fixed this by doing a script that touches the screen once per every second. Works now. Never lags anymore from the first click.
     
    lermy3d likes this.
  15. ___Petr___

    ___Petr___

    Joined:
    Jun 4, 2013
    Posts:
    40
    We have the same issue. Can you post this script please?
     
    lermy3d likes this.
  16. YoungDeveloper

    YoungDeveloper

    Joined:
    Jun 28, 2013
    Posts:
    65
    Auto generated EventSystem comes with StandaloneInputModule component. After removing it EventSystem no longer produces huge spike on first touch on mobile devices.
     
    lermy3d likes this.
  17. Banics

    Banics

    Joined:
    Jan 27, 2017
    Posts:
    2
    Hi. I hav ethe same problem...but removing StandaloneInputModule component all button system stop to work. Someone fix this bug? I work in Unity 5.6.1f1.
    Tnaks and sorry for my bad english.
     
  18. denizluleci

    denizluleci

    Joined:
    May 30, 2016
    Posts:
    7
    Unfortunately I have this problem too :( Any updates?
     
  19. nineswordz

    nineswordz

    Joined:
    Sep 23, 2019
    Posts:
    12
    I have to bump this problem. It's causing a noticeable first touch lag on my game too. Removing the StandaloneInputModule doesn't work. Any help is appreciated.
     
  20. unity_B4Ps5BamZ68Ouw

    unity_B4Ps5BamZ68Ouw

    Joined:
    May 10, 2020
    Posts:
    3
    same prob here...First touch = 500 ms lag....Android mi a1...
     
  21. unity_B4Ps5BamZ68Ouw

    unity_B4Ps5BamZ68Ouw

    Joined:
    May 10, 2020
    Posts:
    3
    ISSUE FIXED FOR ME....this is not a bug....

    OK, i am using a InputHandler script to handle all my touch inputs... It has multiple event handlers which u can subscribe using += Callbackfunction().

    public event Action onInputLeft;
    public event Action onInputRight;
    public event Action onInputUp;
    public event Action onInputDown;
    public event Action onTouchStart;

    but i was only subscribing to left and right functions and hence rest were null.
    My input handler script still called the function which were not subscribed when it registered a touch and hence caused NullRefenceException which caused the 500ms lag.

    The fix was to invoke the function only when it was subscribed ( Not null )

    In your input handler script ....Replace " onTouchStart() " with " onTouchStart?.Invoke() "
    If this was helpful please like the response so others can find it too.
     
  22. prabhukaran

    prabhukaran

    Joined:
    May 5, 2019
    Posts:
    17
    In my case the lag was caused by Mono build in player settings. IL2CPP build fixed it. And I disabled the "Raycast Target" for the objects which doesn't need it. This will reduce the EventSystem effort/Update.
     
    theavijitsinha and ExtraCat like this.
  23. TheMonkeysaur

    TheMonkeysaur

    Joined:
    May 2, 2017
    Posts:
    52
    Just wanted to say a big thank you for this post! I've spend an entire day ripping my project apart trying to figure out what the issue was, and this solved it for me.
     
    prabhukaran likes this.