• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by m3ta · Feb 08, 2016 at 10:19 AM · c#user interfacedesign-patterns

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.

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment
m3ta

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image m3ta · Feb 12, 2016 at 01:39 PM 0
Share

Great answer, thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Programatically creating and setting UI image 0 Answers

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

How to implement dialogue system whilst disabling movement and shooting control? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges