• 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 Habakuk · Nov 10, 2014 at 01:17 PM · gameobjects

How to reduce the number of gameobjects used for representing building blocks

I'm developing a 2D building game for mobile devices where it is possible to spawn building blocks by touching the screen. At the moment each of those blocks is represented by a gameobject that has a sprite renderer and a physics collider attached. The problem is that I need up to 20000 of these blocks in the scene at the same time. That might sound a bit much but there are some restrictions that ensure that the game still performs pretty well. So my problem isn't performance related in terms of physics or rendering, it's just the mere number of gameobjects that gets me into trouble. It doesn't matter if I use my building blocks or just empty gameobjects, after instantiating several thousands of them every additional instatiation takes forever.

For that reason I'm trying to cut down on the number of gameobjects used in my game by combining some blocks into bigger gameobjects. Unfortunately I can't figure out how to get this right.

I read that in Minecraft-like games several thousand blocks are combined into chunk meshes (using one mesh collider) that can be rebuilt whenever a block is deleted but as far as I can tell that is not possible using sprites. Also I think it might not be sensible to follow that approach since my blocks are not necessarily aligned next to each other and therefore there would have to be some holes in the mesh. Has anybody tried to achieve something like this, or do you have any ideas on how i might approach the problem differently?

Any suggestions would be appreciated!

Comment
Add comment · Show 2
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 Linus · Nov 10, 2014 at 02:26 PM 0
Share

Build it, then worry about performance

avatar image Habakuk · Nov 10, 2014 at 02:53 PM 0
Share

I already did that ;) The game is running fine but as soon as my game scene consists of several thousand objects (depending on the used device of course) the problem occurs.

1 Reply

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

Answer by TonyLi · Nov 10, 2014 at 02:47 PM

This is one scenario where I'd recommend addressing efficiency from the start, since this will end up being a fundamental design issue, not simply an optimization issue. 20,000 blocks in a scene will never be practical. Here are some suggestions:

  1. Combine blocks. If you have a 3x3 pattern of blocks, replace it with a single 3x3 GameObject. If you need to break them apart at some point, replace them with the individual blocks only at that point.

  2. Use space partitioning, such as a quadtree, and only spawn GameObjects within the player's immediate area. I write "spawn" instead of "instantiate" because you should use object pooling for this. So the full map will be in memory (an abstraction), not in the scene.

Also, as a final suggestion, consider posting open-ended questions like this on the Unity Forums instead of Answers. In Answers, you'll get better responses for cut-and-dry questions. The Forums are better for discussions.

Comment
Add comment · Show 3 · 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 Habakuk · Nov 10, 2014 at 03:43 PM 0
Share

Thanks for your suggestions! What you mentioned in your first point is what might be sensible in my situation but I don't know if it's even possible. I guess my original text boils down to the question if and how I could combine several gameobjects that each contain a sprite renderer and a physics collider and that are not perfectly aligned next to each other (meaning that there will be some holes in the resulting mesh/collider). I would like to know if it makes sense to put time and effort into this endeavor or if I should just abandon the idea of combining blocks.

avatar image TonyLi · Nov 10, 2014 at 03:57 PM 0
Share

Yeah, you're going to have a hard time with the first point if the shapes are irregular and unpredictable. You could try to stitch meshes together into a single mesh, but in the end I think it wouldn't be worth the significant extra complexity.

Point two will probably give you much more bang for the buck.

avatar image Habakuk · Nov 11, 2014 at 09:27 AM 0
Share

Your answer might not have been what I hoped to hear but what I needed to hear. It helped me decide how to go on with my project. I'll wait a few more hours and if nobody comes up with an unexpected genius solution I'll mark your answer as accepted.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

sync color change of objects c# 1 Answer

toggle gameObjects with a keyPress 1 Answer

What is the Performance implications of stacking gameobjects and switch between them for character customization. 1 Answer

Combine two or more gameobjects with different rigidbody values 0 Answers

How do I check if a boolean variable is true on a group of gameobjects with the same script? 1 Answer


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