Game Development Reference
In-Depth Information
68 //Cycle through all dictionary entries
69 foreach(KeyValuePair<string, List<Component>> Item in Listeners)
70 {
71 //Cycle through all listener objects in list, remove null objects
72 for(int i = Item.Value.Count-1; i>=0; i--)
73 {
74 //If null, then remove item
75 if(Item.Value[i] == null)
76 Item.Value.RemoveAt(i);
77 }
78
79 //If items remain in list for this notification, then add this to tmp
dictionary
80 if(Item.Value.Count > 0)
81 TmpListeners.Add (Item.Key, Item.Value);
82 }
83
84 //Replace listeners object with new, optimized dictionary
85 Listeners = TmpListeners;
86 }
87 //------------------------------------------------
88 //Called when a new level is loaded; remove redundant entries from dictionary; in case
left-over from previous scene
89 void OnLevelWasLoaded()
90 {
91 //Clear redundancies
92 RemoveRedundancies();
93 }
94 //------------------------------------------------
95 }
Working with the NotificationsManager
So how does the NotificationsManager work in practice? Practically every subsequent chapter in
this topic will use it, so we'll see plenty of examples of the class at work. However, to distill the
basics into a simple project and to put things into perspective, let's create a sample application here
that uses the NotificationsManager directly. The steps for creating this project follow.
First, create a new Unity project, if you've not already done so. The project name and import
packages are not critical in themselves—since we're simply building a test-project to try out the
NotificationsManager class. Be sure to the save the default autogenerated scene (I've named mine
TestingNotifications.scene ), and then import the NotificationsManager.cs C# file from Windows
Explorer or Mac Finder into an appropriately named folder inside the Unity Project panel, such as
Scripts (see Figure 3-8 ).
 
Search WWH ::




Custom Search