Best way to create 2D tiled sprite. (UnityScript)
I've created a modified version of this answer here using UnityScript on how to make a 2D tiled sprite. http://answers.unity3d.com/questions/599263/how-to-make-2d-sprite-tiled.html
// 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;
}
Answer by Statement · Oct 28, 2015 at 11:23 PM
The script is creating a 2d tilemap.
var h : int = Mathf.Round(sprite.bounds.size.y);
for (var i : int = 0; i*spriteSize_wu.y < h; i++) {
I haven't tested the code or anything but the above looks dodgy. It'll round the sprites bounds height to the nearest integer. Then it will repeat on for as many spriteSize_wy.y fits in that height. Eh. My brain died a little. Anyway, it means that if the bounds height was 0.5, it would round it up to 1. If spriteSize_wu.y was 0.5, it would run it twice. 0*0.5 = 0, so 0 < 1. Then the next iteration 1*0.5 = 0.5, so 0.5 < 1. The third iteration, 2 * 0.5 = 5, so 1 == 1 and not < 1, meaning the loop would stop there. It would do 2 iterations. I have no idea why that round is in there, but it doesn't seem to do what you want.
If you want to make a strip, you could remove the loop and just set y to 0. Or you could try using the real height without any rounding.
var h = sprite.bounds.size.y;
@Statement thanks for pointing that out, now I understand the script better, and realize this is indeed perfect for creating a 2D tilemap, but also by setting y to 0 and removing the loop it did exactly what I needed to do.
Your answer
Follow this Question
Related Questions
Tiling Sprites and performance sprites vs. GameObjects 0 Answers
Updating tilemap tile sprites? 0 Answers
Sprites get horribly cluttered when using Tile palette 1 Answer
Making my variable global. 1 Answer
Fix tilemap cell size 0 Answers