Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

General optimization questions for a 2d mobile game

Discussion in 'General Graphics' started by Aladine, Dec 12, 2017.

  1. Aladine

    Aladine

    Joined:
    Jul 31, 2013
    Posts:
    195
    Hello everyone,

    I have few questions regarding optimizing a 2D game for mobile and i would really like to get few tips from you.

    So the game is an infinite vertical "runner", when i play the game on a Galaxy s7 it runs smooth, 60fps, no lag, all good.

    However when i try the game on a galaxy s5, the game is running on ~48fps and it just feel junky.

    This is the game btw:



    So the first did i tried to get the game to run better on the S5, is disabling the background camera, so all these trees and mountains on the back are gone, and the game run much better now, still not as good as the s7 but its heaps better.

    Here is how am creating the background elements :

    • this spritesheet contain all the elements



    • this is the import settings of it


    • then the BGMaker_P1 object create the background elements




    • And only enable those within a certain distance from the player

    --------

    Are these disabled objects affecting the framerate ? player see just ~6 background elements at once, so i could just create 12 of those and have them reposition themselves as the player move, or at worst case create one single texture and have its material scroll according to player speed.

    But i wan to understand what's going on here ?

    Are these disabled objects effecting the framerate ?

    Is the Background object that is looping through these +800 objects every frame to check if they are within a certain distance to enable them effecting the framerate ?

    --------

    Here is few screenshot from the profiler of the game running on a Galaxy s5 (profiling the device, not in the editor) :
    • Memory

    • Cpu overall

    • Cpu PostLateUpdate (taking 66.2%, why is TransparentGeometry are taking that much ?)

    • Cpu scripts


    So i think there is an issue with the PostLateUpdate thing ?

    Any advice here would be much appreciated.

    Thanks!

    ----------
    EDIT
    Thank you all for your comments, i really appreciate it!
    It turns out everyone was right, i was going through my code and really facepalming myself every 3 lines..., most of it was made over "prototypes" and it wasn't clean **AT ALL**, the lag was mostly caused by "GetComponent" calls and creating/destroying objects, also those +800 background elements were replace with **JUST 8**, same for the platforms, i was creating/destroying 5 every frame, now am just using 6 and just changing the position of the ones that goes off-screen and put them upfront.
    I ended up remaking most of the "in-control" classes (game manager, level manager, player input, etc..), it's really a lesson-learned for me, that attitude of just "get things to work the fast way", if i need a class then just look for it and "Get-it" at that instance without thinking of the best way to do it using the less amount of ressources is possible, and the amount of variables i had was just a "wtf!" moment to me.
    Anyway,
    I spent the last 3 days cleaning it and the game is now running as smooth as it gets on a Galaxy s5.
    Thanks again!
    --------------
    TL;DR : Spaghetti code caused my game to lag.
     
    Last edited: Dec 17, 2017
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Enabling and disabling objects is heavy. You should use a small pool of objects and recycle them. When they aren't in use just move them far away.

    And the overdraw of all those transparent objects is killing you.
     
    Aladine and theANMATOR2b like this.
  3. Aladine

    Aladine

    Joined:
    Jul 31, 2013
    Posts:
    195
    thanks for the reply, u edited my post please check the updated version, although even that i managed to get rid of the lag/stutter now by just cleaning the code, do you have any tips for those transparent objects ? a 2D game is 99% made out of transparent sprites isn't ? we can't have all of our sprites 100% filled, unless transparent means "half visible" ? for example if you have a 128x128 image, with a white circle in the middle and then the rest of the image has no pixels at all, would you call that transparent ?
    also which is more heavier, that 128² impage with a circle, or a 128x128 that's is fully filled but with a gradient that is all white but its opacity goes from 100% to 0% along one of the axes ?

    Thanks!
     
  4. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Look in the manual for info on overdraw.
     
    Aladine likes this.
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Also mobile resolutions can be ridiculously high these days so consider rendering to a lower resolution. Likely it won't look any different at all with that visual style.

    Watch your shaders and control them like a hawk. It's easy for a desktop shader to slip into a mobile game :)
     
    Aladine and theANMATOR2b like this.
  6. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,074
    Here ya go! Hopefully, this helps. :D
    http://www.paladinstudios.com/2012/07/30/4-ways-to-increase-performance-of-your-unity-game/
     
  7. iabhijeets

    iabhijeets

    Joined:
    Aug 14, 2020
    Posts:
    1
    Hey, im just trying to figure out how did you use touch input for your game?
    meaning is it just tap any where on screen and bird jump on next platform automatically OR is left, up, right swipe?