GUIMessage.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using UnityEngine.XR;
  4. /// <summary>
  5. /// Controls the message displayed as the zed is initialized, and if it becomes disconnected.
  6. /// Needs to be pre-attached to the ZED rig to work; not added in programmatically.
  7. /// </summary><remarks>
  8. /// There are separate text elements for the mono view and the stereo view to account for the
  9. /// difference in display resolutions. 'Mono' elements are displayed in a 'Screen Space - Overlay'
  10. /// canvas, and 'Stereo' elements in a 'Screen Space - Camera' canvas.
  11. /// </remarks>
  12. public class GUIMessage : MonoBehaviour
  13. {
  14. /// <summary>
  15. /// Text under the loading sign for the mono rig ("Loading...", "Camera is not detected", etc.)
  16. /// </summary>
  17. private UnityEngine.UI.Text textmono;
  18. /// <summary>
  19. /// Text under the loading sign for stereo rig's left eye ("Loading...", "Camera is not detected", etc.)
  20. /// </summary>
  21. private UnityEngine.UI.Text textleft;
  22. /// <summary>
  23. /// Text under the loading sign for stereo rig's right eye ("Loading...", "Camera is not detected", etc.)
  24. /// </summary>
  25. private UnityEngine.UI.Text textright;
  26. /// <summary>
  27. /// Flag set to true when the ZED is finished initializing.
  28. /// Starts a timer to wait for the ZED's textures to be loaded.
  29. /// </summary>
  30. private bool ready = false;
  31. /// <summary>
  32. /// Warning container for the mono rig. Contains the text, background, and loading graphic.
  33. /// </summary>
  34. private GameObject warningmono;
  35. /// <summary>
  36. /// Warning container for the stereo rig's left eye. Contains the text, background, and loading graphic.
  37. /// </summary>
  38. private GameObject warningleft;
  39. /// <summary>
  40. /// Warning container for the stereo rig's right eye. Contains the text, background, and loading graphic.
  41. /// </summary>
  42. private GameObject warningright;
  43. /// <summary>
  44. /// Timer used to add a 0.5 second delay between the ZED being initialized and the message disappearing.
  45. /// This is done to let the ZED textures to finish being made.
  46. /// </summary>
  47. private float timerWarning = 0.0f;
  48. /// <summary>
  49. /// If true, stops calling most of the logic in Update() which updates the canvas elements.
  50. /// Called once the ZED is ready and all elements have been properly disabled.
  51. /// </summary>
  52. private bool init = false;
  53. /// <summary>
  54. /// Timer used to delay clearing the text by 0.2 seconds once the camera is initialized.
  55. /// </summary>
  56. private float timer;
  57. /// <summary>
  58. /// Reference to the loading spinner animation for the mono rig.
  59. /// </summary>
  60. private GameObject imagemono;
  61. /// <summary>
  62. /// Reference to the loading spinner animation for the stereo rig's left eye.
  63. /// </summary>
  64. private GameObject imageleft;
  65. /// <summary>
  66. /// Reference to the loading spinner animation for the stereo rig's right eye.
  67. /// </summary>
  68. private GameObject imageright;
  69. /// <summary>
  70. /// Opening status given during the ZED's last attempt to initialize.
  71. /// Used to check if an error has gone on for more than one frame before changing text.
  72. /// </summary>
  73. private sl.ERROR_CODE oldInitStatus;
  74. /// <summary>
  75. /// The zed manager.
  76. /// </summary>
  77. private ZEDManager zedManager;
  78. /// <summary>
  79. /// Creates canvas(es) and canvas elements depending on whether the ZED rig is mono (ZED_Rig_Mono)
  80. /// or stereo (ZED_Rig_Stereo).
  81. /// </summary>
  82. private void Awake()
  83. {
  84. zedManager = GetComponent<ZEDManager>();
  85. oldInitStatus = sl.ERROR_CODE.ERROR_CODE_LAST;
  86. if (!zedManager.IsStereoRig) //Without VR, we use a Screen Space - Overlay canvas.
  87. {
  88. //Instantiate the mono warning prefab and set basic settings for it.
  89. warningmono = Instantiate(Resources.Load("PrefabsUI/Warning") as GameObject, transform);
  90. warningmono.SetActive(true);
  91. warningmono.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceCamera;
  92. //Set the target camera to whichever mono camera in the rig has the highest depth.
  93. Camera highestdepthzedcam = zedManager.GetLeftCamera();
  94. if (zedManager.GetRightCamera() != null && (highestdepthzedcam == null || zedManager.GetRightCamera().depth > highestdepthzedcam.depth))
  95. {
  96. highestdepthzedcam = zedManager.GetRightCamera();
  97. }
  98. warningmono.GetComponent<Canvas>().worldCamera = highestdepthzedcam;
  99. textmono = warningmono.GetComponentInChildren<UnityEngine.UI.Text>();
  100. textmono.color = Color.white;
  101. if (!sl.ZEDCamera.CheckPlugin())
  102. {
  103. textmono.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_NOT_INSTALLED);
  104. }
  105. imagemono = warningmono.transform.GetChild(0).GetChild(1).gameObject;
  106. imagemono.transform.parent.gameObject.SetActive(true);
  107. ready = false;
  108. }
  109. else //In VR, we use two Screen Space - Camera canvases, one for each eye.
  110. {
  111. //Instantiate the left warning prefab and set basic settings for it.
  112. warningleft = Instantiate(Resources.Load("PrefabsUI/Warning_VR") as GameObject, zedManager.GetLeftCameraTransform());
  113. warningleft.SetActive(true);
  114. warningleft.GetComponent<Canvas>().worldCamera = zedManager.GetLeftCamera();
  115. warningleft.GetComponent<Canvas>().planeDistance = 1;
  116. textleft = warningleft.GetComponentInChildren<UnityEngine.UI.Text>();
  117. textleft.color = Color.white;
  118. imageleft = warningleft.transform.GetChild(0).GetChild(1).gameObject;
  119. imageleft.transform.parent.gameObject.SetActive(true);
  120. //Instantiate the right warning prefab and set basic settings for it.
  121. warningright = Instantiate(Resources.Load("PrefabsUI/Warning_VR") as GameObject, zedManager.GetRightCameraTransform());
  122. warningright.SetActive(true);
  123. warningright.GetComponent<Canvas>().worldCamera = zedManager.GetRightCamera();
  124. warningright.GetComponent<Canvas>().planeDistance = 1;
  125. textright = warningright.GetComponentInChildren<UnityEngine.UI.Text>();
  126. textright.color = Color.white;
  127. imageright = warningright.transform.GetChild(0).GetChild(1).gameObject;
  128. imageright.transform.parent.gameObject.SetActive(true);
  129. if (!sl.ZEDCamera.CheckPlugin()) //Warn the use there's no SDK installed.
  130. {
  131. textleft.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_NOT_INSTALLED);
  132. textright.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_NOT_INSTALLED);
  133. }
  134. ready = false;
  135. }
  136. }
  137. /// <summary>
  138. /// Subscribe to OnZedReady and OnZEDDisconnected events.
  139. /// </summary>
  140. private void OnEnable()
  141. {
  142. zedManager.OnZEDReady += Ready;
  143. zedManager.OnZEDDisconnected += ZEDDisconnected;
  144. }
  145. /// <summary>
  146. /// Unsubscribe from OnZedReady and OnZEDDisconnected events.
  147. /// </summary>
  148. private void OnDisable()
  149. {
  150. zedManager.OnZEDReady -= Ready;
  151. zedManager.OnZEDDisconnected -= ZEDDisconnected;
  152. }
  153. /// <summary>
  154. /// Re-enable canvas elements and change message when ZED is disconnected.
  155. /// GameObjects were disabled before because the ZED had to have finished initializing before.
  156. /// </summary>
  157. void ZEDDisconnected()
  158. {
  159. if (warningmono) //Using the mono rig.
  160. {
  161. warningmono.SetActive(true);
  162. imagemono.SetActive(true);
  163. warningmono.transform.GetChild(0).gameObject.SetActive(true);
  164. textmono.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.ZED_IS_DISCONNECETD);
  165. warningmono.layer = 30;
  166. ready = false;
  167. }
  168. if (warningleft) //Using the stereo rig.
  169. {
  170. warningleft.SetActive(true);
  171. imageleft.SetActive(true);
  172. warningleft.transform.GetChild(0).gameObject.SetActive(true);
  173. textleft.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.ZED_IS_DISCONNECETD);
  174. warningleft.layer = 30;
  175. warningright.SetActive(true);
  176. imageright.SetActive(true);
  177. warningright.transform.GetChild(0).gameObject.SetActive(true);
  178. textright.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.ZED_IS_DISCONNECETD);
  179. warningright.layer = 30;
  180. ready = false;
  181. }
  182. }
  183. /// <summary>
  184. /// If visible, print the loading status of the ZED, including relevant errors.
  185. /// </summary>
  186. void Update()
  187. {
  188. if (!init) //This check will pass until 0.5 seconds after the ZED is done initializing.
  189. {
  190. sl.ERROR_CODE e = zedManager.LastInitStatus;
  191. if (e == sl.ERROR_CODE.SUCCESS) //Last initialization attempt was successful.
  192. {
  193. if (!ready)
  194. {
  195. if (textmono)
  196. {
  197. textmono.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_MODULE_LOADING);
  198. }
  199. else if (textleft)
  200. {
  201. textleft.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_MODULE_LOADING);
  202. textright.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_MODULE_LOADING);
  203. }
  204. }
  205. else
  206. {
  207. timer += Time.deltaTime; //Clear text after a short delay.
  208. if (timer > 0.2f)
  209. {
  210. if (textmono)
  211. {
  212. textmono.text = "";
  213. }
  214. else if (textleft)
  215. {
  216. textleft.text = "";
  217. textright.text = "";
  218. }
  219. }
  220. if (imagemono)
  221. { //Disable mono rig canvas.
  222. imagemono.gameObject.SetActive(false);
  223. }
  224. else if (imageleft)
  225. { //Disable stereo rig canvases.
  226. imageleft.gameObject.SetActive(false);
  227. imageright.gameObject.SetActive(false);
  228. }
  229. }
  230. }
  231. else if (e == sl.ERROR_CODE.ERROR_CODE_LAST) //"Loading..."
  232. {
  233. //Initial error code set before an initialization attempt has returned successful or failed.
  234. //In short, it means it's still loading.
  235. if (textmono)
  236. {
  237. textmono.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.CAMERA_LOADING);
  238. }
  239. else if (textleft)
  240. {
  241. textleft.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.CAMERA_LOADING);
  242. textright.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.CAMERA_LOADING);
  243. }
  244. }
  245. else if (e == sl.ERROR_CODE.CAMERA_NOT_DETECTED && oldInitStatus == sl.ERROR_CODE.CAMERA_NOT_DETECTED) //"Camera not detected"
  246. {
  247. if (textmono)
  248. {
  249. textmono.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.UNABLE_TO_OPEN_CAMERA);
  250. }
  251. else if (textleft)
  252. {
  253. textleft.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.UNABLE_TO_OPEN_CAMERA);
  254. textright.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.UNABLE_TO_OPEN_CAMERA);
  255. }
  256. }
  257. else if (e == sl.ERROR_CODE.CAMERA_DETECTION_ISSUE && oldInitStatus == sl.ERROR_CODE.CAMERA_DETECTION_ISSUE) //"Unable to open camera"
  258. {
  259. if (textmono)
  260. {
  261. textmono.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.CAMERA_DETECTION_ISSUE);
  262. }
  263. else if (textleft)
  264. {
  265. textleft.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.CAMERA_DETECTION_ISSUE);
  266. textright.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.CAMERA_DETECTION_ISSUE);
  267. }
  268. }
  269. else if (e == sl.ERROR_CODE.SENSOR_NOT_DETECTED && oldInitStatus == sl.ERROR_CODE.SENSOR_NOT_DETECTED) //"Camera motion sensor not detected"
  270. {
  271. if (textmono)
  272. {
  273. textmono.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SENSOR_NOT_DETECTED);
  274. }
  275. else if (textleft)
  276. {
  277. textleft.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SENSOR_NOT_DETECTED);
  278. textright.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SENSOR_NOT_DETECTED);
  279. }
  280. }
  281. else if (e == sl.ERROR_CODE.LOW_USB_BANDWIDTH && oldInitStatus == sl.ERROR_CODE.LOW_USB_BANDWIDTH)//"Low USB bandwidth"
  282. {
  283. if (textmono)
  284. {
  285. textmono.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.LOW_USB_BANDWIDTH);
  286. }
  287. else if (textleft)
  288. {
  289. textleft.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.LOW_USB_BANDWIDTH);
  290. textright.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.LOW_USB_BANDWIDTH);
  291. }
  292. }
  293. else if (e == sl.ERROR_CODE.INVALID_SVO_FILE && oldInitStatus == sl.ERROR_CODE.INVALID_SVO_FILE)
  294. {
  295. if (textmono)
  296. {
  297. textmono.text = "Invalid SVO File/Path";
  298. }
  299. else if (textleft)
  300. {
  301. textleft.text = "Invalid SVO File/Path";
  302. textright.text = "Invalid SVO File/Path";
  303. }
  304. }
  305. else if (e == sl.ERROR_CODE.INVALID_CALIBRATION_FILE && oldInitStatus == sl.ERROR_CODE.INVALID_CALIBRATION_FILE)
  306. {
  307. if (textmono)
  308. {
  309. textmono.text = "Invalid Calibration file";
  310. }
  311. else if (textleft)
  312. {
  313. textleft.text = "Invalid Calibration file";
  314. textright.text = "Invalid Calibration file";
  315. }
  316. }
  317. else if (e == oldInitStatus)
  318. {
  319. if (textmono)
  320. {
  321. textmono.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.CAMERA_NOT_INITIALIZED);
  322. }
  323. else if (textleft)
  324. {
  325. textleft.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.CAMERA_NOT_INITIALIZED);
  326. textright.text = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.CAMERA_NOT_INITIALIZED);
  327. }
  328. }
  329. oldInitStatus = e;
  330. }
  331. if (ready) //ZED has finished initializing. Set a timer, then disable texts after it expires.
  332. {
  333. timerWarning += Time.deltaTime;
  334. if (timerWarning > 0.5f)
  335. {
  336. if (warningmono)
  337. {
  338. warningmono.SetActive(false);
  339. }
  340. else if (warningleft)
  341. {
  342. warningleft.SetActive(false);
  343. warningright.SetActive(false);
  344. }
  345. }
  346. init = true; //Prevents logic above the if (ready) check from running each frame.
  347. if (imagemono)
  348. {
  349. imagemono.gameObject.transform.parent.gameObject.SetActive(false);
  350. }
  351. else if (imageleft)
  352. {
  353. imageleft.gameObject.transform.parent.gameObject.SetActive(false);
  354. imageright.gameObject.transform.parent.gameObject.SetActive(false);
  355. }
  356. }
  357. }
  358. /// <summary>
  359. /// Set a flag to run timer and disable text. Called by ZEDManager.OnZEDReady when ZED finishes initializing.
  360. /// </summary>
  361. private void Ready()
  362. {
  363. ready = true;
  364. }
  365. }