-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathColoDiscoAnchor.cs
More file actions
357 lines (281 loc) · 9.96 KB
/
ColoDiscoAnchor.cs
File metadata and controls
357 lines (281 loc) · 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Meta.XR.Samples;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Sampleton = SampleController; // only transitional
/// <summary>
/// A sample extension of <see cref="OVRSpatialAnchor"/>, showcasing Colocation Session Discovery and Group Sharing.
/// </summary>
/// <remarks>
/// All key API calls relating to this component are implemented in <see cref="ColoDiscoMan"/>. This is in contrast to
/// how <see cref="SharedAnchor"/> and <see cref="SharedAnchorLoader"/> are split.
/// <br/><br/>
/// <see cref="OVRSpatialAnchor"/> allows us to inherit from it, however we must beware of which Unity Messages we
/// choose to implement! <br/>
/// Undefined states are known to occur if we implement any of:
/// <ul>
/// <li>Start</li>
/// <li>Update</li>
/// <li>LateUpdate</li>
/// <li>OnDestroy</li>
/// </ul>
/// Otherwise, this subclassing is OK.
/// </remarks>
[MetaCodeSample("SharedSpatialAnchors-ColocationSessionGroups")]
public class ColoDiscoAnchor : OVRSpatialAnchor
{
//
// Public API:
// instance
public bool IsSaved { get; set; }
public AnchorSource Source
{
get => m_Source;
set
{
if (m_Source.Equals(value))
return;
if (m_Source.IsSet)
{
Sampleton.LogError($"{name}.{nameof(Source)} is already set!");
return;
}
m_Source = value;
}
}
public void UpdateUI()
{
gameObject.name = $"{Source}:{Uuid:N}";
m_UuidLabel.text = $"{Uuid}\n{nameof(Source)}: {Source}";
if (Alignment.OriginAnchor == this)
{
m_AlignIcon.color = SampleColors.Green;
m_AlignBtn.interactable = false;
}
else
{
m_AlignIcon.color = SampleColors.Gray;
m_AlignBtn.interactable = true;
}
if (LocallySaved.AnchorIsRemembered(Uuid))
{
m_RememberIcon.color = SampleColors.Green;
m_RememberLabel.SetText("Forget UUID");
}
else
{
m_RememberIcon.color = SampleColors.Gray;
m_RememberLabel.SetText("Remember UUID");
}
m_RememberBtn.interactable = true;
var groups = ColoDiscoMan.GetGroupsFor(Uuid);
list2ui(ColoDiscoMan.GetDisplayNamesFor(groups), m_GroupListLabel);
return;
// local function section:
static void list2ui<T>(IEnumerable<T> list, TMP_Text ui)
{
if (!ui)
return;
s_Stringer.Clear();
foreach (var thing in list)
{
s_Stringer.AppendLine(thing.ToString());
}
ui.SetText(s_Stringer);
}
}
//
// UnityEvent Callbacks (for Buttons)
public void Hide()
{
Sampleton.Log($"{nameof(ColoDiscoAnchor)}.{nameof(Hide)}: {Uuid.Brief()}");
Destroy(gameObject);
// NOTE: "Destroy" == "Hide" for anchors only if they have been saved locally or shared.
// Otherwise, this is a proper deletion as far as your app is concerned; even if you saved its Uuid prior to
// destroying the anchor, there is no guarantee you'll be able to load it back (although it isn't guaranteed
// that you *can't* load it back, either.. better !!! than ???)
}
public async void Erase()
{
var uuid = Uuid;
Sampleton.Log($"{nameof(ColoDiscoAnchor)}.{nameof(Erase)}: {uuid}");
// API call: instance OVRSpatialAnchor.EraseAnchorAsync()
var result = await EraseAnchorAsync();
var loggedResult = result.Status.ForLogging();
if (!result.Success)
{
if (!Source.IsMine)
{
loggedResult += $"\n (You aren't the original creator of {uuid.Brief()}.)";
if (m_EraseIcon)
m_EraseIcon.color = SampleColors.Yellow;
}
else if (m_EraseIcon)
{
m_EraseIcon.color = SampleColors.Alert;
}
Sampleton.LogError($"- Erase Anchor FAILED! {loggedResult}");
return;
}
LocallySaved.IgnoreAnchor(uuid);
LocallySaved.CommitToDisk();
ColoDiscoMan.NotifyAnchorErased(uuid);
Destroy(gameObject);
Sampleton.Log($"+ Erase Anchor: {loggedResult}");
if (Source.IsMine)
return;
Sampleton.Log(
$"- WARNING: You aren't the original creator of {uuid.Brief()}." +
"\n (The owner, you, or other sharees may see undefined behaviours.)",
LogType.Warning
);
}
public void Share()
{
ColoDiscoMan.ShareToActiveGroup(this);
}
public void SetAsAlignmentAnchor()
{
Sampleton.Log($"{nameof(SetAsAlignmentAnchor)}: {gameObject.name}");
var realignObjects = ColoDiscoMan.CurrentNonAnchoredObjects;
Alignment.SetOrigin(this, realignObjects);
}
public async void ToggleRememberUuid()
{
if (LocallySaved.AnchorIsRemembered(Uuid))
{
if (LocallySaved.ForgetAnchor(Uuid))
Sampleton.Log($"+ Forget Anchor: {SampleColors.RichText.Noice}Success</color>");
else
Sampleton.LogError($"- Forget Anchor: {SampleColors.RichText.Alert}Failure</color>");
}
else
{
string loggedResult = null;
if (!IsSaved)
{
var saveResult = await SaveAnchorAsync();
IsSaved = saveResult.Success;
loggedResult = saveResult.Status.ForLogging();
}
IsSaved = IsSaved && LocallySaved.RememberAnchor(Uuid, Source.IsMine);
if (IsSaved)
Sampleton.Log($"+ Remember (Save) Anchor: {loggedResult}");
else
Sampleton.LogError($"- Remember (Save) Anchor FAILED! (SaveAnchorAsync returned {loggedResult})");
}
UpdateUI();
LocallySaved.CommitToDisk();
}
//
// Fields & Constants
[Header("[ReadOnly] - no need to touch these manually:")]
[SerializeField]
TMP_Text m_UuidLabel;
[SerializeField]
TMP_Text m_GroupListLabel;
[SerializeField]
Button m_AlignBtn;
[SerializeField]
Image m_AlignIcon;
[SerializeField]
Button m_RememberBtn;
[SerializeField]
Image m_RememberIcon;
[SerializeField]
TMP_Text m_RememberLabel;
[SerializeField]
Image m_EraseIcon;
AnchorSource m_Source;
static readonly System.Text.StringBuilder s_Stringer = new();
//
// Unity Messages
void OnValidate()
{
var idLabel = transform.FindChildRecursive("ID Label");
if (!idLabel || !idLabel.TryGetComponent(out m_UuidLabel))
{
Debug.LogError($"\"{name}\" seems to be improperly set-up.", this);
}
var btn = transform.FindChildRecursive("Button: Align");
if (!btn || !btn.TryGetComponent(out m_AlignBtn))
{
Debug.LogError($"\"{name}\" seems to be improperly set-up.", this);
}
else
{
var icon = m_AlignBtn.transform.FindChildRecursive("Icon");
if (!icon || !icon.TryGetComponent(out m_AlignIcon))
Debug.LogError($"\"{name}\" seems to be improperly set-up.", this);
}
btn = transform.FindChildRecursive("Button: Remember UUID");
if (!btn || !btn.TryGetComponent(out m_RememberBtn))
{
Debug.LogError($"\"{name}\" seems to be improperly set-up.", this);
}
else
{
var icon = btn.FindChildRecursive("Icon");
if (!icon || !icon.TryGetComponent(out m_RememberIcon))
Debug.LogError($"\"{name}\" seems to be improperly set-up.", this);
m_RememberLabel = btn.GetComponentInChildren<TMP_Text>();
if (!m_RememberLabel)
Debug.LogError($"\"{name}\" seems to be improperly set-up.", this);
}
btn = transform.FindChildRecursive("Button: Erase");
if (btn)
{
var icon = btn.FindChildRecursive("Icon");
if (icon)
m_EraseIcon = icon.GetComponent<Image>();
}
var listBox = transform.FindChildRecursive("List: Groups Shared");
if (!listBox || !listBox.TryGetComponent(out m_GroupListLabel))
{
Debug.LogError($"\"{name}\" seems to be improperly set-up.", this);
}
}
async void Awake() // Can't override Start, but since we immediately await, Awake is equivalent enough.
{
var canvas = GetComponentInChildren<Canvas>();
if (canvas)
canvas.enabled = false; // don't render controls until creation & localization is complete
// handy API: instance OVRSpatialAnchor.WhenCreatedAsync()
if (await WhenCreatedAsync())
{
Sampleton.Log($"{nameof(ColoDiscoAnchor)}: Created!");
}
else
{
Sampleton.LogError($"{nameof(ColoDiscoAnchor)}: FAILED TO CREATE!\n- destroying instance..");
Destroy(gameObject);
return;
}
var uuid = Uuid;
Sampleton.Log($"+ Uuid: {uuid}");
if (!Source.IsSet)
Source = AnchorSource.New(uuid);
gameObject.name = $"{Source}:{Uuid:N}";
// handy API: instance OVRSpatialAnchor.WhenLocalizedAsync()
if (!await WhenLocalizedAsync())
{
Sampleton.LogError($"- Localization FAILED! ({uuid.Brief()})");
Destroy(gameObject);
return;
}
Sampleton.Log($"+ Loaded spatial anchor {uuid.Brief()}; bound and localized! ({Source.Origin})");
if (canvas)
{
UpdateUI();
canvas.enabled = true;
}
ColoDiscoMan.NotifyAnchorLocalized(this);
}
void OnEnable()
{
UpdateUI();
}
} // end MonoBehaviour ColoDiscoAnchor