Skip to content

Instantly share code, notes, and snippets.

@ashblue
Created July 16, 2014 04:48
Show Gist options
  • Save ashblue/05657e218c7fdf3a2d54 to your computer and use it in GitHub Desktop.
Save ashblue/05657e218c7fdf3a2d54 to your computer and use it in GitHub Desktop.
Unity background tiling script
using UnityEngine;
using System.Collections;
// Must be applied to an empty object wrapping a sprite image
public class Tiler : MonoBehaviour {
Hashtable tileCollection = new Hashtable(); // X and Y coordinates formatted as x0y0 relative to the starting tile
GameObject originTile;
public bool repeatX = true;
public bool repeatY = false;
[Tooltip("How far in advance to render a new background piece")]
public int offsetX = 2;
public int offsetY = 2;
[Tooltip("Render each connected tile in reverse on the corresponding axis")]
public bool reverseScaleX = false;
public bool reverseScaleY = false;
// Use this for initialization
void Awake () {
if (transform.childCount != 1) {
Debug.LogError("Repeating backgrounds must have one child element");
return;
}
AddTile(0, 0);
foreach (Transform child in transform) {
originTile = child.gameObject;
originTile.AddComponent("TilerChild");
}
// Add all props to the child object inside the tiling script
TilerChild tileChild = originTile.GetComponent<TilerChild>();
tileChild.tileApi = this;
tileChild.repeatX = repeatX;
tileChild.repeatY = repeatY;
tileChild.offsetX = offsetX;
tileChild.offsetY = offsetY;
tileChild.reverseScaleX = reverseScaleX;
tileChild.reverseScaleY = reverseScaleY;
}
public void AddTile (int x, int y) {
tileCollection.Add("x" + x + "y" + y, true);
}
public bool IsTile (int x, int y) {
return tileCollection.ContainsKey("x" + x + "y" + y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment