Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- /*
- * Notification in your game
- * Call Notifications.AddMessage(YOUR_MESSAGE);
- * My settings in inspector https://imgur.com/a/6UmL884
- * for ask: discord: av.stn
- */
- namespace avstn.notifications
- {
- public class Notifications : MonoBehaviour
- {
- public float speedFadeOut;
- public int timeDisplayNotif = 3;
- public GUIStyle style;
- public Vector2 msgOffs;
- public Vector2 msgSize;
- private Vector2 _msgOffs;
- private float _alphaCh = 1;
- private bool isFadeOut = false;
- private List<string> messages;
- private static Notifications cache;
- private const int MAX_NOTIFY_MESSAGES = 5;
- private float AlphaCh
- {
- get { return _alphaCh; }
- set
- {
- _alphaCh = value;
- if (_alphaCh <= 0)
- {
- isFadeOut = false;
- messages.Clear();
- _msgOffs = msgOffs;
- _alphaCh = 1;
- }
- }
- }
- private void Awake()
- {
- cache = this;
- }
- private void Start()
- {
- messages = new List<string>();
- if (timeDisplayNotif <= 0)
- timeDisplayNotif = 3;
- }
- private void Update()
- {
- if (isFadeOut && AlphaCh > 0)
- AlphaCh -= Time.deltaTime * speedFadeOut;
- }
- private void OnGUI()
- {
- if (messages.Count == 0)
- return;
- Color col = GUI.color;
- GUI.color = new Color(col.r, col.g, col.b, _alphaCh);
- for (int i = 0; i < messages.Count; i++)
- GUI.Box(new Rect(new Vector2(_msgOffs.x, _msgOffs.y + (msgSize.y * i)), msgSize), messages[i], style);
- GUI.color = col;
- }
- public static void AddMessage(string message)
- {
- cache.addMessage(message);
- }
- private void addMessage(string msg)
- {
- CancelInvoke("runFadeOut");
- isFadeOut = false;
- _alphaCh = 1;
- if (messages.Count == MAX_NOTIFY_MESSAGES)
- {
- for (int i = 1; i < MAX_NOTIFY_MESSAGES; i++)
- cache.messages[i - 1] = messages[i];
- messages[MAX_NOTIFY_MESSAGES - 1] = msg;
- }
- else
- messages.Add(msg);
- Invoke("runFadeOut", timeDisplayNotif);
- _msgOffs = new Vector2(msgOffs.x, msgOffs.y - (messages.Count * msgSize.y));
- }
- private void runFadeOut()
- {
- isFadeOut = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement