AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ScrollingSessionListUIController.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 using System.Collections.Generic;
4 using UnityEngine;
5 using System;
6 using System.Linq;
7 
8 namespace HoloToolkit.Unity.SharingWithUNET
9 {
13  public class ScrollingSessionListUIController : SingleInstance<ScrollingSessionListUIController>
14  {
18  private NetworkDiscoveryWithAnchors networkDiscovery;
19 
24  private Dictionary<string, NetworkDiscoveryWithAnchors.SessionInfo> sessionList;
25 
30  private int SessionIndex = 0;
31 
36 
40  public NetworkDiscoveryWithAnchors.SessionInfo SelectedSession { get; private set; }
41 
42  private void Start()
43  {
44  // On the immersive device the UI is best placed a little closer than on a HoloLens
45 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
46  if (UnityEngine.XR.WSA.HolographicSettings.IsDisplayOpaque)
47  {
48  gameObject.GetComponent<SimpleTagalong>().TagalongDistance = 1;
49  }
50 #endif
51 
52  // Register for events when sessions are found / joined.
53  networkDiscovery = NetworkDiscoveryWithAnchors.Instance;
54  networkDiscovery.SessionListChanged += NetworkDiscovery_SessionListChanged;
55  networkDiscovery.ConnectionStatusChanged += NetworkDiscovery_ConnectionStatusChanged;
56  ScrollSessions(0);
57  }
58 
64  private void NetworkDiscovery_ConnectionStatusChanged(object sender, EventArgs e)
65  {
66  gameObject.SetActive(networkDiscovery.running && !networkDiscovery.isServer);
67  // sets the UI to be active when not connected and disabled when connected.
68  //SetChildren(networkDiscovery.running && !networkDiscovery.isServer);
69  }
70 
76  private void NetworkDiscovery_SessionListChanged(object sender, EventArgs e)
77  {
78  sessionList = networkDiscovery.remoteSessions;
79  SessionIndex = Mathf.Min(SessionIndex, sessionList.Count);
80 
81  // this will force a recalculation of the buttons.
82  ScrollSessions(0);
83  }
84 
89  void SetChildren(bool Enabled)
90  {
91  foreach (Renderer mr in GetComponentsInChildren<Renderer>())
92  {
93  mr.enabled = Enabled;
94  }
95 
96  foreach (BoxCollider bc in GetComponentsInChildren<BoxCollider>())
97  {
98  bc.enabled = Enabled;
99  }
100  }
101 
107  public void ScrollSessions(int Direction)
108  {
109  int sessionCount = sessionList == null ? 0 : sessionList.Count;
110  // Update the session index
111  SessionIndex = Mathf.Clamp(SessionIndex + Direction, 0, Mathf.Max(0, sessionCount - SessionControls.Length));
112 
113  // Update all of the controls
114  for (int index = 0; index < SessionControls.Length; index++)
115  {
116  if (SessionIndex + index < sessionCount)
117  {
118  SessionControls[index].gameObject.SetActive(true);
119  NetworkDiscoveryWithAnchors.SessionInfo sessionInfo = sessionList.Values.ElementAt(SessionIndex + index);
120  SessionControls[index].SetSessionInfo(sessionInfo);
121  }
122  else
123  {
124  SessionControls[index].gameObject.SetActive(false);
125  }
126  }
127  }
128 
134  {
135  SelectedSession = sessionInfo;
136  // Recalculating the session list so we can update the text colors.
137  ScrollSessions(0);
138  }
139 
143  public void JoinSelectedSession()
144  {
145  if (SelectedSession != null && networkDiscovery.running)
146  {
147  networkDiscovery.JoinSession(SelectedSession);
148  }
149  }
150  }
151 }
A Tagalong that stays at a fixed distance from the camera and always seeks to have a part of itself i...
void SetSessionInfo(NetworkDiscoveryWithAnchors.SessionInfo sessionInfo)
Sets the session information associated with this button
Inherits from UNet&#39;s NetworkDiscovery script. Adds automatic anchor management on discovery...
void ScrollSessions(int Direction)
Updates which session is the &#39;top&#39; session in the list, and sets the session buttons accordingly ...
void JoinSession(SessionInfo session)
Call to join a session
SessionListButton [] SessionControls
List of session controls that will have the session info on them.
Represents a button on a list of sessions. Tapping the button indicates the selected session ...
A simplified version of the Singleton class which doesn&#39;t depend on the Instance being set in Awake ...
Dictionary< string, SessionInfo > remoteSessions
Keeps track of current remote sessions.
void JoinSelectedSession()
Joins the selected session if there is a selected session.
void SetSelectedSession(NetworkDiscoveryWithAnchors.SessionInfo sessionInfo)
Sets the selected session