- Home /
Spawning letter tiles evenly across screen
I have been watching the various Unity 2D tutorials available on Lynda as well as on the Unity website and I am trying to create a test game that simply has a grid of letters that are evenly distributed on the screen (think of a word search).
I have my actual letter tiles that were made in Illustrator (128x128 pixels) that I have imported into the project. I also have a Letter
prefab that has a public Sprite
property which is going to be set to a random letter in the alphabet (basically it loads the letter tile by selecting a random letter, and then loading "Letter_X.png" where X is the letter it selected).
This is all fine and works great when I manually drag the Letter
prefabs onto the screen. My issue is that I am trying to get the game to spawn in a grid (of any size) of letters onto the screen and have them all fit nicely.
I have a general idea of what I think I need to do. I know I need to do the following:
If my grid is 10x10
Divide the screen width by the number of horizontal tiles in the grid (10). This will give me the size of each "tile"
Use the result of the above calculation to scale the size of the sprite accordingly.
Loop through each position, instantiating a
Letter
prefab at the current "grid position".
I am currently stuck on the third step. If my original sprite is 128x128 and my Pixels to Units is also set to 128, how would I then convert a location on the screen (which is not in units) to a position in the scene that is at the exact same spot. Further more, how would I make sure that my camera can always show all of the tiles on the screen without having any bleed outside of the edges of the screen?
Your answer
Follow this Question
Related Questions
How prefab the instance works? 1 Answer
instantiating a projectile continually over time 2 Answers
Why does transform.Rotate not move the sprite and BoxCollider2D together? 1 Answer
Instantiate always at the same position (0,0,0) 1 Answer
Adding prefabs to a list or an array from a folder and instantiating them. 2 Answers