Search Unity

Optimizing for mobile

Discussion in 'Scripting' started by denwik, Aug 6, 2019.

  1. denwik

    denwik

    Joined:
    Sep 22, 2017
    Posts:
    27
    Hi!
    I'm developing a space shooter. I'm having problems understanding what codes to use in specific situations, i mean wich are the most effective for mobile.

    A specific problem, my bullet deactivates right now in the top of the screen by coliding with a "top screen killer" trigger basicly. Then i found this code:

    Code (CSharp):
    1. void OnBecameInvisible()
    2.     {
    3.         gameObject.SetActive(false);
    4.     }
    How do i know what is most processing effective? :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    That's an easy answer! The first method that you implement successfully and it turns out to be fast enough to ship the game, THAT is the one to use.

    Any other choice indicates that you're more interested in writing code than shipping games, and I have no replies than can be helpful in that case.
     
    denwik likes this.
  3. denwik

    denwik

    Joined:
    Sep 22, 2017
    Posts:
    27
    I get you're answer and i like it a lot! The problem is that i just wanna write code that is efficient cuse the game runs amazingly but acording to xcode for ios developing it eats my battery like i eat pocorn during Titanic. And there is a lot of small fixes i wanna do just to make the experience better for users.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Okay, that's funny!

    Seriously, Unity is just heavier to run presently than an equivalent native XCode app.

    If you doubt me, make an empty project, build it to phone with a single empty scene, no scripts no nothing.

    Here is my results running on a 2017 iPad Air, a project built in Unity 5.6.6:

    u566.png

    And here it is in Unity 2018.4.2, the highest version I have on this computer:

    u2018.4.2.png

    Unity is working on it, they have made a teeny bit of progress, and it has a ways to go.

    I can however state with confidence that your choice of how to destroy a player-fired bullet at the top of the screen will not impact this in any meaningful way.
     
  5. denwik

    denwik

    Joined:
    Sep 22, 2017
    Posts:
    27
    Hi! Thanks for sharing this! I had my suspicion that this was the case
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Since you deactivate (and not destroy) the objects, i'm assuming you are already using object pooling. If not, definitely have a look at that for mobile. Also, maybe have a look at the new DOTS architecture (ECS, Jobs, Burst) Unity offers, which is a highly performant (and thus energy efficient and useful for mobile) way of writing code. If you use many entities, like bullets, enemies, ... then this may be for you. However, be aware that getting used to it may take weeks or even months, as it is not object oriented anymore and thus a lot less straight forward and definitely nothing i'd recomment to a beginner. However, converting small pieces of a project at a time should be possible if you really want to. Like said before tho, if the game runs well i would not over-optimize it. Optimizing can eat a lot of time without any real results to show for it, so if there is no tangible problem dont stress it, and if there is then tackle that specifically.
     
    denwik and Kurt-Dekker like this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Excellent point, and optimizing will almost certainly make the code more brittle and difficult to understand and extend.

    Never optimize anything that the Profiler didn't show you was problem code.

    Never optimize speculatively. That's a SUPER common destructive engineering behavior.