Euras

ColourGrid

Feb 1st, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class ColourGrid : MonoBehaviour {
  6.  
  7.     public static GameObject gO;
  8.     public int ColourIndex = 0;
  9.  
  10.     void Awake()
  11.     {
  12.         gO = transform.gameObject;
  13.         transform.gameObject.SetActive(false);
  14.  
  15.         Texture2D[] tx = new Texture2D[Place.tiles.Length];
  16.  
  17.         for (int i = 0; i < Place.tiles.Length; i++)
  18.         {
  19.             GameObject g = new GameObject();
  20.             g.transform.parent = transform;
  21.             g.name = "tile[" + i + "]";
  22.  
  23.             tx[i] = (Texture2D)Resources.Load("gui-blocks/" + (i + 1), typeof(Texture2D));
  24.             g.AddComponent<Image>().sprite = Sprite.Create(tx[i], new Rect(0, 0, 64, 64), new Vector2(0.5f, 0.5f));
  25.             g.AddComponent<Button>().targetGraphic = g.GetComponent<Image>();
  26.             g.AddComponent<GlobalValues>().i = ColourIndex++;
  27.             g.GetComponent<Button>().onClick.AddListener(() => SetColour(g.GetComponent<GlobalValues>().i));
  28.         }
  29.     }
  30.  
  31.     public void SetColour(int i)
  32.     {
  33.         Place.blockType = i + 1;
  34.         transform.gameObject.SetActive(false);
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment