6 using System.Collections.Generic;
10 #if !UNITY_EDITOR && UNITY_WSA 11 using Windows.Networking;
12 using Windows.Networking.Connectivity;
36 if (objects.Length != 1)
42 _Instance = objects[0];
65 return (isServer || !running);
78 public Dictionary<string, SessionInfo> remoteSessions =
new Dictionary<string, SessionInfo>();
89 public int BroadcastInterval = 1000;
96 public string ServerIp {
get;
private set; }
101 public string LocalIp {
get;
set; }
107 private bool CheckComponents()
109 #if !UNITY_EDITOR && UNITY_WSA 112 Debug.Log(
"Need a UNetNetworkTransmitter in the scene for sending anchor data");
116 if (NetworkManager.singleton == null)
118 Debug.Log(
"Need a NetworkManager in the scene");
127 #if !UNITY_EDITOR && UNITY_WSA 129 foreach (HostName hostName
in NetworkInformation.GetHostNames())
131 if (hostName.DisplayName.Split(
".".ToCharArray()).Length == 4)
133 Debug.Log(
"Local IP " + hostName.DisplayName);
134 LocalIp = hostName.DisplayName;
139 LocalIp =
"editor" +
UnityEngine.Random.Range(0, 999999).ToString(); ;
148 if (!CheckComponents())
150 Debug.Log(
"Invalid configuration detected. Network Discovery disabled.");
155 broadcastInterval = BroadcastInterval;
157 broadcastData = GetLocalComputerName() +
'\0';
167 private string GetLocalComputerName()
169 #if !UNITY_EDITOR && UNITY_WSA 170 foreach (HostName hostName
in NetworkInformation.GetHostNames())
172 if (hostName.Type == HostNameType.DomainName)
175 Debug.Log(
"My name is " + hostName.DisplayName);
176 return hostName.DisplayName;
179 return "NotSureWhatMyNameIs";
181 return System.Environment.ExpandEnvironmentVariables(
"%ComputerName%");
189 private void MaybeInitAsServer()
191 StartCoroutine(InitAsServer());
194 private IEnumerator InitAsServer()
196 Debug.Log(
"Acting as host");
197 #if !UNITY_EDITOR && UNITY_WSA 198 NetworkManager.singleton.serverBindToIP =
true;
199 NetworkManager.singleton.serverBindAddress = LocalIp;
213 NetworkManager.singleton.StartHost();
222 #if !UNITY_EDITOR && UNITY_WSA 226 Debug.LogWarning(
"This script will need modification to work in the Unity Editor");
238 ServerIp = fromAddress.Substring(fromAddress.LastIndexOf(
':') + 1);
240 if (remoteSessions.TryGetValue(ServerIp, out sessionInfo) ==
false)
242 Debug.Log(
"new session: " + fromAddress);
244 remoteSessions.Add(ServerIp,
new SessionInfo() { SessionIp = ServerIp, SessionName = data });
245 SignalSessionListEvent();
256 remoteSessions.Clear();
277 NetworkManager.singleton.networkAddress = ServerIp;
278 #if !UNITY_EDITOR && UNITY_WSA 282 Debug.LogWarning(
"This script will need modification to work in the Unity Editor");
285 NetworkManager.singleton.StartClient();
286 SignalConnectionStatusEvent();
297 #if !UNITY_EDITOR && UNITY_WSA 298 NetworkManager.singleton.serverBindToIP =
true;
299 NetworkManager.singleton.serverBindAddress = LocalIp;
305 NetworkManager.singleton.StartHost();
309 #if !UNITY_EDITOR && UNITY_WSA 311 Invoke(
"InvokeCreateAnchor", 0.25f);
313 Debug.LogWarning(
"This script will need modification to work in the Unity Editor");
316 SignalSessionListEvent();
317 SignalConnectionStatusEvent();
324 void InvokeCreateAnchor()
332 void SignalSessionListEvent()
334 EventHandler<EventArgs> sessionListChanged = SessionListChanged;
335 if (sessionListChanged != null)
337 sessionListChanged(
this, EventArgs.Empty);
344 void SignalConnectionStatusEvent()
346 EventHandler<EventArgs> connectionEvent = this.ConnectionStatusChanged;
347 if (connectionEvent != null)
349 connectionEvent(
this, EventArgs.Empty);