From 80c4983b344a81a021e3b87b8d73f782ef9f8e28 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 6 Jun 2025 11:52:35 +0200 Subject: [PATCH] Fix issue with things from different participants --- Unity/Participant.cs | 6 +++++- Unity/SiteServer.cs | 23 ++++++++++++++++------- src/Participant.cs | 8 ++++++++ src/Participants/ParticipantUDP.cs | 10 +--------- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/Unity/Participant.cs b/Unity/Participant.cs index feee822..b7aec55 100644 --- a/Unity/Participant.cs +++ b/Unity/Participant.cs @@ -48,7 +48,11 @@ namespace RoboidControl.Unity { if (coreThing.component == null) { Thing[] things = FindObjectsByType(FindObjectsSortMode.None); // Debug.Log(things.Length); - Thing thing = things.FirstOrDefault(t => t.core != null && t.core.id == coreThing.id); + Thing thing = things.FirstOrDefault(t => + t.core != null && + t.core.owner.networkId == coreThing.owner.networkId && + t.core.id == coreThing.id + ); if (thing == null) thing = Thing.Create(coreThing); coreThing.component = thing; diff --git a/Unity/SiteServer.cs b/Unity/SiteServer.cs index da5e0bc..522e3af 100644 --- a/Unity/SiteServer.cs +++ b/Unity/SiteServer.cs @@ -1,16 +1,14 @@ #if UNITY_5_3_OR_NEWER using System; -using System.Collections.Generic; using UnityEngine; namespace RoboidControl.Unity { public class SiteServer : Participant { - //public RoboidControl.SiteServer site; public RoboidControl.SiteServer site => this.coreParticipant as RoboidControl.SiteServer; - - //public Queue thingQueue = new(); + public string modelURL; + private RoboidControl.Thing model; protected virtual void Awake() { #if RC_DEBUG @@ -18,11 +16,20 @@ namespace RoboidControl.Unity { #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(); + site.Close(); } protected override void Update() { @@ -33,8 +40,6 @@ namespace RoboidControl.Unity { HandleUpdateEvent(e); site.Update(); - // while (thingQueue.TryDequeue(out RoboidControl.Thing thing)) - // thing.CreateComponent(); } private void HandleUpdateEvent(RoboidControl.Participant.UpdateEvent e) { @@ -46,6 +51,10 @@ namespace RoboidControl.Unity { 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); diff --git a/src/Participant.cs b/src/Participant.cs index 2bebcb3..b1071b3 100644 --- a/src/Participant.cs +++ b/src/Participant.cs @@ -191,6 +191,14 @@ namespace RoboidControl { return true; } + public void SendThingInfo(Thing thing) { + this.Send(new ThingMsg(thing.owner.networkId, thing)); + this.Send(new NameMsg(thing.owner.networkId, thing)); + this.Send(new ModelUrlMsg(thing.owner.networkId, thing)); + this.Send(new PoseMsg(thing.owner.networkId, thing)); + this.Send(new BinaryMsg(thing.owner.networkId, thing)); + } + #endregion Send #region Participant Registry diff --git a/src/Participants/ParticipantUDP.cs b/src/Participants/ParticipantUDP.cs index 5ea154c..40b81b5 100644 --- a/src/Participants/ParticipantUDP.cs +++ b/src/Participants/ParticipantUDP.cs @@ -232,14 +232,6 @@ namespace RoboidControl { #region Send - public void SendThingInfo(Participant owner, Thing thing) { - owner.Send(new ThingMsg(this.networkId, thing)); - owner.Send(new NameMsg(this.networkId, thing)); - owner.Send(new ModelUrlMsg(this.networkId, thing)); - owner.Send(new PoseMsg(this.networkId, thing)); - owner.Send(new BinaryMsg(this.networkId, thing)); - } - public void PublishThingInfo(Thing thing) { // Console.WriteLine("Publish thing info"); this.Publish(new ThingMsg(this.networkId, thing)); @@ -386,7 +378,7 @@ namespace RoboidControl { if (this.networkId != msg.networkId) { this.networkId = msg.networkId; foreach (Thing thing in this.things) //Thing.GetAllThings()) - this.SendThingInfo(sender, thing); + sender.SendThingInfo(thing); } }