Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by johnkol · Aug 06, 2010 at 12:42 AM · performanceoptimizationspeedgridinitialization

Need help optimizing grid initialization.

Hello everyone,

I am working on a game which uses a 40 X 40 grid. Each cell in the grid is made up of GridCell objects.

class GridCell
{
    public Vector3 cell_pos;
    public bool cell_status;
    public GameObject cell_go;
}

And here is how I am initializing the grid currently,

void CreateGrid() { _grid = new GridCell[Config.GRID_ROW_COUNT, Config.GRID_COL_COUNT];

 for (int i = 0; i < Config.GRID_ROW_COUNT; ++i)
 {
     for (int j = 0; j < Config.GRID_COL_COUNT; ++j)
     {
         _grid[i, j] = new GridCell();
         _grid[i, j].cell_pos = new Vector3(Config.GRID_CELL_WIDTH / 2 + i * Config.GRID_CELL_WIDTH, 0.05f, (Config.GRID_CELL_HEIGHT / 2 + j * Config.GRID_CELL_HEIGHT));
         _grid[i, j].cell_status = false;
         _grid[i, j].cell_go = Instantiate(Resources.Load("GridCell")) as GameObject;
         _grid[i, j].cell_go.renderer.enabled = false;
         _grid[i, j].cell_go.transform.position = _grid[i, j].cell_pos;
     }
 }

}

As you can see I am initializing 1600 GameObjects here and this causes my load time to shoot up. Is there anyway I can optimize this initialization. It will be really helpful.

Thank you!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by cncguy · Aug 06, 2010 at 12:55 AM

I had this same issue. As far as I could see if you have to create a gameobject then you have to create a gameobject and suffer the consequences. All I have to suggest is putting a yield statement in the inner or outer loop. This will stop your game 'freezing' as it loads but will have the appearance of creating the grid over a number of frames. You could however make this a 'feature' Of the start of the level to mitigate the issue. I.e. A sort of materialization of your grid fanning out from some point.

[edit] you are calling Resources.load every object. Take that out of the loop and create a reference to it. Then instantiate via the reference.Also use the full constructor for instantiate to save a call to transform every iteration. Eg. Something like:

void CreateGrid() { _grid = new GridCell[Config.GRID_ROW_COUNT, Config.GRID_COL_COUNT];

GameObject gridObj =Resources.Load("GridCell")) as GameObject;

for (int i = 0; i < Config.GRID_ROW_COUNT; ++i) { for (int j = 0; j < Config.GRID_COL_COUNT; ++j) { _grid[i, j] = new GridCell(); _grid[i, j].cell_pos = new Vector3(Config.GRID_CELL_WIDTH / 2 + i Config.GRID_CELL_WIDTH, 0.05f, (Config.GRID_CELL_HEIGHT / 2 + j Config.GRID_CELL_HEIGHT)); _grid[i, j].cell_status = false; _grid[i, j].cell_go = Instantiate(gridObj,_grid[i, j].cell_pos, Quaternion.identity)as GameObject; _grid[i, j].cell_go.renderer.enabled = false; } } }

sorry about the formatting - tricky on the iPhone. Ps I code in jscript so may not be 100% syntactically correct.

[edit 2]

make sure your grid has it's renderer enabled by default and remove the call that enables the rendere for each cell.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image johnkol · Aug 06, 2010 at 02:24 PM 0
Share

Thanks a lot cncguy! Really appreciate it. Though there was no significant performance improvement (and I understand that is the drawback with initializing so many GameObjects) I like your idea to mitigate the problem. Add some flashy effects and I think I can get away with it :) Thanks again!

avatar image cncguy · Aug 06, 2010 at 10:30 PM 0
Share

No worries - any time.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

What's killing my rendering performance? (pic included) 1 Answer

First time a method is executed is ~4 times slower 0 Answers

What's a good method for measuring shader performance? 0 Answers

(2 hidden skinned meshes) or (1 extra draw call) for character props? 2 Answers

Memory management and C# do's and don'ts? 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges