Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GameObserver Input.touches GC Alloc

Discussion in 'Unity Analytics' started by skateborden, May 14, 2015.

Thread Status:
Not open for further replies.
  1. skateborden

    skateborden

    Joined:
    Apr 1, 2009
    Posts:
    35
    We are currently in the process of optimizing the iOS version of our game. Our game involves dragging objects around the screen and we noticed in the profiler that whenever a finger is touching the screen GameObserver.Update() is allocating 56B of garbage per frame. Digging around I discovered that GameObserver is part of Unity Analytics, and checking out GameObserver.Update() in MonoDevelop's Assembly Browser this is what's going on:

    Code (CSharp):
    1. if (Input.touchCount > 0 && Input.touches [0].phase == TouchPhase.Began)
    2. {
    3.     this.OnClick ();
    4. }
    This is something we also saw in our own code recently, and is noted in the documentation, Input.touches allocates but using Input.GetTouch instead does not. It would be great if GameObserver could use Input.GetTouch(0) and not allocate every frame a finger is touching the screen. Also, I'm curious why Unity Analytics is listening for all clicks?
     
  2. mbirk

    mbirk

    Unity Technologies

    Joined:
    Apr 25, 2014
    Posts:
    18
    Hi @skateborden,

    Thanks for looking into this! I agree we should not be generating garbage on touch events. Perhaps we can simply switch to the GetTouch api that you pointed out.

    As for why we are collecting this data, we once had a feature called the "Intensity Score", a component of a more general "Engagement Score". The Intensity Score included a metric which is the number of clicks, touches, and keypresses (we have never logged individual clicks or keypresses).

    However, the Intensity Score is of dubious value and we are currently not computing or surfacing this metric. So perhaps we will just remove that part of the GameObserver altogether.

    mcb
     
  3. skateborden

    skateborden

    Joined:
    Apr 1, 2009
    Posts:
    35
    @mbirk Thanks for the quick reply. Other than this one issue I'm really enjoying Unity Analytics so far, it has the easiest and smoothest onboarding I've seen in an analytics package. Keep up the good work!
     
  4. SmallGiantGames

    SmallGiantGames

    Joined:
    Oct 2, 2013
    Posts:
    2
    This issue still persists in the latest patch version. We're not even using analytics, but we rely on the new IAP functionality that forces analytics on, so now the code snippet above is generating garbage on every frame.

    If you want to check the state of touch input, you should really use Input.GetTouch(0), which won't cause any allocation. Honestly, the Input.touches API should be removed altogether, because it's just begging to be misused...
     
  5. mhalttu

    mhalttu

    Joined:
    Mar 13, 2013
    Posts:
    30
    @mbirk @JoshPeterson @Mantas Puida Hi guys! There is one thing I don't understand, and I wonder if any of you guys would be kind enough to explain it to me.
    1. There are cool PC and console games made with Unity, but I believe most of your customer base is developing for iOS and Android.
    2. Pauses caused by garbage collection are one of the most common reasons for frame drops in mobile games made with Unity, and you have been doing big changes to improve the situation.
    3. This issue causes 100 bytes of garbage on every frame. It's not much, but it does add up.
    4. Fixing this issue requires one minor, safe change on one line in the Unity code base.
    Am I misunderstanding something? If all of the statements above are true, how is it possible that the issue has not been resolved in one year? What am I missing?
     
  6. erika_d

    erika_d

    Unity Technologies

    Joined:
    Jan 20, 2016
    Posts:
    413
    Hi @mhalttu,

    Thanks for bringing this back to the forefront! Apologies that it slipped through the cracks, but we have now created the fix for the issue. It will go through testing next and should be released in an upcoming 5.3 patch soon! Thanks again!
     
  7. mhalttu

    mhalttu

    Joined:
    Mar 13, 2013
    Posts:
    30
    @erika_d A big thank you for taking care of the issue!
     
    erika_d likes this.
  8. peci_scx

    peci_scx

    Joined:
    Sep 7, 2015
    Posts:
    3
    february 2021, unity 2019.4.13f1,
    Input.touches still produces 100bytes of garbage.

    plse fix.
     
  9. peci_scx

    peci_scx

    Joined:
    Sep 7, 2015
    Posts:
    3
    Ah sorry, my fault. Used the wrong interface.
    for all others coming here: the solution is to use Input.GetTouch(index) instead of Input.touches.
     
    Bomadeno likes this.
Thread Status:
Not open for further replies.