AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
DefaultInitializationErrorHandler.cs
Go to the documentation of this file.
1 /*==============================================================================
2 Copyright (c) 2017 PTC Inc. All Rights Reserved.
3 
4 Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
5 All Rights Reserved.
6 Confidential and Proprietary - Protected under copyright and other laws.
7 ==============================================================================*/
8 
9 using UnityEngine;
10 using Vuforia;
11 
15 public class DefaultInitializationErrorHandler : MonoBehaviour
16 {
17  #region Vuforia_lifecycle_events
18 
19  public void OnVuforiaInitializationError(VuforiaUnity.InitError initError)
20  {
21  if (initError != VuforiaUnity.InitError.INIT_SUCCESS)
22  {
23  SetErrorCode(initError);
24  SetErrorOccurred(true);
25  }
26  }
27 
28  #endregion // Vuforia_lifecycle_events
29 
30  #region PRIVATE_MEMBER_VARIABLES
31 
32  string mErrorText = "";
33  bool mErrorOccurred;
34 
35  const string headerLabel = "Vuforia Initialization Error";
36 
37  GUIStyle bodyStyle;
38  GUIStyle headerStyle;
39  GUIStyle footerStyle;
40 
41  Texture2D bodyTexture;
42  Texture2D headerTexture;
43  Texture2D footerTexture;
44 
45  #endregion // PRIVATE_MEMBER_VARIABLES
46 
47  #region UNTIY_MONOBEHAVIOUR_METHODS
48 
49  void Awake()
50  {
51  // Check for an initialization error on start.
52  VuforiaRuntime.Instance.RegisterVuforiaInitErrorCallback(OnVuforiaInitializationError);
53  }
54 
55  void Start()
56  {
57  SetupGUIStyles();
58  }
59 
60  void OnGUI()
61  {
62  // On error, create a full screen window.
63  if (mErrorOccurred)
64  GUI.Window(0, new Rect(0, 0, Screen.width, Screen.height), DrawWindowContent, "");
65  }
66 
70  void OnDestroy()
71  {
72  VuforiaRuntime.Instance.UnregisterVuforiaInitErrorCallback(OnVuforiaInitializationError);
73  }
74 
75  #endregion // UNTIY_MONOBEHAVIOUR_METHODS
76 
77  #region PRIVATE_METHODS
78 
79  void DrawWindowContent(int id)
80  {
81  var headerRect = new Rect(0, 0, Screen.width, Screen.height / 8);
82  var bodyRect = new Rect(0, Screen.height / 8, Screen.width, Screen.height / 8 * 6);
83  var footerRect = new Rect(0, Screen.height - Screen.height / 8, Screen.width, Screen.height / 8);
84 
85  GUI.Label(headerRect, headerLabel, headerStyle);
86  GUI.Label(bodyRect, mErrorText, bodyStyle);
87 
88  if (GUI.Button(footerRect, "Close", footerStyle))
89  {
90 #if UNITY_EDITOR
91  UnityEditor.EditorApplication.isPlaying = false;
92  #else
93  Application.Quit();
94 #endif
95  }
96  }
97 
98  void SetErrorCode(VuforiaUnity.InitError errorCode)
99  {
100  switch (errorCode)
101  {
102  case VuforiaUnity.InitError.INIT_EXTERNAL_DEVICE_NOT_DETECTED:
103  mErrorText =
104  "Failed to initialize Vuforia because this " +
105  "device is not docked with required external hardware.";
106  break;
107  case VuforiaUnity.InitError.INIT_LICENSE_ERROR_MISSING_KEY:
108  mErrorText =
109  "Vuforia App key is missing. Please get a valid key " +
110  "by logging into your account at developer.vuforia.com " +
111  "and creating a new project.";
112  break;
113  case VuforiaUnity.InitError.INIT_LICENSE_ERROR_INVALID_KEY:
114  mErrorText =
115  "Vuforia App key is invalid. " +
116  "Please get a valid key by logging into your account at " +
117  "developer.vuforia.com and creating a new project. \n\n" +
118  getKeyInfo();
119  break;
120  case VuforiaUnity.InitError.INIT_LICENSE_ERROR_NO_NETWORK_TRANSIENT:
121  mErrorText = "Unable to contact server. Please try again later.";
122  break;
123  case VuforiaUnity.InitError.INIT_LICENSE_ERROR_NO_NETWORK_PERMANENT:
124  mErrorText = "No network available. Please make sure you are connected to the Internet.";
125  break;
126  case VuforiaUnity.InitError.INIT_LICENSE_ERROR_CANCELED_KEY:
127  mErrorText =
128  "This App license key has been cancelled and may no longer be used. " +
129  "Please get a new license key. \n\n" +
130  getKeyInfo();
131  break;
132  case VuforiaUnity.InitError.INIT_LICENSE_ERROR_PRODUCT_TYPE_MISMATCH:
133  mErrorText =
134  "Vuforia App key is not valid for this product. Please get a valid key " +
135  "by logging into your account at developer.vuforia.com and choosing the " +
136  "right product type during project creation. \n\n" +
137  getKeyInfo() + " \n\n" +
138  "Note that Universal Windows Platform (UWP) apps require " +
139  "a license key created on or after August 9th, 2016.";
140  break;
141  case VuforiaUnity.InitError.INIT_NO_CAMERA_ACCESS:
142  mErrorText =
143  "User denied Camera access to this app.\n" +
144  "To restore, enable Camera access in Settings:\n" +
145  "Settings > Privacy > Camera > " + Application.productName + "\n" +
146  "Also verify that the Camera is enabled in:\n" +
147  "Settings > General > Restrictions.";
148  break;
149  case VuforiaUnity.InitError.INIT_DEVICE_NOT_SUPPORTED:
150  mErrorText = "Failed to initialize Vuforia because this device is not supported.";
151  break;
152  case VuforiaUnity.InitError.INIT_ERROR:
153  mErrorText = "Failed to initialize Vuforia.";
154  break;
155  }
156 
157  // Prepend the error code in red
158  mErrorText = "<color=red>" + errorCode.ToString().Replace("_", " ") + "</color>\n\n" + mErrorText;
159 
160  // Remove rich text tags for console logging
161  var errorTextConsole = mErrorText.Replace("<color=red>", "").Replace("</color>", "");
162 
163  Debug.LogError("Vuforia initialization failed: " + errorCode + "\n\n" + errorTextConsole);
164  }
165 
166  void SetErrorOccurred(bool errorOccurred)
167  {
168  mErrorOccurred = errorOccurred;
169  }
170 
171  string getKeyInfo()
172  {
173  string key = VuforiaConfiguration.Instance.Vuforia.LicenseKey;
174  string keyInfo;
175  if (key.Length > 10)
176  keyInfo =
177  "Your current key is <color=red>" + key.Length + "</color> characters in length. " +
178  "It begins with <color=red>" + key.Substring(0, 5) + "</color> " +
179  "and ends with <color=red>" + key.Substring(key.Length - 5, 5) + "</color>.";
180  else
181  keyInfo =
182  "Your current key is <color=red>" + key.Length + "</color> characters in length. \n" +
183  "The key is: <color=red>" + key + "</color>.";
184  return keyInfo;
185  }
186 
187  void SetupGUIStyles()
188  {
189  // Called from Start() to determine physical size of device for text sizing
190  var shortSidePixels = Screen.width < Screen.height ? Screen.width : Screen.height;
191  var shortSideInches = shortSidePixels / Screen.dpi;
192  var physicalSizeMultiplier = shortSideInches > 4.0f ? 2 : 1;
193 
194  // Create 1x1 pixel background textures for body, header, and footer
195  bodyTexture = CreateSinglePixelTexture(Color.white);
196  headerTexture = CreateSinglePixelTexture(new Color(
197  Mathf.InverseLerp(0, 255, 220),
198  Mathf.InverseLerp(0, 255, 220),
199  Mathf.InverseLerp(0, 255, 220))); // RGB(220)
200  footerTexture = CreateSinglePixelTexture(new Color(
201  Mathf.InverseLerp(0, 255, 35),
202  Mathf.InverseLerp(0, 255, 178),
203  Mathf.InverseLerp(0, 255, 0))); // RGB(35,178,0)
204 
205  // Create body style and set values
206  bodyStyle = new GUIStyle();
207  bodyStyle.normal.background = bodyTexture;
208  bodyStyle.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
209  bodyStyle.fontSize = (int) (18 * physicalSizeMultiplier * Screen.dpi / 160);
210  bodyStyle.normal.textColor = Color.black;
211  bodyStyle.wordWrap = true;
212  bodyStyle.alignment = TextAnchor.MiddleCenter;
213  bodyStyle.padding = new RectOffset(40, 40, 0, 0);
214 
215  // Duplicate body style and change necessary values
216  headerStyle = new GUIStyle(bodyStyle);
217  headerStyle.normal.background = headerTexture;
218  headerStyle.fontSize = (int) (24 * physicalSizeMultiplier * Screen.dpi / 160);
219 
220  // Duplicate body style and change necessary values
221  footerStyle = new GUIStyle(bodyStyle);
222  footerStyle.normal.background = footerTexture;
223  footerStyle.normal.textColor = Color.white;
224  footerStyle.fontSize = (int) (28 * physicalSizeMultiplier * Screen.dpi / 160);
225  }
226 
227  Texture2D CreateSinglePixelTexture(Color color)
228  {
229  // Called by SetupGUIStyles() to create 1x1 texture
230  var texture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
231  texture.SetPixel(0, 0, color);
232  texture.Apply();
233  return texture;
234  }
235 
236  #endregion // PRIVATE_METHODS
237 }
void OnVuforiaInitializationError(VuforiaUnity.InitError initError)
A custom handler that registers for Vuforia initialization errors