1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- public class RedDot : MonoBehaviour {
- public enum Align
- {
- TopLeft,
- TopRight,
- BottomLeft,
- BottomRight,
- }
- public Text text;
- private void SetNum(int value)
- {
- text.text = value.ToString ();
- }
- public static void SetValue(Transform container, int value, Align align=Align.TopLeft)
- {
- RedDot redDot = container.GetComponentInChildren<RedDot> ();
- if (redDot == null && value != 0) {
- GameObject redDotObj = GameObject.Instantiate<GameObject> (Resources.Load<GameObject> ("Prefabs/UI/RedDot"));
- PopUpManager.AddToMainCanvas (redDotObj, container);
- redDot = redDotObj.GetComponent<RedDot> ();
- RectTransform rectTrans = container as RectTransform;
- Vector3[] corners = new Vector3[4];
- rectTrans.GetWorldCorners (corners);
- if(align == Align.TopLeft)
- {
- Vector3 pos = corners [1];
- pos.z = 0;
- redDot.transform.position = pos;
- }
- else if(align == Align.TopRight)
- {
- Vector3 pos = corners [2];
- pos.z = 0;
- redDot.transform.position = pos;
- }
- else if(align == Align.BottomRight)
- {
- Vector3 pos = corners [3];
- pos.z = 0;
- redDot.transform.position = pos;
- }
- else if(align == Align.BottomLeft)
- {
- Vector3 pos = corners [0];
- pos.z = 0;
- redDot.transform.position = pos;
- }
- }
- if (value == 0) {
- if (redDot != null) {
- GameObject.Destroy (redDot.gameObject);
- }
- } else {
- redDot.SetNum (value);
- }
- }
- }
|