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
0
Question by Setzer22 · Dec 26, 2012 at 03:32 PM · gameobjectinstantiatearraymap

Instantiate large block maps

Hello everyone

I'll try to keep it simple so I won't annoy you with the details.

In my game, i have a tile-based map system, much like it's done in minecraft, but only one "layer" of blocks. I read the map from text files and create a map class, the map class has a jagged array (array of arrays, or just matrix) in which I store every block in the map. For each "cell" in the matrix I store information for that block, an ID and a refference to the gameObject model the tile should look like (as well as some more data not meaningful for what I am asking).

So the point is, I want to know what is the best way (in terms of efficiency, as well as simplicity) to instantiate the map I've got saved as "data" in a class to my 3d world in unity.

What first came into my mind was to iterate through all the cells in the matrix, instantiating one by one each block (with the Instantiate() method) in the matrix, but I keep thinking there must be a much better and faster way to do this. Am I wrong?

I'd like some advice on that. I'm not asking for any code examples.

Thank you very much!

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

2 Replies

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

Answer by robertbu · Jan 24, 2013 at 01:11 PM

One solution for this kind of problem is to use a base tile mesh with a single look and then put a plane on the face of each tile to display an image for the unique aspects of the tile. Think of a playing card where you create one game object that is a blank card, and then have a plane display the texture representing a particular card. The planes use UV coordinates into a texture atlas for display, so all of your 10 x 10 tiles would only use 2 draw calls.

Unity has Texture2D.PackTextures() for creating an atlas, though for something as regular as fixed sized planes, an atlas can be created in Photoshop by hand. Because of the number of triangles, the plane that comes with Unity is not a good choice. Code samples of building a plane mesh can be found if you search Unity Answers, but even easier is to use the CreatePlane editor script. You would still have to set the UV coordinates for each plane in code.

We the EZGUI interface package. It provides tools to automate this process, so we don't have to deal with UV coordinates. Their PackedSprite class allows the author to associate a stack of textures with a single plane. At runtime, any of the textures packed can be selected. Since all the textures are in an atlas, changing at runtime does not increase the drawcalls. I'm sure other interface packages have similar solutions. So for your map, you could create a packed sprite that has the ability to display any map element. Then at runtime based on your text data, just select the desired element.

If the number of games objects causes you a problem (there would be 200 for a 10x10 mesh), you could combine meshes (Mesh.CombineMeshes()), or author the tile base as a single mesh and put the planes on top.

Comment
Add comment · 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
0

Answer by Setzer22 · Jan 24, 2013 at 11:40 AM

Well, just for the sake of this being answered, I'll auto-answer myself. Any more answers or opinions are welcome as well (I'm not going to mark mine as answered unless nobody else posts here).

As I thought, Instantiating is not an option. Even in a 10x10 map it may cause performance issues on the slower computers (and high number of draw calls as well). What I was looking for was procedural mesh generation. That's more literally: creating the mesh through code. So, instead of a lot of GameObjects, representing tiles, I had to found a way to create a mesh from scratch (and through code). Using submeshes and texture atlases (still don't know the plural for atlas and it seems I'm to lazy to look it up), and nicely placing the UV coordinates, generating a map through code with different tile types is possible. And with one single mesh, the draw calls were greatly reduced.

There might be other sollutions to this. It's more a design issue than an implementation one, so, as I mentioned earlier, any other answers are welcome.

Comment
Add comment · 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

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Different array value at different times 1 Answer

How to Instantiate a Different Game Object After Getting to the End of an Array? 1 Answer

Instantiate Game Object on Array of Transforms 1 Answer

How can I Instantiate a GameObject and then alter an array that is attached to that GameObject 1 Answer

How to randomly spawn three non repeating gameobjects from an array? 2 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