ZEDPluginInspector.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Collections;
  5. /// <summary>
  6. /// Checks your system for the required ZED SDK version, and displays an error window with instructions if it's missing.
  7. /// Runs automatically when Unity loads. Remove the [InitializeOnLoad] tag to disable this.
  8. /// </summary>
  9. [InitializeOnLoad]
  10. public class ZEDPluginInspector : EditorWindow
  11. {
  12. /// <summary>
  13. /// ZED unity logo
  14. /// </summary>
  15. static Texture2D image = null;
  16. private static EditorWindow window;
  17. private static bool showErrorMode = false;
  18. private static bool showSettingsMode = false;
  19. private static string errorMessage = "";
  20. const bool forceSettingsShow = false;
  21. bool showErrorPlugin = false;
  22. const string ignore = "ignore.";
  23. const string useRecommended = "Use recommended ({0})";
  24. const string currentValue = " (current = {0})";
  25. const string buildTarget = "Build Target";
  26. const string showUnitySplashScreen = "Show Unity Splashscreen";
  27. const string displayResolutionDialog = "Display Resolution Dialog";
  28. const string resizableWindow = "Resizable Window";
  29. const string colorSpace = "Color Space";
  30. const string gpuSkinning = "GPU Skinning";
  31. const string MSAAValue = "Anti Aliasing";
  32. const string runInBackground = "Run In Background";
  33. const string visibleInBackground = "Visible In Background";
  34. const BuildTarget needed_BuildTarget = BuildTarget.StandaloneWindows64;
  35. const bool needed_ShowUnitySplashScreen = false;
  36. const bool needed_ResizableWindow = true;
  37. const ColorSpace needed_ColorSpace = ColorSpace.Linear;
  38. const bool needed_GPUSkinning = true;
  39. const int needed_MSAAValue = 4;
  40. const bool needed_RunInBackground = true;
  41. const bool needed_VisibleInBackground = true;
  42. static ZEDPluginInspector()
  43. {
  44. EditorApplication.update += OnInit;
  45. }
  46. static void OnInit()
  47. {
  48. EditorApplication.update -= OnInit;
  49. if (!EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isCompiling && !EditorApplication.isUpdating)
  50. {
  51. if (!EditorPrefs.HasKey("ZED_NoWarning_Plugin"))
  52. EditorApplication.update += UpdateWnd;
  53. }
  54. else
  55. EditorApplication.update += UpdateLog;
  56. EditorApplication.update += UpdateSettingsWnd;
  57. }
  58. void OnEnable()
  59. {
  60. addMissingTag();
  61. }
  62. /// <summary>
  63. /// Makes sure the project's tags are loaded, as they are used in some samples but may get deleted on import or
  64. /// if shared via source control.
  65. /// </summary>
  66. static public void addMissingTag()
  67. {
  68. // Open tag manager
  69. SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
  70. SerializedProperty tagsProp = tagManager.FindProperty("tags");
  71. // Adding a Tag
  72. string s = "HelpObject";
  73. // Check if not here already
  74. bool found = false;
  75. for (int i = 0; i < tagsProp.arraySize; i++)
  76. {
  77. SerializedProperty t = tagsProp.GetArrayElementAtIndex(i);
  78. if (t.stringValue.Equals(s)) { found = true; break; }
  79. }
  80. //If not found, add it since we use it in GreenScreen.
  81. //This tag may be used anywhere, since it tags helper object that may have a specific behavior
  82. if (!found)
  83. {
  84. tagsProp.InsertArrayElementAtIndex(0);
  85. SerializedProperty n = tagsProp.GetArrayElementAtIndex(0);
  86. n.stringValue = s;
  87. }
  88. // and to save the changes
  89. tagManager.ApplyModifiedProperties();
  90. }
  91. static void UpdateLog()
  92. {
  93. if (!sl.ZEDCamera.CheckPlugin())
  94. {
  95. Debug.Log("ZED SDK is not installed or needs to be updated");
  96. }
  97. EditorApplication.update -= UpdateLog;
  98. }
  99. static void UpdateSettingsWnd()
  100. {
  101. showSettingsMode =
  102. (!EditorPrefs.HasKey(ignore + buildTarget) &&
  103. EditorUserBuildSettings.activeBuildTarget != needed_BuildTarget) ||
  104. (!EditorPrefs.HasKey(ignore + showUnitySplashScreen) &&
  105. PlayerSettings.SplashScreen.show != needed_ShowUnitySplashScreen) ||
  106. (!EditorPrefs.HasKey(ignore + resizableWindow) &&
  107. PlayerSettings.resizableWindow != needed_ResizableWindow) ||
  108. (!EditorPrefs.HasKey(ignore + colorSpace) &&
  109. PlayerSettings.colorSpace != needed_ColorSpace) ||
  110. (!EditorPrefs.HasKey(ignore + gpuSkinning) &&
  111. PlayerSettings.gpuSkinning != needed_GPUSkinning) ||
  112. (!EditorPrefs.HasKey(ignore + runInBackground) &&
  113. PlayerSettings.runInBackground != needed_RunInBackground) ||
  114. (!EditorPrefs.HasKey(ignore + visibleInBackground) &&
  115. PlayerSettings.visibleInBackground != needed_VisibleInBackground) ||
  116. (!EditorPrefs.HasKey(ignore + MSAAValue) &&
  117. QualitySettings.antiAliasing != needed_MSAAValue) ||
  118. forceSettingsShow;
  119. if (showSettingsMode)
  120. {
  121. window = GetWindow<ZEDPluginInspector>(true);
  122. window.maxSize = new Vector2(400, 400);
  123. window.minSize = window.maxSize;
  124. window.Show(true);
  125. }
  126. EditorApplication.update -= UpdateSettingsWnd;
  127. }
  128. static void UpdateWnd()
  129. {
  130. if (!sl.ZEDCamera.CheckPlugin())
  131. {
  132. errorMessage = ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_DEPENDENCIES_ISSUE);
  133. showErrorMode = true;
  134. window = GetWindow<ZEDPluginInspector>(true);
  135. window.maxSize = new Vector2(400, 600);
  136. window.minSize = window.maxSize;
  137. window.Show(true);
  138. }
  139. EditorApplication.update -= UpdateWnd;
  140. }
  141. void OnGUI()
  142. {
  143. if (showErrorMode)
  144. {
  145. showErrorWindow();
  146. }
  147. else if (showSettingsMode)
  148. {
  149. showSettingsWindow();
  150. }
  151. }
  152. /// <summary>
  153. /// Displays a popup window when the correct ZED SDK version isn't installed.
  154. /// </summary>
  155. public void showErrorWindow()
  156. {
  157. if (image == null)
  158. {
  159. image = Resources.Load("Textures/logo", typeof(Texture2D)) as Texture2D;
  160. }
  161. var rect = GUILayoutUtility.GetRect(position.width, 150, GUI.skin.box);
  162. if (image)
  163. {
  164. GUI.DrawTexture(rect, image, ScaleMode.ScaleToFit);
  165. }
  166. GUIStyle myStyle = new GUIStyle(GUI.skin.label);
  167. myStyle.normal.textColor = Color.red;
  168. myStyle.fontStyle = FontStyle.Bold;
  169. GUILayout.Space(20);
  170. GUILayout.BeginHorizontal();
  171. GUILayout.FlexibleSpace();
  172. GUILayout.Label("ZED SDK is not installed or needs to be updated", myStyle);
  173. GUILayout.FlexibleSpace();
  174. GUILayout.EndHorizontal();
  175. myStyle = new GUIStyle(GUI.skin.box);
  176. myStyle.normal.textColor = Color.red;
  177. GUI.Box(new Rect(0, position.height / 2, position.width, 100), errorMessage, myStyle);
  178. GUILayout.FlexibleSpace();
  179. GUILayout.BeginHorizontal();
  180. myStyle.normal.textColor = Color.black;
  181. myStyle.fontStyle = FontStyle.Bold;
  182. GUILayout.Label("Do not ask me again...");
  183. showErrorPlugin = EditorGUILayout.Toggle("", showErrorPlugin);
  184. GUILayout.EndHorizontal();
  185. GUILayout.Space(10);
  186. if (GUILayout.Button("Close"))
  187. {
  188. if (showErrorPlugin)
  189. {
  190. EditorPrefs.SetBool("ZED_NoWarning_Plugin", true);
  191. }
  192. this.Close();
  193. }
  194. }
  195. Vector2 scrollPosition;
  196. /// <summary>
  197. /// Shows a window prompting the user to change project settings to recommended settings, with
  198. /// buttons to automatically do so.
  199. /// </summary>
  200. public void showSettingsWindow()
  201. {
  202. if (image == null)
  203. {
  204. image = Resources.Load("Textures/logo", typeof(Texture2D)) as Texture2D;
  205. }
  206. var rect = GUILayoutUtility.GetRect(position.width, 150, GUI.skin.box);
  207. if (image)
  208. {
  209. GUI.DrawTexture(rect, image, ScaleMode.ScaleToFit);
  210. }
  211. EditorGUILayout.HelpBox("Recommended project settings for ZED Unity Plugin", MessageType.Warning);
  212. scrollPosition = GUILayout.BeginScrollView(scrollPosition);
  213. int numItems = 0;
  214. if (!EditorPrefs.HasKey(ignore + buildTarget) &&
  215. EditorUserBuildSettings.activeBuildTarget != needed_BuildTarget)
  216. {
  217. ++numItems;
  218. GUILayout.Label(buildTarget + string.Format(currentValue, EditorUserBuildSettings.activeBuildTarget));
  219. GUILayout.BeginHorizontal();
  220. if (GUILayout.Button(string.Format(useRecommended, needed_BuildTarget)))
  221. {
  222. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Standalone, needed_BuildTarget);
  223. }
  224. GUILayout.FlexibleSpace();
  225. if (GUILayout.Button("Ignore"))
  226. {
  227. EditorPrefs.SetBool(ignore + buildTarget, true);
  228. }
  229. GUILayout.EndHorizontal();
  230. }
  231. if (!EditorPrefs.HasKey(ignore + showUnitySplashScreen) &&
  232. PlayerSettings.SplashScreen.show != needed_ShowUnitySplashScreen)
  233. {
  234. ++numItems;
  235. GUILayout.Label(showUnitySplashScreen + string.Format(currentValue, PlayerSettings.SplashScreen.show));
  236. GUILayout.BeginHorizontal();
  237. if (GUILayout.Button(string.Format(useRecommended, needed_ShowUnitySplashScreen)))
  238. {
  239. PlayerSettings.SplashScreen.show = needed_ShowUnitySplashScreen;
  240. }
  241. GUILayout.FlexibleSpace();
  242. if (GUILayout.Button("Ignore"))
  243. {
  244. EditorPrefs.SetBool(ignore + showUnitySplashScreen, true);
  245. }
  246. GUILayout.EndHorizontal();
  247. }
  248. if (!EditorPrefs.HasKey(ignore + resizableWindow) &&
  249. PlayerSettings.resizableWindow != needed_ResizableWindow)
  250. {
  251. ++numItems;
  252. GUILayout.Label(resizableWindow + string.Format(currentValue, PlayerSettings.resizableWindow));
  253. GUILayout.BeginHorizontal();
  254. if (GUILayout.Button(string.Format(useRecommended, needed_ResizableWindow)))
  255. {
  256. PlayerSettings.resizableWindow = needed_ResizableWindow;
  257. }
  258. GUILayout.FlexibleSpace();
  259. if (GUILayout.Button("Ignore"))
  260. {
  261. EditorPrefs.SetBool(ignore + resizableWindow, true);
  262. }
  263. GUILayout.EndHorizontal();
  264. }
  265. if (!EditorPrefs.HasKey(ignore + visibleInBackground) &&
  266. PlayerSettings.visibleInBackground != needed_VisibleInBackground)
  267. {
  268. ++numItems;
  269. GUILayout.Label(visibleInBackground + string.Format(currentValue, PlayerSettings.visibleInBackground));
  270. GUILayout.BeginHorizontal();
  271. if (GUILayout.Button(string.Format(useRecommended, needed_VisibleInBackground)))
  272. {
  273. PlayerSettings.visibleInBackground = needed_VisibleInBackground;
  274. }
  275. GUILayout.FlexibleSpace();
  276. if (GUILayout.Button("Ignore"))
  277. {
  278. EditorPrefs.SetBool(ignore + visibleInBackground, true);
  279. }
  280. GUILayout.EndHorizontal();
  281. }
  282. if (!EditorPrefs.HasKey(ignore + runInBackground) &&
  283. PlayerSettings.runInBackground != needed_RunInBackground)
  284. {
  285. ++numItems;
  286. GUILayout.Label(runInBackground + string.Format(currentValue, PlayerSettings.runInBackground));
  287. GUILayout.BeginHorizontal();
  288. if (GUILayout.Button(string.Format(useRecommended, needed_RunInBackground)))
  289. {
  290. PlayerSettings.runInBackground = needed_RunInBackground;
  291. }
  292. GUILayout.FlexibleSpace();
  293. if (GUILayout.Button("Ignore"))
  294. {
  295. EditorPrefs.SetBool(ignore + runInBackground, true);
  296. }
  297. GUILayout.EndHorizontal();
  298. }
  299. if (!EditorPrefs.HasKey(ignore + gpuSkinning) &&
  300. PlayerSettings.gpuSkinning != needed_GPUSkinning)
  301. {
  302. ++numItems;
  303. GUILayout.Label(gpuSkinning + string.Format(currentValue, PlayerSettings.gpuSkinning));
  304. GUILayout.BeginHorizontal();
  305. if (GUILayout.Button(string.Format(useRecommended, needed_GPUSkinning)))
  306. {
  307. PlayerSettings.gpuSkinning = needed_GPUSkinning;
  308. }
  309. GUILayout.FlexibleSpace();
  310. if (GUILayout.Button("Ignore"))
  311. {
  312. EditorPrefs.SetBool(ignore + gpuSkinning, true);
  313. }
  314. GUILayout.EndHorizontal();
  315. }
  316. if (!EditorPrefs.HasKey(ignore + colorSpace) &&
  317. PlayerSettings.colorSpace != needed_ColorSpace)
  318. {
  319. ++numItems;
  320. GUILayout.Label(colorSpace + string.Format(currentValue, PlayerSettings.colorSpace));
  321. GUILayout.BeginHorizontal();
  322. if (GUILayout.Button(string.Format(useRecommended, needed_ColorSpace) + " - requires reloading scene"))
  323. {
  324. PlayerSettings.colorSpace = needed_ColorSpace;
  325. }
  326. GUILayout.FlexibleSpace();
  327. if (GUILayout.Button("Ignore"))
  328. {
  329. EditorPrefs.SetBool(ignore + colorSpace, true);
  330. }
  331. GUILayout.EndHorizontal();
  332. }
  333. if (!EditorPrefs.HasKey(ignore + MSAAValue) &&
  334. QualitySettings.antiAliasing != needed_MSAAValue)
  335. {
  336. ++numItems;
  337. GUILayout.Label(MSAAValue + string.Format(currentValue, QualitySettings.antiAliasing) + "x Multi Sampling");
  338. GUILayout.BeginHorizontal();
  339. if (GUILayout.Button(string.Format(useRecommended, needed_MSAAValue)))
  340. {
  341. QualitySettings.antiAliasing = needed_MSAAValue;
  342. }
  343. GUILayout.FlexibleSpace();
  344. if (GUILayout.Button("Ignore"))
  345. {
  346. EditorPrefs.SetBool(ignore + MSAAValue, true);
  347. }
  348. GUILayout.EndHorizontal();
  349. }
  350. GUILayout.BeginHorizontal();
  351. GUILayout.FlexibleSpace();
  352. if (GUILayout.Button("Clear All Ignores"))
  353. {
  354. EditorPrefs.DeleteKey(ignore + buildTarget);
  355. EditorPrefs.DeleteKey(ignore + showUnitySplashScreen);
  356. EditorPrefs.DeleteKey(ignore + displayResolutionDialog);
  357. EditorPrefs.DeleteKey(ignore + resizableWindow);
  358. EditorPrefs.DeleteKey(ignore + colorSpace);
  359. EditorPrefs.DeleteKey(ignore + gpuSkinning);
  360. EditorPrefs.DeleteKey(ignore + MSAAValue);
  361. EditorPrefs.DeleteKey(ignore + visibleInBackground);
  362. EditorPrefs.DeleteKey(ignore + runInBackground);
  363. EditorPrefs.DeleteKey(ignore + MSAAValue);
  364. }
  365. GUILayout.EndHorizontal();
  366. GUILayout.EndScrollView();
  367. GUILayout.FlexibleSpace();
  368. GUILayout.BeginHorizontal();
  369. if (numItems > 0)
  370. {
  371. if (GUILayout.Button("Accept All"))
  372. {
  373. // Only set those that have not been explicitly ignored.
  374. if (!EditorPrefs.HasKey(ignore + buildTarget))
  375. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Standalone, needed_BuildTarget);
  376. if (!EditorPrefs.HasKey(ignore + showUnitySplashScreen))
  377. PlayerSettings.SplashScreen.show = needed_ShowUnitySplashScreen;
  378. if (!EditorPrefs.HasKey(ignore + resizableWindow))
  379. PlayerSettings.resizableWindow = needed_ResizableWindow;
  380. if (!EditorPrefs.HasKey(ignore + colorSpace))
  381. PlayerSettings.colorSpace = needed_ColorSpace;
  382. if (!EditorPrefs.HasKey(ignore + gpuSkinning))
  383. PlayerSettings.gpuSkinning = needed_GPUSkinning;
  384. if (!EditorPrefs.HasKey(ignore + runInBackground))
  385. PlayerSettings.runInBackground = needed_RunInBackground;
  386. if (!EditorPrefs.HasKey(ignore + visibleInBackground))
  387. PlayerSettings.visibleInBackground = needed_VisibleInBackground;
  388. if (!EditorPrefs.HasKey(ignore + MSAAValue))
  389. QualitySettings.antiAliasing = needed_MSAAValue;
  390. EditorUtility.DisplayDialog("Accept All", "Settings applied", "Ok");
  391. Close();
  392. }
  393. if (GUILayout.Button("Ignore All"))
  394. {
  395. if (EditorUtility.DisplayDialog("Ignore All", "Are you sure?", "Yes, Ignore All", "Cancel"))
  396. {
  397. // Only ignore those that do not currently match our recommended settings.
  398. if (EditorUserBuildSettings.activeBuildTarget != needed_BuildTarget)
  399. EditorPrefs.SetBool(ignore + buildTarget, true);
  400. if (PlayerSettings.SplashScreen.show != needed_ShowUnitySplashScreen)
  401. EditorPrefs.SetBool(ignore + showUnitySplashScreen, true);
  402. if (PlayerSettings.resizableWindow != needed_ResizableWindow)
  403. EditorPrefs.SetBool(ignore + resizableWindow, true);
  404. if (PlayerSettings.colorSpace != needed_ColorSpace)
  405. EditorPrefs.SetBool(ignore + colorSpace, true);
  406. if (PlayerSettings.gpuSkinning != needed_GPUSkinning)
  407. EditorPrefs.SetBool(ignore + gpuSkinning, true);
  408. if (PlayerSettings.runInBackground != needed_RunInBackground)
  409. EditorPrefs.SetBool(ignore + runInBackground, true);
  410. if (PlayerSettings.visibleInBackground != needed_VisibleInBackground)
  411. EditorPrefs.SetBool(ignore + visibleInBackground, true);
  412. if (QualitySettings.antiAliasing != needed_MSAAValue)
  413. EditorPrefs.SetBool(ignore + MSAAValue, true);
  414. Close();
  415. }
  416. }
  417. }
  418. else if (GUILayout.Button("Close"))
  419. {
  420. Close();
  421. }
  422. GUILayout.EndHorizontal();
  423. }
  424. }