-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSampleExtensions.cs
More file actions
158 lines (119 loc) · 4.9 KB
/
SampleExtensions.cs
File metadata and controls
158 lines (119 loc) · 4.9 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
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Meta.XR.Samples;
using JetBrains.Annotations;
using Meta.XR.MRUtilityKit;
using System;
using System.Text;
using UnityEngine;
[MetaCodeSample("SpaceSharing")]
static class SampleExtensions
{
public static readonly Encoding EncodingForSerialization
= new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
public static bool IsInLoadedRoom(in this Vector3 worldPosition, bool testY = false)
{
if (!MRUK.Instance)
return false;
var room = MRUK.Instance.GetCurrentRoom();
if (!room)
return false;
return room.IsPositionInRoom(worldPosition, testY);
}
public static bool IsInLoadedRoom([CanBeNull] this Transform transform, bool testY = false)
{
return transform && IsInLoadedRoom(transform.position, testY);
}
public static string SafeTypeName(this object box)
{
if (box is null)
return "null";
if (box is not Type type)
return SafeTypeName(box.GetType());
if (type.IsArray)
return $"{SafeTypeName(type.GetElementType())}[]";
// note: the following sugar is not exhaustive,
// as this utility is only used on a known subset of input types anyway.
return Type.GetTypeCode(type) switch
{
TypeCode.String => "string",
TypeCode.Single => "float",
TypeCode.Int32 => "int",
TypeCode.Int64 => "long",
TypeCode.UInt64 => "ulong",
TypeCode.Byte => "byte",
_ => type.Name
};
}
public static string Brief(in this Guid guid)
=> $"{guid.ToString("N").Remove(8)}[..]";
public static string ForLogging(this MRUK.LoadDeviceResult status)
=> StatusForLogging(status, details: false, color: true);
public static string ForLogging(this OVRAnchor.ShareResult status)
=> StatusForLogging(status, details: true, color: true);
public static string StatusForLogging<T>(T status, bool details, bool color = true)
where T : struct, Enum
{
s_StrBuf.Clear();
int raw = (int)(object)status;
if (color)
{
switch (raw)
{
case < 0:
s_StrBuf.Append("<color=#F06080FF>"); // "Alert" red
break;
case > 0:
s_StrBuf.Append("<color=#F09606FF>"); // "Warn" yellow
break;
default:
s_StrBuf.Append("<color=#80FF87FF>"); // "Noice" green
break;
}
}
if (Enum.GetName(typeof(T), status) is { Length: > 0 } namedStatus && char.IsLetter(namedStatus[0]))
s_StrBuf.Append(namedStatus);
else
s_StrBuf.Append((OVRPlugin.Result)(object)status);
s_StrBuf.AppendFormat("({0})", raw);
if (color)
s_StrBuf.Append("</color>");
if (details)
s_StrBuf.Append(StatusExtraDetails(status));
return s_StrBuf.ToString();
}
public static string StatusExtraDetails<T>(T status) where T : struct, Enum
{
switch ((OVRPlugin.Result)(object)status)
{
case OVRPlugin.Result.Success:
break;
case OVRPlugin.Result.Failure_SpaceCloudStorageDisabled:
const string kEnhancedSpatialServicesInfoURL = "https://www.meta.com/help/quest/articles/in-vr-experiences/oculus-features/point-cloud/";
#if UNITY_EDITOR
if (UnityEditor.SessionState.GetBool(kEnhancedSpatialServicesInfoURL, true))
{
UnityEditor.SessionState.SetBool(kEnhancedSpatialServicesInfoURL, false);
#else
if (Debug.isDebugBuild)
{
#endif
Sampleton.Log($" -> Application.OpenURL(\"{kEnhancedSpatialServicesInfoURL}\")");
Application.OpenURL(kEnhancedSpatialServicesInfoURL);
}
return "\nEnhanced Spatial Services is disabled on your device. Enable it in OS Settings > Privacy & Safety > Device Permissions";
case OVRPlugin.Result.Failure_SpaceGroupNotFound:
return "\n(this is expected if anchors have not been shared to this group UUID yet)";
case OVRPlugin.Result.Failure_ColocationSessionNetworkFailed:
case OVRPlugin.Result.Failure_SpaceNetworkTimeout:
case OVRPlugin.Result.Failure_SpaceNetworkRequestFailed:
if (Application.internetReachability == NetworkReachability.NotReachable)
return "\n(device lacks internet connection)";
else
return "\n(device has internet)";
}
return string.Empty;
}
//
// impl. details
static readonly StringBuilder s_StrBuf = new();
} // end static class SampleExtensions