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.

96 lines
4.0 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 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 GameObject gameObject;
  17. private Transform transform;
  18. private Transform background;
  19. private Transform bar;
  20. public static int GetSortingOrder(Vector3 position, int offset, int baseSortingOrder = 5000) {
  21. return (int)(baseSortingOrder - position.y) + offset;
  22. }
  23. public class Outline {
  24. public float size = 1f;
  25. public Color color = Color.black;
  26. }
  27. public World_Bar(Transform parent, Vector3 localPosition, Vector3 localScale, Color? backgroundColor, Color barColor, float sizeRatio, int sortingOrder, Outline outline = null) {
  28. SetupParent(parent, localPosition);
  29. if (outline != null) SetupOutline(outline, localScale, sortingOrder - 1);
  30. if (backgroundColor != null) SetupBackground((Color)backgroundColor, localScale, sortingOrder);
  31. SetupBar(barColor, localScale, sortingOrder + 1);
  32. SetSize(sizeRatio);
  33. }
  34. private void SetupParent(Transform parent, Vector3 localPosition) {
  35. gameObject = new GameObject("World_Bar");
  36. transform = gameObject.transform;
  37. transform.SetParent(parent);
  38. transform.localPosition = localPosition;
  39. }
  40. private void SetupOutline(Outline outline, Vector3 localScale, int sortingOrder) {
  41. UtilsClass.CreateWorldSprite(transform, "Outline", Assets.i.s_White, new Vector3(0,0), localScale + new Vector3(outline.size, outline.size), sortingOrder, outline.color);
  42. }
  43. private void SetupBackground(Color backgroundColor, Vector3 localScale, int sortingOrder) {
  44. background = UtilsClass.CreateWorldSprite(transform, "Background", Assets.i.s_White, new Vector3(0,0), localScale, sortingOrder, backgroundColor).transform;
  45. }
  46. private void SetupBar(Color barColor, Vector3 localScale, int sortingOrder) {
  47. GameObject barGO = new GameObject("Bar");
  48. bar = barGO.transform;
  49. bar.SetParent(transform);
  50. bar.localPosition = new Vector3(-localScale.x / 2f, 0, 0);
  51. bar.localScale = new Vector3(1,1,1);
  52. Transform barIn = UtilsClass.CreateWorldSprite(bar, "BarIn", Assets.i.s_White, new Vector3(localScale.x / 2f, 0), localScale, sortingOrder, barColor).transform;
  53. }
  54. public void SetRotation(float rotation) {
  55. transform.localEulerAngles = new Vector3(0, 0, rotation);
  56. }
  57. public void SetSize(float sizeRatio) {
  58. bar.localScale = new Vector3(sizeRatio, 1, 1);
  59. }
  60. public void SetColor(Color color) {
  61. bar.Find("BarIn").GetComponent<SpriteRenderer>().color = color;
  62. }
  63. public void Show() {
  64. gameObject.SetActive(true);
  65. }
  66. public void Hide() {
  67. gameObject.SetActive(false);
  68. }
  69. public Button_Sprite AddButton(System.Action ClickFunc, System.Action MouseOverOnceFunc, System.Action MouseOutOnceFunc) {
  70. Button_Sprite buttonSprite = gameObject.AddComponent<Button_Sprite>();
  71. if (ClickFunc != null)
  72. buttonSprite.ClickFunc = ClickFunc;
  73. if (MouseOverOnceFunc != null)
  74. buttonSprite.MouseOverOnceFunc = MouseOverOnceFunc;
  75. if (MouseOutOnceFunc != null)
  76. buttonSprite.MouseOutOnceFunc = MouseOutOnceFunc;
  77. return buttonSprite;
  78. }
  79. public void DestroySelf() {
  80. if (gameObject != null) {
  81. Object.Destroy(gameObject);
  82. }
  83. }
  84. }
  85. }