Notification area for Civ type game
I'm making a notification area for a turn-based strategy game, notification 'messages' are sent to the player at the start of turns and when significant events occur.
I wrote this class but I'm not sure how to get the rest of the functionality I need -
// Notification object
public class Notify
{
public GameObject notifier;
public GameObject notification_Area;
public GameObject notificationText;
public string m;
public Notify(string message)
{
string m = message;
// Instantiate notification icon from prefab
GameObject notification = Instantiate(Resources.Load("notifier")) as GameObject;
// Set notificationArea as parent
notification_Area = GameObject.Find("notificationArea");
notification.transform.SetParent(notification_Area.transform, false);
// Set message for modal
notificationText = GameObject.Find("notificationText");
notificationText.GetComponent<Text>().text = m;
}
public void Notification()
{
// Set message for modal
notificationText = GameObject.Find("notificationText");
notificationText.GetComponent<Text>().text = m;
}
At the moment the Notify object is linked to a button prefab that has a behaviour script that makes a notification window visible and invisible onclick. The Notify object sets the text for this window, but I want to have multiple notifications so this solution doesn't really work. I need each new instance of Notify to open its own notification window when clicked, with the appropriate text from the message string...can't think how to achieve this.
Answer by JoshuaMcKenzie · Feb 11, 2016 at 07:46 AM
@m3ta I assume you want only one notification to show at a time, but still remember all unread notifications? You likely want a your notificationArea script to hold a queue (cause we want to read them in FIFO order) of notifications which loads in the next message when an external component tries to statically open the modal. Since I am going off the assumption that there will only be one modal instance we can make it a singleton (in a sense):
public class UI_NotificationArea :MonoBehaviour
{
public Text messageBox;
private CanvasGroup grp;
private static UI_NotificationArea instance;
private static Queue<string> notifications = new Queue<string>();
//in case you want your button to use a different sprite based on
// if there's any current Notifications. we can provide this while
// keeping the Queue encapsulated
public static bool HasNotifications { get { return notifications.Count>0; } }
public static void Notify(string notification)
{
notifications.Enqueue(notification);
}
public static void ClearNotifications()
{
notifications.Clear();
}
public static void Open()// i.e your Notification()
{
if(instance!=null && instance.messageBox != null && HasNotifications)
{
// set the message to the current notification
instance.messageBox.text = notifications.Peek();
// show the modal
instance.grp.alpha = 1;
instance.grp.blocksRaycasts = true;
}
}
void Awake()
{
instance = this;
grp = GetComponent<CanvasGroup>();
}
//button functions
public void OnOKClick()
{
notifications.Dequeue();
grp.alpha = 0;
grp.blocksRaycasts = false;
}
public void OnCancelClick()
{
grp.alpha = 0;
grp.blocksRaycasts = false;
}
}
You can have your Notification Area already be in the scene or instantiate it later. the benefit is that you can call Notify() before its instantiated and it can keep track of any notifications that have been presented. you can also choose to clear later without having to go find it (in the event the level changes for example).
then your Notification Button just needs this code:
public class UI_NotificationButton : MonoBehaviour
{
//function to run when the Notification button is clicked
public void OpenNotificationArea()
{
UI_NotificationArea.Open();
}
}
pretty simple. the button doesn't need to go find it, nor need know about its properties (aside from Open). it doesn't even need to mess with the notifications. honestly you can probably move the function into a UI gameObject that the button is parented to to reduce the number of classes.
Your answer
![](https://koobas.hobune.stream/wayback/20220612062928im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Notifying about character/item selection 1 Answer
How can i ask a user for their name? 1 Answer
How to change UI Image color based on speed 1 Answer
Are C# Interfaces purely aesthetic? 2 Answers
Trying to make a tape measure? 0 Answers