Skip to content

Commit a53af12

Browse files
committed
docs(unity): update use for private maeesages
1 parent 1c33aa4 commit a53af12

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

content/unity/runtime.md

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ The example code below contains both synchronous and asynchronous APIs, so pleas
1919
```csharp
2020
using System.Collections;
2121
using ArenaUnity;
22+
using ArenaUnity.Schemas;
23+
using Newtonsoft.Json;
2224
using UnityEngine;
2325
using UnityEngine.UI;
2426

@@ -136,6 +138,9 @@ public class ArenaTestButton : MonoBehaviour
136138
// Display the web browser GUI client URL, set after MQTT connection flow completes.
137139
Debug.Log($"Scene URL: {scene.sceneUrl}");
138140

141+
// Instantiate the callback for all messages.
142+
scene.OnMessageCallback = MessageCallback;
143+
139144
// Create GameObject, and add ArenaObject script to manage updates, it will automatically send an MQTT create message
140145
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
141146
ArenaObject arenaObject = cube.AddComponent(typeof(ArenaObject)) as ArenaObject;
@@ -146,15 +151,54 @@ public class ArenaTestButton : MonoBehaviour
146151
// Publish a custom event under this client's "camera" object
147152
scene.PublishEvent("my-custom-event-type", scene.camid, "{\"my-attribute\": \"my-custom-attribute\"}");
148153

149-
// Publish a fully formed json object update message
150-
const string payload = "{\"object_id\":\"scene-options\",\"persist\":true,\"type\":\"scene-options\",\"action\":\"update\",\"data\":{\"env-presets\":{\"ground\":\"none\"}}}";
151-
scene.PublishObject("scene-options", payload);
154+
// Find other arena users in the scene
155+
string firstUserId = null;
156+
ArenaObject[] objlist = FindObjectsOfType<ArenaObject>();
157+
ArenaCamera[] camlist = FindObjectsOfType<ArenaCamera>();
158+
string localUserId = null;
159+
if (camlist.Length > 0)
160+
localUserId = camlist[0].userid;
161+
foreach (ArenaObject obj in objlist)
162+
{
163+
if (obj.object_type == "camera" && obj.name != localUserId)
164+
{
165+
firstUserId = obj.name;
166+
break;
167+
}
168+
}
152169

153-
// Instantiate the callback for all messages.
154-
scene.OnMessageCallback = MessageCallback;
170+
// Publish a private object update message for first user found in scene
171+
ArenaObjectJson msgpriv = new ArenaObjectJson
172+
{
173+
object_id = "cone-private",
174+
action = "create",
175+
type = "object",
176+
persist = false,
177+
data = new ArenaDataJson
178+
{
179+
object_type = "cone"
180+
}
181+
};
182+
string payloadpriv = JsonConvert.SerializeObject(msgpriv);
183+
scene.PublishObject(msgpriv.object_id, payloadpriv, firstUserId);
184+
185+
// Publish a public object update message
186+
ArenaObjectJson msgpub = new ArenaObjectJson
187+
{
188+
object_id = "box-public",
189+
action = "create",
190+
type = "object",
191+
persist = true,
192+
data = new ArenaDataJson
193+
{
194+
object_type = "box"
195+
}
196+
};
197+
string payloadpub = JsonConvert.SerializeObject(msgpub);
198+
scene.PublishObject(msgpub.object_id, payloadpub);
155199

156200
// Manually ingest a message, not received from MQTT subscriber
157-
scene.ProcessMessage("realm/s/public/example/o/scene-options", payload);
201+
scene.ProcessMessage("realm/s/public/example/o/scene-options", payloadpub);
158202
}
159203

160204
/// <summary>
@@ -191,8 +235,7 @@ public class ArenaTestButton : MonoBehaviour
191235
Debug.Log($"Permissions: {client.permissions}");
192236

193237
// Custom MQTT pub/sub
194-
string[] topics = new string[] { "my/custom/topic/#" };
195-
client.Subscribe(topics);
238+
client.Subscribe("my/custom/topic/#");
196239

197240
yield return new WaitForSeconds(2);
198241
client.Publish("my/custom/topic/channel/device-888", System.Text.Encoding.UTF8.GetBytes("some payload"));

0 commit comments

Comments
 (0)