World_Bar.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 UnityEngine;
  11. namespace CodeMonkey.Utils {
  12. /*
  13. * Bar in the World, great for quickly making a health bar
  14. * */
  15. public class World_Bar {
  16. private Outline outline;
  17. private GameObject gameObject;
  18. private Transform transform;
  19. private Transform background;
  20. private Transform bar;
  21. public static int GetSortingOrder(Vector3 position, int offset, int baseSortingOrder = 5000) {
  22. return (int)(baseSortingOrder - position.y) + offset;
  23. }
  24. public class Outline {
  25. public float size = 1f;
  26. public Color color = Color.black;
  27. }
  28. public static World_Bar Create(Vector3 localPosition, Vector3 localScale, Color? backgroundColor, Color barColor, float sizeRatio, Outline outline = null) {
  29. return Create(null, localPosition, localScale, backgroundColor, barColor, sizeRatio, 0, outline);
  30. }
  31. public static World_Bar Create(Transform parent, Vector3 localPosition, Vector3 localScale, Color? backgroundColor, Color barColor, float sizeRatio, int sortingOrder, Outline outline = null) {
  32. return new World_Bar(parent, localPosition, localScale, backgroundColor, barColor, sizeRatio, sortingOrder, outline);
  33. }
  34. public World_Bar(Transform parent, Vector3 localPosition, Vector3 localScale, Color? backgroundColor, Color barColor, float sizeRatio, int sortingOrder, Outline outline = null) {
  35. this.outline = outline;
  36. SetupParent(parent, localPosition);
  37. if (outline != null) SetupOutline(outline, localScale, sortingOrder - 1);
  38. if (backgroundColor != null) SetupBackground((Color)backgroundColor, localScale, sortingOrder);
  39. SetupBar(barColor, localScale, sortingOrder + 1);
  40. SetSize(sizeRatio);
  41. }
  42. private void SetupParent(Transform parent, Vector3 localPosition) {
  43. gameObject = new GameObject("World_Bar");
  44. transform = gameObject.transform;
  45. transform.SetParent(parent);
  46. transform.localPosition = localPosition;
  47. }
  48. private void SetupOutline(Outline outline, Vector3 localScale, int sortingOrder) {
  49. UtilsClass.CreateWorldSprite(transform, "Outline", Assets.i.s_White, new Vector3(0,0), localScale + new Vector3(outline.size, outline.size), sortingOrder, outline.color);
  50. }
  51. private void SetupBackground(Color backgroundColor, Vector3 localScale, int sortingOrder) {
  52. background = UtilsClass.CreateWorldSprite(transform, "Background", Assets.i.s_White, new Vector3(0,0), localScale, sortingOrder, backgroundColor).transform;
  53. }
  54. private void SetupBar(Color barColor, Vector3 localScale, int sortingOrder) {
  55. GameObject barGO = new GameObject("Bar");
  56. bar = barGO.transform;
  57. bar.SetParent(transform);
  58. bar.localPosition = new Vector3(-localScale.x / 2f, 0, 0);
  59. bar.localScale = new Vector3(1,1,1);
  60. Transform barIn = UtilsClass.CreateWorldSprite(bar, "BarIn", Assets.i.s_White, new Vector3(localScale.x / 2f, 0), localScale, sortingOrder, barColor).transform;
  61. }
  62. public void SetLocalScale(Vector3 localScale) {
  63. // Outline
  64. if (transform.Find("Outline") != null) {
  65. // Has outline
  66. transform.Find("Outline").localScale = localScale + new Vector3(outline.size, outline.size);
  67. }
  68. //Background
  69. background.localScale = localScale;
  70. // Set Bar Scale
  71. bar.localPosition = new Vector3(-localScale.x / 2f, 0, 0);
  72. Transform barIn = bar.Find("BarIn");
  73. barIn.localScale = localScale;
  74. barIn.localPosition = new Vector3(localScale.x / 2f, 0);
  75. }
  76. public void SetSortingOrder(int sortingOrder) {
  77. bar.Find("BarIn").GetComponent<SpriteRenderer>().sortingOrder = sortingOrder;
  78. if (background != null) background.GetComponent<SpriteRenderer>().sortingOrder = sortingOrder - 1;
  79. if (transform.Find("Outline") != null) transform.Find("Outline").GetComponent<SpriteRenderer>().sortingOrder = sortingOrder - 2;
  80. }
  81. public void SetSortingLayerName(string sortingLayerName) {
  82. bar.Find("BarIn").GetComponent<SpriteRenderer>().sortingLayerName = sortingLayerName;
  83. if (background != null) background.GetComponent<SpriteRenderer>().sortingLayerName = sortingLayerName;
  84. if (transform.Find("Outline") != null) transform.Find("Outline").GetComponent<SpriteRenderer>().sortingLayerName = sortingLayerName;
  85. }
  86. public void SetRotation(float rotation) {
  87. transform.localEulerAngles = new Vector3(0, 0, rotation);
  88. }
  89. public void SetSize(float sizeRatio) {
  90. bar.localScale = new Vector3(sizeRatio, 1, 1);
  91. }
  92. public void SetColor(Color color) {
  93. bar.Find("BarIn").GetComponent<SpriteRenderer>().color = color;
  94. }
  95. public void SetActive(bool isActive) {
  96. gameObject.SetActive(isActive);
  97. }
  98. public void Show() {
  99. gameObject.SetActive(true);
  100. }
  101. public void Hide() {
  102. gameObject.SetActive(false);
  103. }
  104. public Button_Sprite AddButton(System.Action ClickFunc, System.Action MouseOverOnceFunc, System.Action MouseOutOnceFunc) {
  105. Button_Sprite buttonSprite = gameObject.AddComponent<Button_Sprite>();
  106. if (ClickFunc != null)
  107. buttonSprite.ClickFunc = ClickFunc;
  108. if (MouseOverOnceFunc != null)
  109. buttonSprite.MouseOverOnceFunc = MouseOverOnceFunc;
  110. if (MouseOutOnceFunc != null)
  111. buttonSprite.MouseOutOnceFunc = MouseOutOnceFunc;
  112. return buttonSprite;
  113. }
  114. public void DestroySelf() {
  115. if (gameObject != null) {
  116. Object.Destroy(gameObject);
  117. }
  118. }
  119. }
  120. }