@@ -19,6 +19,8 @@ The example code below contains both synchronous and asynchronous APIs, so pleas
19
19
``` csharp
20
20
using System .Collections ;
21
21
using ArenaUnity ;
22
+ using ArenaUnity .Schemas ;
23
+ using Newtonsoft .Json ;
22
24
using UnityEngine ;
23
25
using UnityEngine .UI ;
24
26
@@ -136,6 +138,9 @@ public class ArenaTestButton : MonoBehaviour
136
138
// Display the web browser GUI client URL, set after MQTT connection flow completes.
137
139
Debug .Log ($" Scene URL: {scene .sceneUrl }" );
138
140
141
+ // Instantiate the callback for all messages.
142
+ scene .OnMessageCallback = MessageCallback ;
143
+
139
144
// Create GameObject, and add ArenaObject script to manage updates, it will automatically send an MQTT create message
140
145
GameObject cube = GameObject .CreatePrimitive (PrimitiveType .Cube );
141
146
ArenaObject arenaObject = cube .AddComponent (typeof (ArenaObject )) as ArenaObject ;
@@ -146,15 +151,54 @@ public class ArenaTestButton : MonoBehaviour
146
151
// Publish a custom event under this client's "camera" object
147
152
scene .PublishEvent (" my-custom-event-type" , scene .camid , " {\" my-attribute\" : \" my-custom-attribute\" }" );
148
153
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
+ }
152
169
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 );
155
199
156
200
// 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 );
158
202
}
159
203
160
204
/// <summary >
@@ -191,8 +235,7 @@ public class ArenaTestButton : MonoBehaviour
191
235
Debug .Log ($" Permissions: {client .permissions }" );
192
236
193
237
// Custom MQTT pub/sub
194
- string [] topics = new string [] { " my/custom/topic/#" };
195
- client .Subscribe (topics );
238
+ client .Subscribe (" my/custom/topic/#" );
196
239
197
240
yield return new WaitForSeconds (2 );
198
241
client .Publish (" my/custom/topic/channel/device-888" , System .Text .Encoding .UTF8 .GetBytes (" some payload" ));
0 commit comments