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. Dismiss Notice

Memory and performance management

Discussion in 'Scripting' started by matanrad, Oct 23, 2014.

  1. matanrad

    matanrad

    Joined:
    Aug 17, 2014
    Posts:
    5
    Hello, I want to make a small script that a user can type a number (between 1 to 10 million) and click on a button and then he will be moved into a first person game and in the game, the script will draw cubes on the screen, the amount of cubes will be the number the user wrote. Now this is a first person game so not all the cubes will be rendered at one but I still have a lot of questions... First of all, the first way I thought of going for this is just a for loop that creates a gameobject for each cube, but 10 million game objects? I thought of making 1 big model which contains a lot of cubes (I heard in unity there is a max of 65k vertices) and put that on but the problem is I want the player to have an option to change the cube's distance from eachother.
    How do you think is my best way to go about this?
    Thank you very much
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Don't allocate 10 million objects. You need a way to abstract this. Maybe you could have 10 million elements in a table, but they shouldn't be allotted GameObjects 1:1. Even better than 10 million elements would be some kind of area subdivision, such as an octree. For example, if the top half of your world is all blocks and the bottom is all empty, then your octree will only have 2 elements instead of 10 million.

    Instantiate actual GameObjects only near the player. Use a pool for this, so you can reuse GameObjects instead of creating and destroying them.
     
  3. matanrad

    matanrad

    Joined:
    Aug 17, 2014
    Posts:
    5
    I think I understood what you mean, please confirm that this is true: I need to divide my world into areas, and only make a small amount of cubes, and move these cubes to the area the player is in so it will look like there are everywhere he goes so it actually looks like there are 10 million cubes
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Yes. I recommend two intermediate steps:

    1. Make a very small world where you create all of the cubes. Get some basic gameplay working in this environment.

    2. Read about octrees (see the link above) or some other area subdivision technique, and play with it in a few test scenes.

    Then you can put both of these together to make what you described.
     
  5. matanrad

    matanrad

    Joined:
    Aug 17, 2014
    Posts:
    5
    Ok, thank you very much