Created
July 16, 2014 04:48
-
-
Save ashblue/05657e218c7fdf3a2d54 to your computer and use it in GitHub Desktop.
Unity background tiling script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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