SpriteBar.cs 1.14 KB
Newer Older
Luke Smith's avatar
Luke Smith committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class SpriteBar : MonoBehaviour {

	public GameObject emptySprite;
	public RawImage[] spriteBar;

	private GameObject highliter;
	private int highlited;

	private Scrollbar scroll;
	private int startIndex;

	private Sprite[] sprites;

	void Start () {
	
		highliter = GameObject.Find("Highlite");
		sprites = Resources.LoadAll<Sprite> ("Tiles/");
		scroll = GetComponent<Scrollbar> ();

		UpdateSpriteBar ();
	}

	public void UpdateSpriteBar () {

		startIndex = (int)(scroll.value * (float)sprites.Length);
		startIndex -= startIndex % 8;

		for (int i = 0; i < spriteBar.Length; i++) {
			spriteBar [i].texture = null;
		}

		for (int i = 0; i < spriteBar.Length && i + startIndex < sprites.Length; i++) {
			spriteBar [i].texture = sprites[startIndex + i].texture;
		}
	}

	public void SpriteClicked (int i) {

		highlited = i;
		highliter.transform.position = spriteBar [i].transform.position;
	}

	public Sprite GetHighlitedSprite () {

		if (highlited + startIndex < sprites.Length) {
			return sprites [highlited + startIndex];
		}

		return null;
	}
}