DropArea.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class DropArea : MonoBehaviour
  5. {
  6. //Whether this DropArea is a child of the object in question
  7. public bool IsChildThisDropArea(GameObject Parent)
  8. {
  9. return IsChild(Parent, this.gameObject);
  10. }
  11. public static bool IsChild(GameObject Parent, GameObject child)
  12. {
  13. List<GameObject> list = GetAllChildren.GetAll(Parent);
  14. list.Add(Parent);
  15. foreach (GameObject child1 in list)
  16. {
  17. if (child1 == child.gameObject)
  18. {
  19. return true;
  20. }
  21. }
  22. return false;
  23. }
  24. [Header("DropPanel")]
  25. public GameObject DropPanel;
  26. [Header("OnPointerPanel")]
  27. public GameObject OnPointerPanel;
  28. private void Start()
  29. {
  30. if(DropPanel != null)
  31. {
  32. DropPanel.SetActive(false);
  33. }
  34. if(OnPointerPanel != null)
  35. {
  36. OnPointerPanel.SetActive(false);
  37. }
  38. }
  39. public void OnDropPanel()
  40. {
  41. if (DropPanel != null)
  42. {
  43. DropPanel.SetActive(true);
  44. }
  45. if (OnPointerPanel != null)
  46. {
  47. OnPointerPanel.SetActive(false);
  48. }
  49. }
  50. public void OffDropPanel()
  51. {
  52. if (DropPanel != null)
  53. {
  54. DropPanel.SetActive(false);
  55. }
  56. }
  57. public void OnPointerEnter()
  58. {
  59. if (OnPointerPanel != null)
  60. {
  61. OnPointerPanel.SetActive(true);
  62. }
  63. }
  64. public void OnPointerExit()
  65. {
  66. if (OnPointerPanel != null)
  67. {
  68. OnPointerPanel.SetActive(false);
  69. }
  70. }
  71. }
  72. public static class GetAllChildren
  73. {
  74. public static List<GameObject> GetAll(this GameObject obj)
  75. {
  76. List<GameObject> allChildren = new List<GameObject>();
  77. GetChildren(obj, ref allChildren);
  78. return allChildren;
  79. }
  80. //Retrieve child elements and add them to the list
  81. public static void GetChildren(GameObject obj, ref List<GameObject> allChildren)
  82. {
  83. Transform children = obj.GetComponentInChildren<Transform>();
  84. //Ends if there are no child elements.
  85. if (children.childCount == 0)
  86. {
  87. return;
  88. }
  89. foreach (Transform ob in children)
  90. {
  91. allChildren.Add(ob.gameObject);
  92. GetChildren(ob.gameObject, ref allChildren);
  93. }
  94. }
  95. }