Tuesday, January 27, 2015

Unity 2D Units

I'm building a tile system to display a grid of sprits in my game. I'm old school and used to just using pixels so a 32 pixel wide sprite is 32 pixels wide. so when I do this to create a strip of sprites
(a public method variable)public Sprite tile_plain; int i; for (i=0;i<25;i++) { GameObject tile = new GameObject(); tile.AddComponent(); tile.GetComponent().sprite = tile_plain; tile.transform.position = new Vector2((i * tile_plain.rect.width), 5f);

Then only one sprite element shows up on the screen and the others, while they are there are off the screen. To do this, you have to divide by the pixels per unit identified in the sprite.
(a public method variable)public Sprite tile_plain; int i; for (i=0;i<25;i++) { GameObject tile = new GameObject(); tile.AddComponent(); tile.GetComponent().sprite = tile_plain; tile.transform.position = new Vector2((i * tile_plain.rect.width)/tile_plain.pixelsPerUnit, 5f);
This was a good Stackoverflow post (Unity 4.3 - understanding positions and screen resolution, how to properly set position of object?) on the topic.

No comments: