DefaultInitializationErrorHandler.cs
9.38 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
/*==============================================================================
Copyright (c) 2017 PTC Inc. All Rights Reserved.
Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
Confidential and Proprietary - Protected under copyright and other laws.
==============================================================================*/
using UnityEngine;
using Vuforia;
/// <summary>
/// A custom handler that registers for Vuforia initialization errors
///
/// Changes made to this file could be overwritten when upgrading the Vuforia version.
/// When implementing custom error handler behavior, consider inheriting from this class instead.
/// </summary>
public class DefaultInitializationErrorHandler : VuforiaMonoBehaviour
{
#region Vuforia_lifecycle_events
public void OnVuforiaInitializationError(VuforiaUnity.InitError initError)
{
if (initError != VuforiaUnity.InitError.INIT_SUCCESS)
{
SetErrorCode(initError);
SetErrorOccurred(true);
}
}
#endregion // Vuforia_lifecycle_events
#region PRIVATE_MEMBER_VARIABLES
string mErrorText = "";
bool mErrorOccurred;
const string headerLabel = "Vuforia Engine Initialization Error";
GUIStyle bodyStyle;
GUIStyle headerStyle;
GUIStyle footerStyle;
Texture2D bodyTexture;
Texture2D headerTexture;
Texture2D footerTexture;
#endregion // PRIVATE_MEMBER_VARIABLES
#region UNTIY_MONOBEHAVIOUR_METHODS
void Awake()
{
// Check for an initialization error on start.
VuforiaRuntime.Instance.RegisterVuforiaInitErrorCallback(OnVuforiaInitializationError);
}
void Start()
{
SetupGUIStyles();
}
void OnGUI()
{
// On error, create a full screen window.
if (mErrorOccurred)
GUI.Window(0, new Rect(0, 0, Screen.width, Screen.height), DrawWindowContent, "");
}
/// <summary>
/// When this game object is destroyed, it unregisters itself as event handler
/// </summary>
void OnDestroy()
{
VuforiaRuntime.Instance.UnregisterVuforiaInitErrorCallback(OnVuforiaInitializationError);
}
#endregion // UNTIY_MONOBEHAVIOUR_METHODS
#region PRIVATE_METHODS
void DrawWindowContent(int id)
{
var headerRect = new Rect(0, 0, Screen.width, Screen.height / 8);
var bodyRect = new Rect(0, Screen.height / 8, Screen.width, Screen.height / 8 * 6);
var footerRect = new Rect(0, Screen.height - Screen.height / 8, Screen.width, Screen.height / 8);
GUI.Label(headerRect, headerLabel, headerStyle);
GUI.Label(bodyRect, mErrorText, bodyStyle);
if (GUI.Button(footerRect, "Close", footerStyle))
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
void SetErrorCode(VuforiaUnity.InitError errorCode)
{
switch (errorCode)
{
case VuforiaUnity.InitError.INIT_EXTERNAL_DEVICE_NOT_DETECTED:
mErrorText =
"Failed to initialize the Vuforia Engine because this " +
"device is not docked with required external hardware.";
break;
case VuforiaUnity.InitError.INIT_LICENSE_ERROR_MISSING_KEY:
mErrorText =
"Vuforia Engine App key is missing. Please get a valid key " +
"by logging into your account at developer.vuforia.com " +
"and creating a new project.";
break;
case VuforiaUnity.InitError.INIT_LICENSE_ERROR_INVALID_KEY:
mErrorText =
"Vuforia Engine App key is invalid. " +
"Please get a valid key by logging into your account at " +
"developer.vuforia.com and creating a new project. \n\n" +
getKeyInfo();
break;
case VuforiaUnity.InitError.INIT_LICENSE_ERROR_NO_NETWORK_TRANSIENT:
mErrorText = "Unable to contact server. Please try again later.";
break;
case VuforiaUnity.InitError.INIT_LICENSE_ERROR_NO_NETWORK_PERMANENT:
mErrorText = "No network available. Please make sure you are connected to the Internet.";
break;
case VuforiaUnity.InitError.INIT_LICENSE_ERROR_CANCELED_KEY:
mErrorText =
"This App license key has been cancelled and may no longer be used. " +
"Please get a new license key. \n\n" +
getKeyInfo();
break;
case VuforiaUnity.InitError.INIT_LICENSE_ERROR_PRODUCT_TYPE_MISMATCH:
mErrorText =
"Vuforia Engine App key is not valid for this product. Please get a valid key " +
"by logging into your account at developer.vuforia.com and choosing the " +
"right product type during project creation. \n\n" +
getKeyInfo() + " \n\n" +
"Note that Universal Windows Platform (UWP) apps require " +
"a license key created on or after August 9th, 2016.";
break;
case VuforiaUnity.InitError.INIT_NO_CAMERA_ACCESS:
mErrorText =
"User denied Camera access to this app.\n" +
"To restore, enable Camera access in Settings:\n" +
"Settings > Privacy > Camera > " + Application.productName + "\n" +
"Also verify that the Camera is enabled in:\n" +
"Settings > General > Restrictions.";
break;
case VuforiaUnity.InitError.INIT_DEVICE_NOT_SUPPORTED:
mErrorText = "Failed to initialize Vuforia Engine because this device is not supported.";
break;
case VuforiaUnity.InitError.INIT_ERROR:
mErrorText = "Failed to initialize Vuforia Engine.";
break;
}
// Prepend the error code in red
mErrorText = "<color=red>" + errorCode.ToString().Replace("_", " ") + "</color>\n\n" + mErrorText;
// Remove rich text tags for console logging
var errorTextConsole = mErrorText.Replace("<color=red>", "").Replace("</color>", "");
Debug.LogError("Vuforia Engine initialization failed: " + errorCode + "\n\n" + errorTextConsole);
}
void SetErrorOccurred(bool errorOccurred)
{
mErrorOccurred = errorOccurred;
}
string getKeyInfo()
{
string key = VuforiaConfiguration.Instance.Vuforia.LicenseKey;
string keyInfo;
if (key.Length > 10)
keyInfo =
"Your current key is <color=red>" + key.Length + "</color> characters in length. " +
"It begins with <color=red>" + key.Substring(0, 5) + "</color> " +
"and ends with <color=red>" + key.Substring(key.Length - 5, 5) + "</color>.";
else
keyInfo =
"Your current key is <color=red>" + key.Length + "</color> characters in length. \n" +
"The key is: <color=red>" + key + "</color>.";
return keyInfo;
}
void SetupGUIStyles()
{
// Called from Start() to determine physical size of device for text sizing
var shortSidePixels = Screen.width < Screen.height ? Screen.width : Screen.height;
var shortSideInches = shortSidePixels / Screen.dpi;
var physicalSizeMultiplier = shortSideInches > 4.0f ? 2 : 1;
// Create 1x1 pixel background textures for body, header, and footer
bodyTexture = CreateSinglePixelTexture(Color.white);
headerTexture = CreateSinglePixelTexture(new Color(
Mathf.InverseLerp(0, 255, 220),
Mathf.InverseLerp(0, 255, 220),
Mathf.InverseLerp(0, 255, 220))); // RGB(220)
footerTexture = CreateSinglePixelTexture(new Color(
Mathf.InverseLerp(0, 255, 35),
Mathf.InverseLerp(0, 255, 178),
Mathf.InverseLerp(0, 255, 0))); // RGB(35,178,0)
// Create body style and set values
bodyStyle = new GUIStyle();
bodyStyle.normal.background = bodyTexture;
bodyStyle.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
bodyStyle.fontSize = (int) (18 * physicalSizeMultiplier * Screen.dpi / 160);
bodyStyle.normal.textColor = Color.black;
bodyStyle.wordWrap = true;
bodyStyle.alignment = TextAnchor.MiddleCenter;
bodyStyle.padding = new RectOffset(40, 40, 0, 0);
// Duplicate body style and change necessary values
headerStyle = new GUIStyle(bodyStyle);
headerStyle.normal.background = headerTexture;
headerStyle.fontSize = (int) (24 * physicalSizeMultiplier * Screen.dpi / 160);
// Duplicate body style and change necessary values
footerStyle = new GUIStyle(bodyStyle);
footerStyle.normal.background = footerTexture;
footerStyle.normal.textColor = Color.white;
footerStyle.fontSize = (int) (28 * physicalSizeMultiplier * Screen.dpi / 160);
}
Texture2D CreateSinglePixelTexture(Color color)
{
// Called by SetupGUIStyles() to create 1x1 texture
var texture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
texture.SetPixel(0, 0, color);
texture.Apply();
return texture;
}
#endregion // PRIVATE_METHODS
}