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

Is this efficient? Help me optimize if not please.

Discussion in 'Scripting' started by NulIl, Aug 22, 2017.

  1. NulIl

    NulIl

    Joined:
    Aug 12, 2017
    Posts:
    12
    Code (csharp):
    1.  
    2.     private void generateFloor()
    3.     {
    4.         for (int i = 0; i < floorHeight; i++)
    5.         {
    6.             for (int j = 0; j < floorWidth; j++)
    7.             {
    8.                 Vector3 offset = new Vector3(j, i, 0);
    9.                 var newTile = Instantiate(getRandomTile(), getStartCords() + offset, Quaternion.identity, transform);
    10.                 newTile.name = "Tile_" + count;
    11.                 count++;
    12.             }
    13.         }
    14.     }
    15.  
    /Title

    I use this to generated a floor with random tiles (Which are just different shades of the same thing)
    It works perfectly, but i wanna know if this is an efficient way of handling the situation.
     
  2. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    That looks fine to me. What do your
    Code (csharp):
    1. getRandomTile()
    and
    Code (csharp):
    1. getStartCords()
    methods look like?

    Also, not to nit-pick but
    Code (csharp):
    1. getStartCords()
    should probably be spelled
    Code (csharp):
    1. getStartCoords()
     
    NulIl likes this.
  3. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    175
    Does this happen only once per level? Because if it's the case then I'd like to draw your attention to the fact that, since it will only happen once, its' inefficiency bears little relevance.

    I don't see anything that would stand out as inefficient here, this excerpt of code is too small to be massively faulty. There may possibly be perverse ways of making it more efficient, but this should be good enough.
     
    Kiwasi and NulIl like this.
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Having every tile be a separate object is not efficient, no.

    --Eric
     
    Kiwasi and NulIl like this.
  5. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,591
    The best partner you can ask, whether something is efficient, is a Profiler. Once you're familiar using a Profiler, there is no need to guess or ask these type of questions anymore, because you can use such Profiler to measure performance.

    Here are a few videos that profile help to get started with the Unity Profiler.



     
    NulIl likes this.
  6. NulIl

    NulIl

    Joined:
    Aug 12, 2017
    Posts:
    12
    unfortunately I am just learning, so I don't have the pro version. But thanks for commenting and sharing knowledge regardless.
     
  7. NulIl

    NulIl

    Joined:
    Aug 12, 2017
    Posts:
    12
    Code (csharp):
    1.  
    2.  //Gets the coordinates of Bottom left corner
    3.     private Vector3 getStartCoords() {
    4.         return new Vector3(-(floorWidth / 2f - 0.5f),-(floorHeight / 2f - 0.5f),0);
    5.     }
    6.     //Gets a random tile sprite for a lively floor
    7.     private GameObject getRandomTile()
    8.     {
    9.         int index = Random.Range(0, tileSet.Length);
    10.         return tileSet[index];
    11.     }
    12.  
    i fixed coords thanks for the heads up :)
     
  8. NulIl

    NulIl

    Joined:
    Aug 12, 2017
    Posts:
    12
    I honestly can't think of any other way to generate custom height x width floor without having every tile be aa separate object..is there a way to combine all the tiles into one gameobject after the code is done instantiating?
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Don't instantiate objects, use the Mesh class.

    --Eric
     
    NulIl likes this.
  10. NulIl

    NulIl

    Joined:
    Aug 12, 2017
    Posts:
    12
    I am kinda new, would you mind giving more details, you don't have to do a step by step guide, maybe link me to a tutorial if there is one. Thanks for helping.
     
  11. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    You don't need the Pro version to use the Profiler. It's under Window -> Profiler.
     
    TaleOf4Gamers, Peter77 and NulIl like this.
  12. NulIl

    NulIl

    Joined:
    Aug 12, 2017
    Posts:
    12
    You sir just don't know how much this is gonna help me. Thank You from the bottom of my heart.
     
    makeshiftwings likes this.
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Profiler has been part of personal for a couple of years now. The only features you don't get are cosmetic, namely dark skin and removing the splash screen.

    Do you actually need it?

    @Eric5h5 is certainly right, using a single GameObject for the floor is much more efficient then using a thousand tiles. But it will take you more work to understand, develop, and code. If an inefficient system performs well enough there is often merit to simply moving on.

    If you are actually having performance problems, I would start with the Mesh class. The basic idea is to build a single mesh for your floor, and have the uvs point to different spots on a single tile texture.
     
    NulIl likes this.
  14. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    You Dont get cloud services with out a plus or pro license (Cloud Build, Exception Reporting, Ads, Analytics etc)

    But as far as the player and editor goes yeah, the only difference is splash screen and editor themes
     
    NulIl likes this.
  15. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You get some of them. Cloud Build, Ads, Analytics, Collaborate and MultiPlayer all work on personal. Performance reporting doesn't.

    The personal services are slightly limited, but you really don't really need the plus/pro level services until your game is deployed and making money.
     
    NulIl likes this.
  16. NulIl

    NulIl

    Joined:
    Aug 12, 2017
    Posts:
    12
    Thanks for all the help guys, I have done some research with the profiler and it seems that the game is running very smooth, just a bit of fps drops during generation (but since all of this is gonna be done at the beginning of each level) i have decided to just go with it hopefully I won't run into problems later.

    Again Thank you everyone for the great help.