SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
5.2 KiB

4 years ago
  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(Transform parent, Vector2 anchoredPosition, Vector2 size, Color barColor, float sizeRatio) {
  35. SetupParent(parent, anchoredPosition, size);
  36. SetupBar(barColor);
  37. SetSize(sizeRatio);
  38. }
  39. public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color barColor, float sizeRatio, Outline outline) {
  40. SetupParent(parent, anchoredPosition, size);
  41. if (outline != null) SetupOutline(outline, size);
  42. SetupBar(barColor);
  43. SetSize(sizeRatio);
  44. }
  45. public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color backgroundColor, Color barColor, float sizeRatio) {
  46. SetupParent(parent, anchoredPosition, size);
  47. SetupBackground(backgroundColor);
  48. SetupBar(barColor);
  49. SetSize(sizeRatio);
  50. }
  51. public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color backgroundColor, Color barColor, float sizeRatio, Outline outline) {
  52. SetupParent(parent, anchoredPosition, size);
  53. if (outline != null) SetupOutline(outline, size);
  54. SetupBackground(backgroundColor);
  55. SetupBar(barColor);
  56. SetSize(sizeRatio);
  57. }
  58. private void SetupParent(Transform parent, Vector2 anchoredPosition, Vector2 size) {
  59. this.size = size;
  60. gameObject = new GameObject("UI_Bar", typeof(RectTransform));
  61. rectTransform = gameObject.GetComponent<RectTransform>();
  62. rectTransform.SetParent(parent, false);
  63. rectTransform.sizeDelta = size;
  64. rectTransform.anchorMin = new Vector2(0, .5f);
  65. rectTransform.anchorMax = new Vector2(0, .5f);
  66. rectTransform.pivot = new Vector2(0, .5f);
  67. rectTransform.anchoredPosition = anchoredPosition;
  68. }
  69. private RectTransform SetupOutline(Outline outline, Vector2 size) {
  70. return UtilsClass.DrawSprite(outline.color, gameObject.transform, Vector2.zero, size + new Vector2(outline.size, outline.size), "Outline");
  71. }
  72. private void SetupBackground(Color backgroundColor) {
  73. background = UtilsClass.DrawSprite(backgroundColor, gameObject.transform, Vector2.zero, Vector2.zero, "Background");
  74. background.anchorMin = new Vector2(0,0);
  75. background.anchorMax = new Vector2(1,1);
  76. }
  77. private void SetupBar(Color barColor) {
  78. bar = UtilsClass.DrawSprite(barColor, gameObject.transform, Vector2.zero, Vector2.zero, "Bar");
  79. bar.anchorMin = new Vector2(0,0);
  80. bar.anchorMax = new Vector2(0,1f);
  81. bar.pivot = new Vector2(0,.5f);
  82. }
  83. public void SetSize(float sizeRatio) {
  84. bar.sizeDelta = new Vector2(sizeRatio * size.x, 0);
  85. }
  86. public void SetColor(Color color) {
  87. bar.GetComponent<Image>().color = color;
  88. }
  89. public void SetActive(bool active) {
  90. gameObject.SetActive(active);
  91. }
  92. public void AddOutline(Outline outline) {
  93. RectTransform outlineRectTransform = SetupOutline(outline, size);
  94. outlineRectTransform.transform.SetAsFirstSibling();
  95. }
  96. public void SetRaycastTarget(bool set) {
  97. foreach (Transform trans in gameObject.transform) {
  98. if (trans.GetComponent<Image>() != null) {
  99. trans.GetComponent<Image>().raycastTarget = set;
  100. }
  101. }
  102. }
  103. public void DestroySelf() {
  104. UnityEngine.Object.Destroy(gameObject);
  105. }
  106. public Button_UI AddButton() {
  107. return AddButton(null, null, null);
  108. }
  109. public Button_UI AddButton(Action ClickFunc, Action MouseOverOnceFunc, Action MouseOutOnceFunc) {
  110. Button_UI buttonUI = gameObject.AddComponent<Button_UI>();
  111. if (ClickFunc != null)
  112. buttonUI.ClickFunc = ClickFunc;
  113. if (MouseOverOnceFunc != null)
  114. buttonUI.MouseOverOnceFunc = MouseOverOnceFunc;
  115. if (MouseOutOnceFunc != null)
  116. buttonUI.MouseOutOnceFunc = MouseOutOnceFunc;
  117. return buttonUI;
  118. }
  119. }
  120. }