Thursday, January 29, 2015

Unity2D script to programatcally add a sprite

I'm create a grid of tiles. The tiles are sprites and I'm laying them out programmatically. Here is the code to generate one of the tiles.

public Sprite tile_plain; // assign this in unity by dragging a sprite to it void Start() { 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); }

No comments: