2 using System.Collections;
3 using System.Collections.Generic;
17 public GameObject firstNotification;
18 public GameObject secondNotification;
19 public GameObject thirdNotification;
20 public GameObject statusBar;
21 public GameObject additionalInformation;
22 public GameObject customAdditionalInfoIn;
23 public GameObject customAdditionalInfo = null;
25 [Header(
"Variables for moving notification center to the viewport")]
26 public bool moveNotificationCenter =
true;
27 public int minXPositionNotificationCenter = -650;
28 public int maxXPositionNotificationCenter = 1000;
30 private List<Notification> notitficationList =
new List<Notification>();
38 throw(
new System.Exception(
"Cannot create NotificationControl: Only one NotificationControl may exist!" ));
45 firstNotification.SetActive(
false);
46 secondNotification.SetActive(
false);
47 thirdNotification.SetActive(
false);
48 this.gameObject.SetActive(
false);
51 this.GetComponent<RectTransform>().localPosition =
new Vector2(0, this.GetComponent<RectTransform>().localPosition.y);
53 hideAdditionalInformation();
58 bool listChanged = deleteNotificationsIfExpired();
61 hideAdditionalInformation();
62 updateNotificationCenter();
66 if (moveNotificationCenter)
69 Ray ray =
new Ray(Camera.main.transform.position, Camera.main.transform.forward);
70 LayerMask onlyUIMeshLayer = 100000000;
72 if (Physics.Raycast(ray, out hit, Mathf.Infinity, onlyUIMeshLayer)) {
73 float statusbarWidth = statusBar.GetComponent<RectTransform>().rect.width;
74 int newXPos = (
int)(statusbarWidth * hit.textureCoord.x);
75 newXPos = newXPos - (int)(statusbarWidth / 2);
76 newXPos = Math.Min(newXPos, maxXPositionNotificationCenter);
77 newXPos = Math.Max(newXPos, minXPositionNotificationCenter);
78 this.GetComponent<RectTransform>().localPosition =
new Vector2(newXPos, this.GetComponent<RectTransform>().localPosition.y);
85 System.Random rnd =
new System.Random();
89 createNotification(m);
90 createNotification(n);
91 createNotification(i);
96 notitficationList.Add(n);
97 orderNotitficationList();
98 updateNotificationCenter();
105 createNotification (n);
108 private bool deleteNotificationsIfExpired()
111 for(
int i = notitficationList.Count - 1; i >= 0; i--)
113 if(notitficationList[i].ExpireDate < DateTime.Now)
115 notitficationList.RemoveAt(i);
123 private void updateNotificationCenter()
125 if (notitficationList.Count == 0)
127 this.gameObject.SetActive(
false);
131 firstNotification.SetActive(
false);
132 secondNotification.SetActive(
false);
133 thirdNotification.SetActive(
false);
135 this.gameObject.SetActive(
true);
137 if(notitficationList.Count > 0)
139 firstNotification.SetActive(
true);
140 setTextAndIconInNotifiaction(firstNotification, notitficationList[0],
true);
142 if (notitficationList.Count > 1)
144 secondNotification.SetActive(
true);
145 setTextAndIconInNotifiaction(secondNotification, notitficationList[1],
false);
147 if (notitficationList.Count > 2)
149 thirdNotification.SetActive(
true);
150 setTextAndIconInNotifiaction(thirdNotification, notitficationList[2],
false);
154 private void setTextAndIconInNotifiaction(GameObject notificationGameObject,
Notification n,
bool firstNotification)
157 notificationGameObject.GetComponentInChildren<Text>().text = n.Text;
160 if (n.NotificationSprite != null)
162 notificationGameObject.transform.Find(
"Icon").GetComponent<Image>().sprite = n.NotificationSprite;
169 if (firstNotification)
171 if(n.AdditionalInfo != null)
173 if (n.AdditionalInfo.Type ==
AdditionalInfo.AdditionalInfoType.TEXT)
175 additionalInformation.GetComponentInChildren<Text>().text = n.AdditionalInfo.AdditionalInfoText;
176 customAdditionalInfo = null;
178 if (n.AdditionalInfo.Type ==
AdditionalInfo.AdditionalInfoType.CUSTOM)
180 customAdditionalInfo = n.AdditionalInfo.CustomUIElement;
185 hideAdditionalInformation();
192 public void deleteNotoficationPressed(GameObject sender)
194 for (
int i = notitficationList.Count - 1; i >= 0; i--)
198 notitficationList.RemoveAt(i);
202 hideAdditionalInformation();
203 updateNotificationCenter();
207 private void orderNotitficationList(){
208 notitficationList.Sort((x, y) => {
209 int result = x.ExpireDate.CompareTo(y.ExpireDate);
211 result = x.CreationDate.CompareTo(y.CreationDate);
217 public void showAdditionalInformation()
219 if(notitficationList.Count > 0 && notitficationList[0].AdditionalInfo != null)
223 additionalInformation.SetActive(
true);
226 customAdditionalInfo.SetActive(
true);
232 public void hideAdditionalInformation()
234 additionalInformation.SetActive(
false);
235 if(customAdditionalInfo != null)
237 customAdditionalInfo.SetActive(
false);
Used for notification game object to reference the acutal notifiaction.
void createNotification(string text, TimeSpan timeToLive, Sprite notificationSprite=null, AdditionalInfo additionalInfo=null)
Convenience overload.