I’ve created a modified version of this answer here using UnityScript on how to make a 2D tiled sprite. How to make 2d sprite tiled? - Questions & Answers - Unity Discussions
// using UnityEngine;
// using System.Collections;
#pragma strict
public var gridX : float = 0.0f;
public var gridY : float = 0.0f;
var sprite : SpriteRenderer;
function Awake () {
sprite = GetComponent.<SpriteRenderer>();
// if(!GetSpriteAlignment(gameObject).Equals(SpriteAlignment.TopRight)){
// Debug.LogError("You forgot change the sprite pivot to Top Right.");
// }
var spriteSize_wu : Vector2 = new Vector2(sprite.bounds.size.x / transform.localScale.x, sprite.bounds.size.y / transform.localScale.y);
var scale : Vector3 = new Vector3(1.0f, 1.0f, 1.0f);
if (0.0f != gridX) {
var width_wu : float = sprite.bounds.size.x / gridX;
scale.x = width_wu / spriteSize_wu.x;
spriteSize_wu.x = width_wu;
}
if (0.0f != gridY) {
var height_wu : float = sprite.bounds.size.y / gridY;
scale.y = height_wu / spriteSize_wu.y;
spriteSize_wu.y = height_wu;
}
var childPrefab : GameObject = new GameObject();
var childSprite : SpriteRenderer = childPrefab.AddComponent.<SpriteRenderer>();
childPrefab.transform.position = transform.position;
childSprite.sprite = sprite.sprite;
var child : GameObject ;
var h : int = Mathf.Round(sprite.bounds.size.y);
for (var i : int = 0; i*spriteSize_wu.y < h; i++) {
var w : int = Mathf.Round(sprite.bounds.size.x);
for (var j : int = 0; j*spriteSize_wu.x < w; j++) {
child = Instantiate(childPrefab) as GameObject;
child.transform.position = transform.position - (new Vector3(spriteSize_wu.x*j, spriteSize_wu.y*i, 0));
child.transform.localScale = scale;
child.transform.parent = transform;
}
}
Destroy(childPrefab);
sprite.enabled = false; // Disable this SpriteRenderer and let the prefab children render themselves
}
I’m not the best at doing this and am still very new to Unity, so it’s working but with one caveat: It’s creating two rows of these tiles instead of one:
I’m basically trying to generate portions of the platformer to avoid having to duplicate a gameobject over and over manually.
So, am I not understanding quite what is being done with the code, or am I missing something? It’s not a deal breaker, and with the camera I am using I’m sure I could find a work around, but I’m hoping someone will point out my mistakes and aid me in the right direction.
UPDATE OCT 29
The code above works perfectly for creating a 2D tilemap as statement pointed below and here is the adjustment if you want basically use this on only one row:
var child : GameObject ;
var w : int = Mathf.Round(sprite.bounds.size.x);
for (var j : int = 0; j*spriteSize_wu.x < w; j++) {
child = Instantiate(childPrefab) as GameObject;
child.transform.position = transform.position - (new Vector3(spriteSize_wu.x*j, 0, 0));
child.transform.localScale = scale;
child.transform.parent = transform;
}