• 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 Hydropulse17 · Jan 26, 2017 at 03:09 AM · instantiate prefabmultidimensional arrayviewporttoworldpoint

Instantiating grid of objects starting at the bottom left of the viewport

alt textI need to instantiate prefabs(Tiles) in rows and columns like a grid. I used a multidimensional array with 3 elements in two dimensions and, using nested for loops, I instantiated 1 prefab at each "coordinate" in world space like:

 0,0 0,1 0,2
 1,0 1,1 1,2
 2,0 2,1 2,2

Of course this means my grid of objects starts at 0,0 in world space and moves up to the right, but I need them to start at the bottom left corner of the screen. I knew the solution had something to do with converting the index of each dimension of the array to view port space, so I did that and got them instantiating from the bottom left, but they're no longer adjacent, which I expected. Which probably means some maths need to be done but IDK.

If you can tell me what math to do that would be great, or if you have another solution...

I don't know if it's relevant, but each sprite used for the prefabs I'm instantiating is 256x256 pixels and they're using 256 pixels per unit so that one sprite is 1x1 unit(I won't be using any physics in this project).

Attached is a picture of what I'm getting, versus a Photoshop of what I want.

         for (int i = 0; i < grid.GetLength(0); i++)
         {
             for (int j = 0; j < grid.GetLength(1); j++)
             {
                 grid[i, j] = Instantiate(tile, Camera.main.ViewportToWorldPoint(new Vector3(i, j, 0)), Quaternion.identity);
             }
         }


untitled-1.png (37.2 kB)
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
0
Best Answer

Answer by Bunny83 · Jan 26, 2017 at 02:13 PM

Well, if you want to lay them out on the screen you don't want to use the Viewport space. Viewport coordinates goes from 0 to 1. So 0,0 is the bottom left corner and 1,1, is the top right corner of the screen. Those are also square-coordinates while the screen itself is usually a rectangle so even when calculating the right scale you have to deal with the aspect ratio as well.

If each tile is one world unit wide, just place them in worldspace. You can use ViewportToWorldPoint to determine the "first point". Something like that:

 Vector3 start = Camera.main.ViewportToWorldPoint(new Vector3(0f, 0f, 0f)) + new Vector3(0.5f, 0.5f, 0f);
 
 for (int i = 0; i < grid.GetLength(0); i++)
 {
     for (int j = 0; j < grid.GetLength(1); j++)
     {
         grid[i, j] = Instantiate(tile, start + new Vector3(i, j, 0f), Quaternion.identity);
     }
 }

I added "0.5" on the x and y axis since it looks like your prefabs origin is in the center. Since you said a tile is 1 unit wide moving it 0.5 will place it right in the lower left corner. Now you just add "whole units" to each axis (since integers are whole numbers it should work out just right). If for some reason your tiles are larger or smaller you just have to multiply "i" and "j" with your scaling factor. If you want a little space between the tiles you can do

 + new Vector3(i*(1f + 0.1f), j*(1f + 0.1f), 0f), 

Here the "1f" would be the scale / worldsize of one tile and the "0.1f" would be the space between those tiles

Comment
Add comment · Show 1 · 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 Hydropulse17 · Jan 26, 2017 at 03:02 PM 0
Share

That worked beautifully, and I appreciate that 0.5f offset too. I was going to add each tile as a child to an empty game object to fake an origin, but your way is cleaner.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

63 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Projection of viewport on plane 2 Answers

2 dimensional grid of GameObjects, access in different GameObje 0 Answers

How to Create Dynamic Multidimensional Array? 1 Answer

NullReferenceException inside clone script 1 Answer

Prefab error while using Network.Instantiate 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges