Advertisement
zero3growlithe

Unity UI - UI grids base class

Apr 6th, 2017
130
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. /* -------------------------- */
  7. /*  CODE USAGE EXAMPLE BELOW  */
  8. /* vvvvvvvvvvvvvvvvvvvvvvvvvv */
  9.  
  10. // I mean below the BaseMenuGrid class
  11.  
  12. // HIERARCHY LAYOUT
  13. // video showing compatible UI hierarchy layout: https://www.youtube.com/watch?v=lUun2xW6FJ4
  14.  
  15. public class BaseMenuGrid : MonoBehaviour
  16. {
  17. //*** ATTRIBUTES ***//
  18.     [Header("[ References ]")]
  19.     [SerializeField]
  20.     private RectTransform targetGrid; // target transform that will contain spawned grid elements
  21.     [SerializeField]
  22.     private GameObject gridElement; // grid element used to fill the targetGrid
  23.  
  24.     [Space(10)]
  25.     [SerializeField]
  26.     private GameObject emptyGridLabel; // object displayed when the list is empty
  27.  
  28. //*** PROPERTIES ***//
  29.     // REFERENCES
  30.     protected RectTransform TargetGrid {
  31.         get {return targetGrid;}
  32.     }
  33.     protected GameObject GridElement {
  34.         get {return gridElement;}
  35.     }
  36.  
  37.     protected GameObject EmptyGridLabel {
  38.         get {return emptyGridLabel;}
  39.     }
  40.  
  41.     // VARIABLES
  42.     public int ElementCount {
  43.         get {return TargetGrid.childCount;}
  44.     }
  45.  
  46.     protected List<Transform> ProtectedGridElements {get; set;}
  47.  
  48.     // not a property due to ref
  49.     private Coroutine DisplayNoElementsLabelCoroutine;
  50.  
  51. //*** METHODS - PUBLIC ***//
  52.     public List<T> GetGridElements<T> () where T : Component
  53.     {
  54.         List<T> output = new List<T>();
  55.  
  56.         foreach (Transform child in TargetGrid)
  57.         {
  58.             output.Add(child.gameObject.GetComponent<T>());
  59.         }
  60.  
  61.         return output;
  62.     }
  63.  
  64.     public List<GameObject> GetGridElements ()
  65.     {
  66.         List<GameObject> output = new List<GameObject>();
  67.  
  68.         foreach (Transform child in TargetGrid)
  69.         {
  70.             output.Add(child.gameObject);
  71.         }
  72.  
  73.         return output;
  74.     }
  75.  
  76.     public virtual void UpdateGrid ()
  77.     {
  78.  
  79.     }
  80.  
  81.     public virtual void UpdateGrid<T> (List<object> elements) where T : Object
  82.     {
  83.        
  84.     }
  85.  
  86.     public virtual void UpdateGrid<T> (object[] elements) where T : Object
  87.     {
  88.  
  89.     }
  90.  
  91.     public virtual void UpdateGrid (List<GameObject> elements)
  92.     {
  93.        
  94.     }
  95.  
  96.     public virtual void UpdateGrid (GameObject[] elements)
  97.     {
  98.  
  99.     }
  100.  
  101.     public virtual void CreateGrid ()
  102.     {
  103.         ClearGrid();
  104.         ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
  105.     }
  106.  
  107.     public virtual void CreateGrid<T> (List<T> elements) where T : Object
  108.     {
  109.         ClearGrid();
  110.         ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
  111.     }
  112.  
  113.     public virtual void CreateGrid<T> (T[] elements) where T : Object
  114.     {
  115.         ClearGrid();
  116.         ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
  117.     }
  118.  
  119.     public virtual void CreateGrid (List<string> elements)
  120.     {
  121.         ClearGrid();
  122.         ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
  123.     }
  124.  
  125.     public virtual void CreateGrid (string[] elements)
  126.     {
  127.         ClearGrid();
  128.         ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
  129.     }
  130.  
  131.     public virtual void CreateGrid (List<GameObject> elements)
  132.     {
  133.         ClearGrid();
  134.         ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
  135.     }
  136.  
  137.     public virtual void CreateGrid (GameObject[] elements)
  138.     {
  139.         ClearGrid();
  140.         ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
  141.     }
  142.  
  143.     public virtual GameObject SpawnGridElement ()
  144.     {
  145.         GameObject spawnedElement = Instantiate(GridElement);
  146.  
  147.         SetUpGridElement(spawnedElement);
  148.  
  149.         return spawnedElement;
  150.     }
  151.  
  152.     public virtual void ClearGrid ()
  153.     {
  154.         if (TargetGrid == null)
  155.         {
  156.             return;
  157.         }
  158.  
  159.         // protect children!
  160.         if (ProtectedGridElements == null)
  161.         {
  162.             CreateProtectedElementsList();
  163.         }
  164.  
  165.         // collect all children
  166.         List<Transform> children = new List<Transform>();
  167.  
  168.         foreach (Transform child in TargetGrid)
  169.         {
  170.             if (ProtectedGridElements.Contains(child) == false)
  171.             {
  172.                 children.Add(child);
  173.             }
  174.         }
  175.  
  176.         // destroy all children (lel)
  177.         foreach (Transform child in children)
  178.         {
  179.             DestroyImmediate(child.gameObject);
  180.         }
  181.     }
  182.  
  183.     public virtual void SetUpGridElement (GameObject element)
  184.     {
  185.         element.transform.SetParent(TargetGrid);
  186.         element.transform.localScale = Vector3.one;
  187.         element.SetActive(true);
  188.     }
  189.  
  190. //*** METHODS - PROTECTED ***//
  191.     protected virtual void Awake ()
  192.     {
  193.         GridElement.SetActive(false);
  194.     }
  195.  
  196.     protected virtual void Start ()
  197.     {
  198.        
  199.     }
  200.    
  201.     protected virtual void Update ()
  202.     {
  203.        
  204.     }
  205.  
  206.     protected void ExecuteRunMethodOnEndOfFrame (ref Coroutine runningCoroutine, System.Action method)
  207.     {
  208.         if (runningCoroutine != null)
  209.         {
  210.             StopCoroutine(runningCoroutine);
  211.         }
  212.  
  213.         runningCoroutine = ExecuteRunMethodOnEndOfFrame(method);
  214.     }
  215.  
  216.     protected Coroutine ExecuteRunMethodOnEndOfFrame (System.Action method)
  217.     {
  218.         if (gameObject.activeInHierarchy == true)
  219.         {
  220.             return StartCoroutine(RunMethodOnEndOfFrame(method));
  221.         }
  222.  
  223.         return null;
  224.     }
  225.  
  226.     protected IEnumerator RunMethodOnEndOfFrame (System.Action method)
  227.     {
  228.         // a weird Unity hack ffs
  229.         if (Time.frameCount > 1)
  230.         {
  231.             yield return new WaitForEndOfFrame();
  232.         }
  233.         else
  234.         {
  235.             yield return null;
  236.         }
  237.  
  238.         if (method != null)
  239.         {
  240.             method();
  241.         }
  242.     }
  243.  
  244. //*** METHODS - PRIVATE ***//
  245.     private void UpdateNoElementsLabel ()
  246.     {
  247.         if (EmptyGridLabel != null)
  248.         {
  249.             EmptyGridLabel.SetActive(TargetGrid.childCount == 0);
  250.         }
  251.     }
  252.  
  253.     private void CreateProtectedElementsList ()
  254.     {
  255.         ProtectedGridElements = new List<Transform>();
  256.  
  257.         foreach (Transform child in TargetGrid)
  258.         {
  259.             ProtectedGridElements.Add(child);
  260.         }
  261.     }
  262. }
  263.  
  264. /* -------------------------- */
  265. /*  CODE USAGE EXAMPLE BELOW  */
  266. /* vvvvvvvvvvvvvvvvvvvvvvvvvv */
  267.  
  268. // override BaseMenuGrid class and CreateGrid methods and handle passed parameters any way you need
  269. public class TestCreateGrid: BaseMenuGrid
  270. {
  271.     #region FUNCTIONS
  272.  
  273.     public override void CreateGrid (List<string> gridElements)
  274.     {
  275.         base.CreateGrid();
  276.  
  277.         for (int i = 0; i < gridElements.Count; i++)
  278.         {
  279.             GameObject element = SpawnGridElement(); // it is important to use SpawnGridElement method
  280.  
  281.             element.transform.name = gridElements[i];
  282.         }
  283.     }
  284.    
  285.     protected override void Start()
  286.     {
  287.         base.Start();
  288.  
  289.         // usage example
  290.         List<string> someList = new List<string>(){
  291.             "someObject",
  292.             "andAnotherObject",
  293.             "objectAnotherThanAnother"
  294.         };
  295.  
  296.         CreateGrid(someList);
  297.     }
  298.  
  299.     #endregion
  300.  
  301.     #region CLASS_ENUMS
  302.  
  303.     #endregion
  304. }
Advertisement
RAW Paste Data Copied
Advertisement