Advertisement
halleman19

push notify in game | unity3d | c#

Feb 2nd, 2025 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | Gaming | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. /*
  5.  * Notification in your game
  6.  * Call Notifications.AddMessage(YOUR_MESSAGE);
  7.  * My settings in inspector https://imgur.com/a/6UmL884
  8.  * for ask: discord: av.stn
  9.  */
  10.  
  11. namespace avstn.notifications
  12. {
  13.     public class Notifications : MonoBehaviour
  14.     {
  15.         public float speedFadeOut;
  16.  
  17.         public int timeDisplayNotif = 3;
  18.  
  19.         public GUIStyle style;
  20.  
  21.         public Vector2 msgOffs;
  22.         public Vector2 msgSize;
  23.  
  24.         private Vector2 _msgOffs;
  25.  
  26.         private float _alphaCh = 1;
  27.         private bool isFadeOut = false;
  28.  
  29.         private List<string> messages;
  30.  
  31.         private static Notifications cache;
  32.  
  33.         private const int MAX_NOTIFY_MESSAGES = 5;
  34.  
  35.         private float AlphaCh
  36.         {
  37.             get { return _alphaCh; }
  38.             set
  39.             {
  40.                 _alphaCh = value;
  41.  
  42.                 if (_alphaCh <= 0)
  43.                 {
  44.                     isFadeOut = false;
  45.                     messages.Clear();
  46.                     _msgOffs = msgOffs;
  47.                     _alphaCh = 1;
  48.                 }
  49.             }
  50.         }
  51.  
  52.         private void Awake()
  53.         {
  54.             cache = this;
  55.         }
  56.  
  57.         private void Start()
  58.         {
  59.             messages = new List<string>();
  60.  
  61.             if (timeDisplayNotif <= 0)
  62.                 timeDisplayNotif = 3;
  63.         }
  64.  
  65.         private void Update()
  66.         {
  67.             if (isFadeOut && AlphaCh > 0)
  68.                 AlphaCh -= Time.deltaTime * speedFadeOut;
  69.         }
  70.  
  71.         private void OnGUI()
  72.         {
  73.             if (messages.Count == 0)
  74.                 return;
  75.  
  76.             Color col = GUI.color;
  77.  
  78.             GUI.color = new Color(col.r, col.g, col.b, _alphaCh);
  79.  
  80.             for (int i = 0; i < messages.Count; i++)
  81.                 GUI.Box(new Rect(new Vector2(_msgOffs.x, _msgOffs.y + (msgSize.y * i)), msgSize), messages[i], style);
  82.  
  83.             GUI.color = col;
  84.         }
  85.  
  86.         public static void AddMessage(string message)
  87.         {
  88.             cache.addMessage(message);
  89.         }
  90.  
  91.         private void addMessage(string msg)
  92.         {
  93.             CancelInvoke("runFadeOut");
  94.  
  95.             isFadeOut = false;
  96.             _alphaCh = 1;
  97.  
  98.             if (messages.Count == MAX_NOTIFY_MESSAGES)
  99.             {
  100.                 for (int i = 1; i < MAX_NOTIFY_MESSAGES; i++)
  101.                     cache.messages[i - 1] = messages[i];
  102.  
  103.                 messages[MAX_NOTIFY_MESSAGES - 1] = msg;
  104.             }
  105.             else
  106.                 messages.Add(msg);
  107.  
  108.             Invoke("runFadeOut", timeDisplayNotif);
  109.  
  110.             _msgOffs = new Vector2(msgOffs.x, msgOffs.y - (messages.Count * msgSize.y));
  111.         }
  112.  
  113.         private void runFadeOut()
  114.         {
  115.             isFadeOut = true;
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement