| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- /*
- ------------------- Code Monkey -------------------
- Thank you for downloading the Code Monkey Utilities
- I hope you find them useful in your projects
- If you have any questions use the contact form
- Cheers!
- unitycodemonkey.com
- --------------------------------------------------
- */
- using System;
- using UnityEngine;
- using UnityEngine.UI;
- namespace CodeMonkey.Utils {
- /*
- * Bar in the UI with scaleable Bar and Background
- * */
- public class UI_Bar {
-
- public GameObject gameObject;
- private RectTransform rectTransform;
- private RectTransform background;
- private RectTransform bar;
- private Vector2 size;
-
- /*
- * Outline into for Bar
- * */
- public class Outline {
- public float size = 1f;
- public Color color = Color.black;
- public Outline(float size, Color color) {
- this.size = size;
- this.color = color;
- }
- }
- public UI_Bar(GameObject gameObject, RectTransform background, RectTransform bar) {
- this.gameObject = gameObject;
- rectTransform = gameObject.GetComponent<RectTransform>();
- this.background = background;
- this.bar = bar;
- size = background.GetComponent<RectTransform>().sizeDelta;
- }
- public UI_Bar(GameObject gameObject, Transform background, Transform bar) {
- this.gameObject = gameObject;
- rectTransform = gameObject.GetComponent<RectTransform>();
- this.background = background.GetComponent<RectTransform>();
- this.bar = bar.GetComponent<RectTransform>();
- size = background.GetComponent<RectTransform>().sizeDelta;
- }
- public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color barColor, float sizeRatio) {
- SetupParent(parent, anchoredPosition, size);
- SetupBar(barColor);
- SetSize(sizeRatio);
- }
- public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color barColor, float sizeRatio, Outline outline) {
- SetupParent(parent, anchoredPosition, size);
- if (outline != null) SetupOutline(outline, size);
- SetupBar(barColor);
- SetSize(sizeRatio);
- }
- public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color backgroundColor, Color barColor, float sizeRatio) {
- SetupParent(parent, anchoredPosition, size);
- SetupBackground(backgroundColor);
- SetupBar(barColor);
- SetSize(sizeRatio);
- }
- public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color backgroundColor, Color barColor, float sizeRatio, Outline outline) {
- SetupParent(parent, anchoredPosition, size);
- if (outline != null) SetupOutline(outline, size);
- SetupBackground(backgroundColor);
- SetupBar(barColor);
- SetSize(sizeRatio);
- }
- private void SetupParent(Transform parent, Vector2 anchoredPosition, Vector2 size) {
- this.size = size;
- gameObject = new GameObject("UI_Bar", typeof(RectTransform));
- rectTransform = gameObject.GetComponent<RectTransform>();
- rectTransform.SetParent(parent, false);
- rectTransform.sizeDelta = size;
- rectTransform.anchorMin = new Vector2(0, .5f);
- rectTransform.anchorMax = new Vector2(0, .5f);
- rectTransform.pivot = new Vector2(0, .5f);
- rectTransform.anchoredPosition = anchoredPosition;
- }
- private RectTransform SetupOutline(Outline outline, Vector2 size) {
- return UtilsClass.DrawSprite(outline.color, gameObject.transform, Vector2.zero, size + new Vector2(outline.size, outline.size), "Outline");
- }
- private void SetupBackground(Color backgroundColor) {
- background = UtilsClass.DrawSprite(backgroundColor, gameObject.transform, Vector2.zero, Vector2.zero, "Background");
- background.anchorMin = new Vector2(0,0);
- background.anchorMax = new Vector2(1,1);
- }
- private void SetupBar(Color barColor) {
- bar = UtilsClass.DrawSprite(barColor, gameObject.transform, Vector2.zero, Vector2.zero, "Bar");
- bar.anchorMin = new Vector2(0,0);
- bar.anchorMax = new Vector2(0,1f);
- bar.pivot = new Vector2(0,.5f);
- }
- public void SetSize(float sizeRatio) {
- bar.sizeDelta = new Vector2(sizeRatio * size.x, 0f);
- }
- public void SetColor(Color color) {
- bar.GetComponent<Image>().color = color;
- }
- public void SetActive(bool active) {
- gameObject.SetActive(active);
- }
- public Vector2 GetAnchoredPosition() {
- return rectTransform.anchoredPosition;
- }
- public Vector2 GetSize() {
- return rectTransform.sizeDelta;
- }
- public void AddOutline(Outline outline) {
- RectTransform outlineRectTransform = SetupOutline(outline, size);
- outlineRectTransform.transform.SetAsFirstSibling();
- }
- public void SetRaycastTarget(bool set) {
- foreach (Transform trans in gameObject.transform) {
- if (trans.GetComponent<Image>() != null) {
- trans.GetComponent<Image>().raycastTarget = set;
- }
- }
- }
- public void DestroySelf() {
- UnityEngine.Object.Destroy(gameObject);
- }
- public Button_UI AddButton() {
- return AddButton(null, null, null);
- }
- public Button_UI AddButton(Action ClickFunc, Action MouseOverOnceFunc, Action MouseOutOnceFunc) {
- Button_UI buttonUI = gameObject.AddComponent<Button_UI>();
- if (ClickFunc != null)
- buttonUI.ClickFunc = ClickFunc;
- if (MouseOverOnceFunc != null)
- buttonUI.MouseOverOnceFunc = MouseOverOnceFunc;
- if (MouseOutOnceFunc != null)
- buttonUI.MouseOutOnceFunc = MouseOutOnceFunc;
- return buttonUI;
- }
- }
- }
|