AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
UserNotifications.cs
Go to the documentation of this file.
1 // Licensed under the MIT License. See LICENSE in the project root for license information.
2 
3 using System;
4 using UnityEngine;
5 
6 namespace HoloToolkit.Sharing.Tests
7 {
11  public class UserNotifications : MonoBehaviour
12  {
13  private SessionUsersTracker usersTracker;
14  private static User localUser = null;
15 
16  private void Start()
17  {
18  // SharingStage should be valid at this point, but we may not be connected.
19  if (SharingStage.Instance.IsConnected)
20  {
21  Connected();
22  }
23  else
24  {
25  SharingStage.Instance.SharingManagerConnected += Connected;
26  }
27  }
28 
29  private void Connected(object sender = null, EventArgs e = null)
30  {
31  SharingStage.Instance.SharingManagerConnected -= Connected;
32 
33  usersTracker = SharingStage.Instance.SessionUsersTracker;
34  localUser = SharingStage.Instance.Manager.GetLocalUser();
35 
36  usersTracker.UserJoined += NotifyUserJoined;
37  usersTracker.UserLeft += NotifyUserLeft;
38  }
39 
40  private void NotifyUserJoined(User user)
41  {
42  if (user.IsValid() && localUser.GetID() != user.GetID())
43  {
44  Debug.LogFormat("[User Notifications] User {0} has joined the room.", user.GetName());
45  }
46  }
47 
48  private void NotifyUserLeft(User user)
49  {
50  if (user.IsValid() && localUser.GetID() != user.GetID())
51  {
52  Debug.LogFormat("[User Notifications] User {0} has left the room.", user.GetName());
53  }
54  }
55 
56  private void OnDestroy()
57  {
58  if (usersTracker != null)
59  {
60  usersTracker.UserJoined -= NotifyUserJoined;
61  usersTracker.UserLeft -= NotifyUserLeft;
62  }
63  usersTracker = null;
64  }
65  }
66 }
virtual bool IsValid()
Definition: User.cs:54
Action< User > UserJoined
UserJoined event notifies when a user joins the current session.
Keeps track of the users in the current session. Instance is created by Sharing Stage when a connecti...
virtual int GetID()
Definition: User.cs:49
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
Used to demonstrate how to get notifications when users leave and enter room.
The SharingStage is in charge of managing the core networking layer for the application.
Definition: SharingStage.cs:14
Action< User > UserLeft
UserLeft event notifies when a user leaves the current session.
virtual XString GetName()
Definition: User.cs:43