UI_BarMultiple.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.Collections.Generic;
  11. using UnityEngine;
  12. using UnityEngine.UI;
  13. namespace CodeMonkey.Utils {
  14. /*
  15. * UI Container with multiple bars, useful for displaying one bar with multiple inner bars like success chance and failure chance
  16. * */
  17. public class UI_BarMultiple {
  18. private GameObject gameObject;
  19. private RectTransform rectTransform;
  20. private RectTransform[] barArr;
  21. private Image[] barImageArr;
  22. private Vector2 size;
  23. public class Outline {
  24. public float size = 1f;
  25. public Color color = Color.black;
  26. public Outline(float size, Color color) {
  27. this.size = size;
  28. this.color = color;
  29. }
  30. }
  31. public UI_BarMultiple(Transform parent, Vector2 anchoredPosition, Vector2 size, Color[] barColorArr, Outline outline) {
  32. this.size = size;
  33. SetupParent(parent, anchoredPosition, size);
  34. if (outline != null) SetupOutline(outline, size);
  35. List<RectTransform> barList = new List<RectTransform>();
  36. List<Image> barImageList = new List<Image>();
  37. List<float> defaultSizeList = new List<float>();
  38. foreach (Color color in barColorArr) {
  39. barList.Add(SetupBar(color));
  40. defaultSizeList.Add(1f / barColorArr.Length);
  41. }
  42. barArr = barList.ToArray();
  43. barImageArr = barImageList.ToArray();
  44. SetSizes(defaultSizeList.ToArray());
  45. }
  46. private void SetupParent(Transform parent, Vector2 anchoredPosition, Vector2 size) {
  47. gameObject = new GameObject("UI_BarMultiple", typeof(RectTransform));
  48. rectTransform = gameObject.GetComponent<RectTransform>();
  49. rectTransform.SetParent(parent, false);
  50. rectTransform.sizeDelta = size;
  51. rectTransform.anchorMin = new Vector2(0, .5f);
  52. rectTransform.anchorMax = new Vector2(0, .5f);
  53. rectTransform.pivot = new Vector2(0, .5f);
  54. rectTransform.anchoredPosition = anchoredPosition;
  55. }
  56. private void SetupOutline(Outline outline, Vector2 size) {
  57. UtilsClass.DrawSprite(outline.color, gameObject.transform, Vector2.zero, size + new Vector2(outline.size, outline.size), "Outline");
  58. }
  59. private RectTransform SetupBar(Color barColor) {
  60. RectTransform bar = UtilsClass.DrawSprite(barColor, gameObject.transform, Vector2.zero, Vector2.zero, "Bar");
  61. bar.anchorMin = new Vector2(0,0);
  62. bar.anchorMax = new Vector2(0,1f);
  63. bar.pivot = new Vector2(0,.5f);
  64. return bar;
  65. }
  66. public void SetSizes(float[] sizeArr) {
  67. if (sizeArr.Length != barArr.Length) {
  68. throw new System.Exception("Length doesn't match!");
  69. }
  70. Vector2 pos = Vector2.zero;
  71. for (int i=0; i<sizeArr.Length; i++) {
  72. float scaledSize = sizeArr[i] * size.x;
  73. barArr[i].anchoredPosition = pos;
  74. barArr[i].sizeDelta = new Vector2(scaledSize, 0f);
  75. pos.x += scaledSize;
  76. }
  77. }
  78. public RectTransform GetBar(int index) {
  79. return barArr[index];
  80. }
  81. public Vector2 GetSize() {
  82. return size;
  83. }
  84. public void DestroySelf() {
  85. UnityEngine.Object.Destroy(gameObject);
  86. }
  87. }
  88. }