RedDot.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. public class RedDot : MonoBehaviour {
  5. public enum Align
  6. {
  7. TopLeft,
  8. TopRight,
  9. BottomLeft,
  10. BottomRight,
  11. }
  12. public Text text;
  13. private void SetNum(int value)
  14. {
  15. text.text = value.ToString ();
  16. }
  17. public static void SetValue(Transform container, int value, Align align=Align.TopLeft)
  18. {
  19. RedDot redDot = container.GetComponentInChildren<RedDot> ();
  20. if (redDot == null && value != 0) {
  21. GameObject redDotObj = GameObject.Instantiate<GameObject> (Resources.Load<GameObject> ("Prefabs/UI/RedDot"));
  22. PopUpManager.AddToMainCanvas (redDotObj, container);
  23. redDot = redDotObj.GetComponent<RedDot> ();
  24. RectTransform rectTrans = container as RectTransform;
  25. Vector3[] corners = new Vector3[4];
  26. rectTrans.GetWorldCorners (corners);
  27. if(align == Align.TopLeft)
  28. {
  29. Vector3 pos = corners [1];
  30. pos.z = 0;
  31. redDot.transform.position = pos;
  32. }
  33. else if(align == Align.TopRight)
  34. {
  35. Vector3 pos = corners [2];
  36. pos.z = 0;
  37. redDot.transform.position = pos;
  38. }
  39. else if(align == Align.BottomRight)
  40. {
  41. Vector3 pos = corners [3];
  42. pos.z = 0;
  43. redDot.transform.position = pos;
  44. }
  45. else if(align == Align.BottomLeft)
  46. {
  47. Vector3 pos = corners [0];
  48. pos.z = 0;
  49. redDot.transform.position = pos;
  50. }
  51. }
  52. if (value == 0) {
  53. if (redDot != null) {
  54. GameObject.Destroy (redDot.gameObject);
  55. }
  56. } else {
  57. redDot.SetNum (value);
  58. }
  59. }
  60. }