Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System.Collections.Generic;
- /* -------------------------- */
- /* CODE USAGE EXAMPLE BELOW */
- /* vvvvvvvvvvvvvvvvvvvvvvvvvv */
- // I mean below the BaseMenuGrid class
- // HIERARCHY LAYOUT
- // video showing compatible UI hierarchy layout: https://www.youtube.com/watch?v=lUun2xW6FJ4
- public class BaseMenuGrid : MonoBehaviour
- {
- //*** ATTRIBUTES ***//
- [Header("[ References ]")]
- [SerializeField]
- private RectTransform targetGrid; // target transform that will contain spawned grid elements
- [SerializeField]
- private GameObject gridElement; // grid element used to fill the targetGrid
- [Space(10)]
- [SerializeField]
- private GameObject emptyGridLabel; // object displayed when the list is empty
- //*** PROPERTIES ***//
- // REFERENCES
- protected RectTransform TargetGrid {
- get {return targetGrid;}
- }
- protected GameObject GridElement {
- get {return gridElement;}
- }
- protected GameObject EmptyGridLabel {
- get {return emptyGridLabel;}
- }
- // VARIABLES
- public int ElementCount {
- get {return TargetGrid.childCount;}
- }
- protected List<Transform> ProtectedGridElements {get; set;}
- // not a property due to ref
- private Coroutine DisplayNoElementsLabelCoroutine;
- //*** METHODS - PUBLIC ***//
- public List<T> GetGridElements<T> () where T : Component
- {
- List<T> output = new List<T>();
- foreach (Transform child in TargetGrid)
- {
- output.Add(child.gameObject.GetComponent<T>());
- }
- return output;
- }
- public List<GameObject> GetGridElements ()
- {
- List<GameObject> output = new List<GameObject>();
- foreach (Transform child in TargetGrid)
- {
- output.Add(child.gameObject);
- }
- return output;
- }
- public virtual void UpdateGrid ()
- {
- }
- public virtual void UpdateGrid<T> (List<object> elements) where T : Object
- {
- }
- public virtual void UpdateGrid<T> (object[] elements) where T : Object
- {
- }
- public virtual void UpdateGrid (List<GameObject> elements)
- {
- }
- public virtual void UpdateGrid (GameObject[] elements)
- {
- }
- public virtual void CreateGrid ()
- {
- ClearGrid();
- ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
- }
- public virtual void CreateGrid<T> (List<T> elements) where T : Object
- {
- ClearGrid();
- ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
- }
- public virtual void CreateGrid<T> (T[] elements) where T : Object
- {
- ClearGrid();
- ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
- }
- public virtual void CreateGrid (List<string> elements)
- {
- ClearGrid();
- ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
- }
- public virtual void CreateGrid (string[] elements)
- {
- ClearGrid();
- ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
- }
- public virtual void CreateGrid (List<GameObject> elements)
- {
- ClearGrid();
- ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
- }
- public virtual void CreateGrid (GameObject[] elements)
- {
- ClearGrid();
- ExecuteRunMethodOnEndOfFrame(ref DisplayNoElementsLabelCoroutine, UpdateNoElementsLabel);
- }
- public virtual GameObject SpawnGridElement ()
- {
- GameObject spawnedElement = Instantiate(GridElement);
- SetUpGridElement(spawnedElement);
- return spawnedElement;
- }
- public virtual void ClearGrid ()
- {
- if (TargetGrid == null)
- {
- return;
- }
- // protect children!
- if (ProtectedGridElements == null)
- {
- CreateProtectedElementsList();
- }
- // collect all children
- List<Transform> children = new List<Transform>();
- foreach (Transform child in TargetGrid)
- {
- if (ProtectedGridElements.Contains(child) == false)
- {
- children.Add(child);
- }
- }
- // destroy all children (lel)
- foreach (Transform child in children)
- {
- DestroyImmediate(child.gameObject);
- }
- }
- public virtual void SetUpGridElement (GameObject element)
- {
- element.transform.SetParent(TargetGrid);
- element.transform.localScale = Vector3.one;
- element.SetActive(true);
- }
- //*** METHODS - PROTECTED ***//
- protected virtual void Awake ()
- {
- GridElement.SetActive(false);
- }
- protected virtual void Start ()
- {
- }
- protected virtual void Update ()
- {
- }
- protected void ExecuteRunMethodOnEndOfFrame (ref Coroutine runningCoroutine, System.Action method)
- {
- if (runningCoroutine != null)
- {
- StopCoroutine(runningCoroutine);
- }
- runningCoroutine = ExecuteRunMethodOnEndOfFrame(method);
- }
- protected Coroutine ExecuteRunMethodOnEndOfFrame (System.Action method)
- {
- if (gameObject.activeInHierarchy == true)
- {
- return StartCoroutine(RunMethodOnEndOfFrame(method));
- }
- return null;
- }
- protected IEnumerator RunMethodOnEndOfFrame (System.Action method)
- {
- // a weird Unity hack ffs
- if (Time.frameCount > 1)
- {
- yield return new WaitForEndOfFrame();
- }
- else
- {
- yield return null;
- }
- if (method != null)
- {
- method();
- }
- }
- //*** METHODS - PRIVATE ***//
- private void UpdateNoElementsLabel ()
- {
- if (EmptyGridLabel != null)
- {
- EmptyGridLabel.SetActive(TargetGrid.childCount == 0);
- }
- }
- private void CreateProtectedElementsList ()
- {
- ProtectedGridElements = new List<Transform>();
- foreach (Transform child in TargetGrid)
- {
- ProtectedGridElements.Add(child);
- }
- }
- }
- /* -------------------------- */
- /* CODE USAGE EXAMPLE BELOW */
- /* vvvvvvvvvvvvvvvvvvvvvvvvvv */
- // override BaseMenuGrid class and CreateGrid methods and handle passed parameters any way you need
- public class TestCreateGrid: BaseMenuGrid
- {
- #region FUNCTIONS
- public override void CreateGrid (List<string> gridElements)
- {
- base.CreateGrid();
- for (int i = 0; i < gridElements.Count; i++)
- {
- GameObject element = SpawnGridElement(); // it is important to use SpawnGridElement method
- element.transform.name = gridElements[i];
- }
- }
- protected override void Start()
- {
- base.Start();
- // usage example
- List<string> someList = new List<string>(){
- "someObject",
- "andAnotherObject",
- "objectAnotherThanAnother"
- };
- CreateGrid(someList);
- }
- #endregion
- #region CLASS_ENUMS
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement