UI_Bar.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ------------------- Code Monkey -------------------
  3. Thank you for downloading the Code Monkey Utilities
  4. I hope you find them useful in your projects
  5. If you have any questions use the contact form
  6. Cheers!
  7. unitycodemonkey.com
  8. --------------------------------------------------
  9. */
  10. using System;
  11. using UnityEngine;
  12. using UnityEngine.UI;
  13. namespace CodeMonkey.Utils {
  14. /*
  15. * Bar in the UI with scaleable Bar and Background
  16. * */
  17. public class UI_Bar {
  18. public GameObject gameObject;
  19. private RectTransform rectTransform;
  20. private RectTransform background;
  21. private RectTransform bar;
  22. private Vector2 size;
  23. /*
  24. * Outline into for Bar
  25. * */
  26. public class Outline {
  27. public float size = 1f;
  28. public Color color = Color.black;
  29. public Outline(float size, Color color) {
  30. this.size = size;
  31. this.color = color;
  32. }
  33. }
  34. public UI_Bar(GameObject gameObject, RectTransform background, RectTransform bar) {
  35. this.gameObject = gameObject;
  36. rectTransform = gameObject.GetComponent<RectTransform>();
  37. this.background = background;
  38. this.bar = bar;
  39. size = background.GetComponent<RectTransform>().sizeDelta;
  40. }
  41. public UI_Bar(GameObject gameObject, Transform background, Transform bar) {
  42. this.gameObject = gameObject;
  43. rectTransform = gameObject.GetComponent<RectTransform>();
  44. this.background = background.GetComponent<RectTransform>();
  45. this.bar = bar.GetComponent<RectTransform>();
  46. size = background.GetComponent<RectTransform>().sizeDelta;
  47. }
  48. public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color barColor, float sizeRatio) {
  49. SetupParent(parent, anchoredPosition, size);
  50. SetupBar(barColor);
  51. SetSize(sizeRatio);
  52. }
  53. public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color barColor, float sizeRatio, Outline outline) {
  54. SetupParent(parent, anchoredPosition, size);
  55. if (outline != null) SetupOutline(outline, size);
  56. SetupBar(barColor);
  57. SetSize(sizeRatio);
  58. }
  59. public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color backgroundColor, Color barColor, float sizeRatio) {
  60. SetupParent(parent, anchoredPosition, size);
  61. SetupBackground(backgroundColor);
  62. SetupBar(barColor);
  63. SetSize(sizeRatio);
  64. }
  65. public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color backgroundColor, Color barColor, float sizeRatio, Outline outline) {
  66. SetupParent(parent, anchoredPosition, size);
  67. if (outline != null) SetupOutline(outline, size);
  68. SetupBackground(backgroundColor);
  69. SetupBar(barColor);
  70. SetSize(sizeRatio);
  71. }
  72. private void SetupParent(Transform parent, Vector2 anchoredPosition, Vector2 size) {
  73. this.size = size;
  74. gameObject = new GameObject("UI_Bar", typeof(RectTransform));
  75. rectTransform = gameObject.GetComponent<RectTransform>();
  76. rectTransform.SetParent(parent, false);
  77. rectTransform.sizeDelta = size;
  78. rectTransform.anchorMin = new Vector2(0, .5f);
  79. rectTransform.anchorMax = new Vector2(0, .5f);
  80. rectTransform.pivot = new Vector2(0, .5f);
  81. rectTransform.anchoredPosition = anchoredPosition;
  82. }
  83. private RectTransform SetupOutline(Outline outline, Vector2 size) {
  84. return UtilsClass.DrawSprite(outline.color, gameObject.transform, Vector2.zero, size + new Vector2(outline.size, outline.size), "Outline");
  85. }
  86. private void SetupBackground(Color backgroundColor) {
  87. background = UtilsClass.DrawSprite(backgroundColor, gameObject.transform, Vector2.zero, Vector2.zero, "Background");
  88. background.anchorMin = new Vector2(0,0);
  89. background.anchorMax = new Vector2(1,1);
  90. }
  91. private void SetupBar(Color barColor) {
  92. bar = UtilsClass.DrawSprite(barColor, gameObject.transform, Vector2.zero, Vector2.zero, "Bar");
  93. bar.anchorMin = new Vector2(0,0);
  94. bar.anchorMax = new Vector2(0,1f);
  95. bar.pivot = new Vector2(0,.5f);
  96. }
  97. public void SetSize(float sizeRatio) {
  98. bar.sizeDelta = new Vector2(sizeRatio * size.x, 0f);
  99. }
  100. public void SetColor(Color color) {
  101. bar.GetComponent<Image>().color = color;
  102. }
  103. public void SetActive(bool active) {
  104. gameObject.SetActive(active);
  105. }
  106. public Vector2 GetAnchoredPosition() {
  107. return rectTransform.anchoredPosition;
  108. }
  109. public Vector2 GetSize() {
  110. return rectTransform.sizeDelta;
  111. }
  112. public void AddOutline(Outline outline) {
  113. RectTransform outlineRectTransform = SetupOutline(outline, size);
  114. outlineRectTransform.transform.SetAsFirstSibling();
  115. }
  116. public void SetRaycastTarget(bool set) {
  117. foreach (Transform trans in gameObject.transform) {
  118. if (trans.GetComponent<Image>() != null) {
  119. trans.GetComponent<Image>().raycastTarget = set;
  120. }
  121. }
  122. }
  123. public void DestroySelf() {
  124. UnityEngine.Object.Destroy(gameObject);
  125. }
  126. public Button_UI AddButton() {
  127. return AddButton(null, null, null);
  128. }
  129. public Button_UI AddButton(Action ClickFunc, Action MouseOverOnceFunc, Action MouseOutOnceFunc) {
  130. Button_UI buttonUI = gameObject.AddComponent<Button_UI>();
  131. if (ClickFunc != null)
  132. buttonUI.ClickFunc = ClickFunc;
  133. if (MouseOverOnceFunc != null)
  134. buttonUI.MouseOverOnceFunc = MouseOverOnceFunc;
  135. if (MouseOutOnceFunc != null)
  136. buttonUI.MouseOutOnceFunc = MouseOutOnceFunc;
  137. return buttonUI;
  138. }
  139. }
  140. }