RoboidControl-csharp/Unity/SiteServer.cs

67 lines
2.0 KiB
C#

#if UNITY_5_3_OR_NEWER
using System;
using UnityEngine;
namespace RoboidControl.Unity {
public class SiteServer : Participant {
public RoboidControl.SiteServer site => this.coreParticipant as RoboidControl.SiteServer;
public string modelURL;
private RoboidControl.Thing model;
protected virtual void Awake() {
#if RC_DEBUG
Console.SetOut(new UnityLogWriter());
#endif
this.coreParticipant = new RoboidControl.SiteServer(port);
#if GLTF
if (!string.IsNullOrEmpty(modelURL)) {
model = new() {
name = "Model",
modelUrl = this.modelURL
};
}
#endif
}
void OnApplicationQuit() {
if (site != null)
site.Close();
}
protected override void Update() {
if (site == null)
return;
while (site.updateQueue.TryDequeue(out RoboidControl.Participant.UpdateEvent e))
HandleUpdateEvent(e);
site.Update();
}
private void HandleUpdateEvent(RoboidControl.Participant.UpdateEvent e) {
switch (e.messageId) {
case ParticipantMsg.Id:
GameObject remoteParticipant = new GameObject("RemoteParticipant");
Participant participant = remoteParticipant.AddComponent<Participant>();
participant.coreParticipant = e.participant;
participant.coreParticipant.component = participant;
participant.ipAddress = e.participant.ipAddress;
participant.port = e.participant.port;
foreach (RoboidControl.Thing thing in this.site.things)
participant.coreParticipant.SendThingInfo(thing);
break;
case ThingMsg.id:
HandleThingEvent(e);
break;
}
}
}
}
#endif