UXREnvFix.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. using System.Net.Http.Headers;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. using UnityEngine.XR.Management;
  7. using UnityEditor.XR.Management;
  8. namespace Rokid.UXR.Editor
  9. {
  10. [InitializeOnLoad]
  11. public class UXREnvFix : EditorWindow
  12. {
  13. #region Config
  14. private const string ignorePrefix = "UXREnvFix";
  15. private static FixItem[] fixItems;
  16. private static UXREnvFix window;
  17. private Vector2 scrollPosition;
  18. private static string minUnityVersion = "2020.3.26";
  19. private static AndroidSdkVersions minSdkVersion = AndroidSdkVersions.AndroidApiLevel26;
  20. private static AndroidArchitecture targetArchitecture = AndroidArchitecture.ARM64;
  21. #endregion
  22. static UXREnvFix()
  23. {
  24. EditorApplication.update -= OnUpdate;
  25. EditorApplication.update += OnUpdate;
  26. }
  27. private static void OnUpdate()
  28. {
  29. bool show = false;
  30. if (fixItems == null) { RegisterItems(); }
  31. foreach (var item in fixItems)
  32. {
  33. if (!item.IsIgnored() && !item.IsValid() && item.Level > MessageType.Warning && item.IsAutoPop())
  34. {
  35. show = true;
  36. }
  37. }
  38. if (show)
  39. {
  40. ShowWindow();
  41. }
  42. EditorApplication.update -= OnUpdate;
  43. }
  44. private static void RegisterItems()
  45. {
  46. fixItems = new FixItem[]
  47. {
  48. new CheckBuildTarget(MessageType.Error),
  49. new CheckUnityMinVersion(MessageType.Error),
  50. #if UNITY_2020_3_OR_NEWER
  51. new CheckCardboardPackage(MessageType.Error),//Should NOT add Cardboard Package into project
  52. #endif
  53. new CkeckMTRendering(MessageType.Error),
  54. new CkeckAndroidGraphicsAPI(MessageType.Error),
  55. new CkeckAndroidOrientation(MessageType.Warning),
  56. // new CkeckColorSpace(MessageType.Warning),
  57. new CheckOptimizedFramePacing(MessageType.Warning),
  58. new CheckMinmumAPILevel(MessageType.Error),
  59. new CheckArchitecture(MessageType.Error)
  60. };
  61. }
  62. [MenuItem("UXR/Env/Project Environment Fix", false)]
  63. public static void ShowWindow()
  64. {
  65. RegisterItems();
  66. window = GetWindow<UXREnvFix>(true);
  67. window.minSize = new Vector2(320, 300);
  68. window.maxSize = new Vector2(320, 800);
  69. window.titleContent = new GUIContent("UXR SDK | Environment Fix");
  70. }
  71. //[MenuItem("UXR/Env/Delete Ignore", false)]
  72. public static void DeleteIgonre()
  73. {
  74. foreach (var item in fixItems)
  75. {
  76. item.DeleteIgonre();
  77. }
  78. }
  79. public void OnGUI()
  80. {
  81. string resourcePath = GetResourcePath();
  82. Texture2D logo = AssetDatabase.LoadAssetAtPath<Texture2D>(resourcePath + "RokidIcon.png");
  83. Rect rect = GUILayoutUtility.GetRect(position.width, 80, GUI.skin.box);
  84. GUI.DrawTexture(rect, logo, ScaleMode.ScaleToFit);
  85. string aboutText = "This window provides tips to help fix common issues with UXR SDK and your project.";
  86. EditorGUILayout.LabelField(aboutText, EditorStyles.textArea);
  87. int ignoredCount = 0;
  88. int fixableCount = 0;
  89. int invalidNotIgnored = 0;
  90. for (int i = 0; i < fixItems.Length; i++)
  91. {
  92. FixItem item = fixItems[i];
  93. bool ignored = item.IsIgnored();
  94. bool valid = item.IsValid();
  95. bool fixable = item.IsFixable();
  96. if (!valid && !ignored && fixable)
  97. {
  98. fixableCount++;
  99. }
  100. if (!valid && !ignored)
  101. {
  102. invalidNotIgnored++;
  103. }
  104. if (ignored)
  105. {
  106. ignoredCount++;
  107. }
  108. }
  109. Rect issuesRect = EditorGUILayout.GetControlRect();
  110. GUI.Box(new Rect(issuesRect.x - 4, issuesRect.y, issuesRect.width + 8, issuesRect.height), "Tips", EditorStyles.toolbarButton);
  111. if (invalidNotIgnored > 0)
  112. {
  113. scrollPosition = GUILayout.BeginScrollView(scrollPosition);
  114. {
  115. for (int i = 0; i < fixItems.Length; i++)
  116. {
  117. FixItem item = fixItems[i];
  118. if (!item.IsIgnored() && !item.IsValid())
  119. {
  120. invalidNotIgnored++;
  121. GUILayout.BeginVertical("box");
  122. {
  123. item.DrawGUI();
  124. EditorGUILayout.BeginHorizontal();
  125. {
  126. // Aligns buttons to the right
  127. GUILayout.FlexibleSpace();
  128. if (item.IsFixable())
  129. {
  130. if (GUILayout.Button("Fix"))
  131. item.Fix();
  132. }
  133. //if (GUILayout.Button("Ignore"))
  134. // check.Ignore();
  135. }
  136. EditorGUILayout.EndHorizontal();
  137. }
  138. GUILayout.EndVertical();
  139. }
  140. }
  141. }
  142. GUILayout.EndScrollView();
  143. }
  144. GUILayout.FlexibleSpace();
  145. if (invalidNotIgnored == 0)
  146. {
  147. GUILayout.BeginHorizontal();
  148. {
  149. GUILayout.FlexibleSpace();
  150. GUILayout.BeginVertical();
  151. {
  152. GUILayout.Label("No issue found");
  153. if (GUILayout.Button("Close Window"))
  154. Close();
  155. }
  156. GUILayout.EndVertical();
  157. GUILayout.FlexibleSpace();
  158. }
  159. GUILayout.EndHorizontal();
  160. GUILayout.FlexibleSpace();
  161. }
  162. EditorGUILayout.BeginHorizontal("box");
  163. {
  164. if (fixableCount > 0)
  165. {
  166. if (GUILayout.Button("Accept All"))
  167. {
  168. if (EditorUtility.DisplayDialog("Accept All", "Are you sure?", "Yes, Accept All", "Cancel"))
  169. {
  170. for (int i = 0; i < fixItems.Length; i++)
  171. {
  172. FixItem item = fixItems[i];
  173. if (!item.IsIgnored() &&
  174. !item.IsValid())
  175. {
  176. if (item.IsFixable())
  177. item.Fix();
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. GUILayout.EndHorizontal();
  185. }
  186. private string GetResourcePath()
  187. {
  188. var ms = MonoScript.FromScriptableObject(this);
  189. var path = AssetDatabase.GetAssetPath(ms);
  190. path = Path.GetDirectoryName(path);
  191. return path + "\\Textures\\";
  192. }
  193. private abstract class FixItem
  194. {
  195. protected string key;
  196. protected MessageType level;
  197. public MessageType Level
  198. {
  199. get
  200. {
  201. return level;
  202. }
  203. }
  204. public FixItem(MessageType level)
  205. {
  206. this.level = level;
  207. }
  208. public void Ignore()
  209. {
  210. EditorPrefs.SetBool(ignorePrefix + key, true);
  211. }
  212. public bool IsIgnored()
  213. {
  214. return EditorPrefs.HasKey(ignorePrefix + key);
  215. }
  216. public void DeleteIgonre()
  217. {
  218. Debug.Log("DeleteIgnore" + ignorePrefix + key);
  219. EditorPrefs.DeleteKey(ignorePrefix + key);
  220. }
  221. public abstract bool IsValid();
  222. public abstract bool IsAutoPop();
  223. public abstract void DrawGUI();
  224. public abstract bool IsFixable();
  225. public abstract void Fix();
  226. protected void DrawContent(string title, string msg)
  227. {
  228. EditorGUILayout.HelpBox(title, level);
  229. EditorGUILayout.LabelField(msg, EditorStyles.textArea);
  230. }
  231. }
  232. private class CheckCardboardPackage : FixItem
  233. {
  234. private enum PackageStates
  235. {
  236. None,
  237. WaitingForList,
  238. Failed,
  239. Added,
  240. }
  241. UnityEditor.PackageManager.Requests.ListRequest request;
  242. PackageStates packageState = PackageStates.None;
  243. bool isvalid = true;
  244. bool initOnStart;
  245. bool hasLoader;
  246. public CheckCardboardPackage(MessageType level) : base(level)
  247. {
  248. key = this.GetType().Name;
  249. request = null;
  250. isvalid = true;
  251. #if UNITY_2020_3_OR_NEWER
  252. EditorApplication.update -= CheckPackageUpdate;
  253. EditorApplication.update += CheckPackageUpdate;
  254. #endif
  255. }
  256. #if UNITY_2020_3_OR_NEWER
  257. void CheckPackageUpdate()
  258. {
  259. switch (packageState)
  260. {
  261. case PackageStates.None:
  262. request = UnityEditor.PackageManager.Client.List(true, false);
  263. packageState = PackageStates.WaitingForList;
  264. break;
  265. case PackageStates.WaitingForList:
  266. if (request.IsCompleted)
  267. {
  268. if (request.Error != null || request.Status == UnityEditor.PackageManager.StatusCode.Failure)
  269. {
  270. packageState = PackageStates.Failed;
  271. break;
  272. }
  273. UnityEditor.PackageManager.PackageCollection col = request.Result;
  274. foreach (var info in col)
  275. {
  276. if (info.name == "com.google.xr.cardboard")
  277. {
  278. packageState = PackageStates.Added;
  279. isvalid = false;
  280. break;
  281. }
  282. }
  283. if (packageState != PackageStates.Added) isvalid = true;
  284. }
  285. break;
  286. default:
  287. break;
  288. }
  289. }
  290. #endif
  291. public override bool IsValid()
  292. {
  293. return isvalid;//!(isvalid && initOnStart && hasLoader);
  294. }
  295. public override void DrawGUI()
  296. {
  297. string message = @"You must remove Cardboard XR Plugin " + @"
  298. In dropdown list of Window> Package Manager >Select Cardboard XR Plugin> Remove";
  299. DrawContent("Please Remove Cardboard XR Plugin", message);
  300. }
  301. public override bool IsFixable()
  302. {
  303. if (isvalid)
  304. {
  305. XRGeneralSettings sets = XRGeneralSettingsPerBuildTarget.XRGeneralSettingsForBuildTarget(BuildTargetGroup.Android);
  306. if (sets != null)
  307. {
  308. initOnStart = sets.InitManagerOnStart;
  309. XRManagerSettings plugins = sets.AssignedSettings;
  310. var loaders = sets.Manager.activeLoaders;
  311. hasLoader = false;
  312. for (int i = 0; i < loaders.Count; i++)
  313. {
  314. if (loaders[i].GetType() == typeof(Rokid.XR.Core.XRLoader))
  315. hasLoader = true;
  316. break;
  317. }
  318. if (!hasLoader || !initOnStart) return true;
  319. }
  320. }
  321. return false;
  322. }
  323. public override void Fix()
  324. {
  325. var sets = XRGeneralSettingsPerBuildTarget.XRGeneralSettingsForBuildTarget(BuildTargetGroup.Android);
  326. bool initOnStart = sets.InitManagerOnStart;
  327. if (!initOnStart) sets.InitManagerOnStart = true;
  328. XRManagerSettings plugins = sets.AssignedSettings;
  329. var loaders = sets.Manager.activeLoaders;
  330. bool hasLoader = false;
  331. for (int i = 0; i < loaders.Count; i++)
  332. {
  333. if (loaders[i].GetType() == typeof(Rokid.XR.Core.XRLoader))
  334. hasLoader = true;
  335. break;
  336. }
  337. if (!hasLoader)
  338. {
  339. var xrLoader = new Rokid.XR.Core.XRLoader();
  340. plugins.TryAddLoader(xrLoader);
  341. }
  342. }
  343. public override bool IsAutoPop()
  344. {
  345. return false;
  346. }
  347. }
  348. private class CkeckAndroidGraphicsAPI : FixItem
  349. {
  350. public CkeckAndroidGraphicsAPI(MessageType level) : base(level)
  351. {
  352. key = this.GetType().Name;
  353. }
  354. public override bool IsValid()
  355. {
  356. if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
  357. {
  358. if (PlayerSettings.GetUseDefaultGraphicsAPIs(BuildTarget.Android))
  359. {
  360. return false;
  361. }
  362. var graphics = PlayerSettings.GetGraphicsAPIs(BuildTarget.Android);
  363. if (graphics != null && graphics.Length >= 1 &&
  364. graphics[0] == UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3)
  365. {
  366. return true;
  367. }
  368. return false;
  369. }
  370. else
  371. {
  372. return false;
  373. }
  374. }
  375. public override void DrawGUI()
  376. {
  377. string message = @"Graphics APIs should be set to OpenGLES. Player Settings > Other Settings > Graphics APIs , choose 'OpenGLES3'.";
  378. DrawContent("Graphics APIs is not OpenGLES", message);
  379. }
  380. public override bool IsFixable()
  381. {
  382. return true;
  383. }
  384. public override void Fix()
  385. {
  386. if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
  387. {
  388. PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.Android, false);
  389. PlayerSettings.SetGraphicsAPIs(BuildTarget.Android, new GraphicsDeviceType[1] { GraphicsDeviceType.OpenGLES3 });
  390. }
  391. }
  392. public override bool IsAutoPop()
  393. {
  394. return true;
  395. }
  396. }
  397. private class CkeckMTRendering : FixItem
  398. {
  399. public CkeckMTRendering(MessageType level) : base(level)
  400. {
  401. key = this.GetType().Name;
  402. }
  403. public override bool IsValid()
  404. {
  405. if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
  406. {
  407. return !PlayerSettings.GetMobileMTRendering(BuildTargetGroup.Android);
  408. }
  409. else
  410. {
  411. return false;
  412. }
  413. }
  414. public override void DrawGUI()
  415. {
  416. string message = @"In order to run correct on mobile devices, the RenderingThreadingMode should be set.
  417. in dropdown list of Player Settings > Other Settings > Multithreaded Rendering, close toggle.";
  418. DrawContent("Multithreaded Rendering not close", message);
  419. }
  420. public override bool IsFixable()
  421. {
  422. return true;
  423. }
  424. public override void Fix()
  425. {
  426. if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
  427. {
  428. PlayerSettings.SetMobileMTRendering(BuildTargetGroup.Android, false);
  429. }
  430. }
  431. public override bool IsAutoPop()
  432. {
  433. return true;
  434. }
  435. }
  436. private class CkeckAndroidOrientation : FixItem
  437. {
  438. public CkeckAndroidOrientation(MessageType level) : base(level)
  439. {
  440. key = this.GetType().Name;
  441. }
  442. public override bool IsValid()
  443. {
  444. return PlayerSettings.defaultInterfaceOrientation == UIOrientation.Portrait;
  445. }
  446. public override void DrawGUI()
  447. {
  448. string message = @"In order to display correct on mobile devices, the orientation should be set to portrait.
  449. in dropdown list of Player Settings > Resolution and Presentation > Default Orientation, choose 'Portrait'.";
  450. DrawContent("Orientation is not portrait", message);
  451. }
  452. public override bool IsFixable()
  453. {
  454. return true;
  455. }
  456. public override void Fix()
  457. {
  458. if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
  459. {
  460. PlayerSettings.defaultInterfaceOrientation = UIOrientation.Portrait;
  461. }
  462. }
  463. public override bool IsAutoPop()
  464. {
  465. return true;
  466. }
  467. }
  468. private class CkeckColorSpace : FixItem
  469. {
  470. public CkeckColorSpace(MessageType level) : base(level)
  471. {
  472. key = this.GetType().Name;
  473. }
  474. public override bool IsValid()
  475. {
  476. return PlayerSettings.colorSpace == ColorSpace.Gamma;
  477. }
  478. public override void DrawGUI()
  479. {
  480. string message = @"In order to display correct on mobile devices, the colorSpace should be set to gamma.
  481. in dropdown list of Player Settings > Other Settings > Color Space, choose 'Gamma'.";
  482. DrawContent("ColorSpace is not Linear", message);
  483. }
  484. public override bool IsFixable()
  485. {
  486. return true;
  487. }
  488. public override void Fix()
  489. {
  490. if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
  491. {
  492. PlayerSettings.colorSpace = ColorSpace.Gamma;
  493. }
  494. }
  495. public override bool IsAutoPop()
  496. {
  497. return true;
  498. }
  499. }
  500. private class CkeckAndroidPermission : FixItem
  501. {
  502. public CkeckAndroidPermission(MessageType level) : base(level)
  503. {
  504. key = this.GetType().Name;
  505. }
  506. public override bool IsValid()
  507. {
  508. if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
  509. {
  510. return PlayerSettings.Android.forceInternetPermission;
  511. }
  512. else
  513. {
  514. return false;
  515. }
  516. }
  517. public override void DrawGUI()
  518. {
  519. string message = @"In order to run correct on mobile devices, the internet access premission should be set.
  520. in dropdown list of Player Settings > Other Settings > Internet Access, choose 'Require'.";
  521. DrawContent("internet access permission not available", message);
  522. }
  523. public override bool IsFixable()
  524. {
  525. return true;
  526. }
  527. public override void Fix()
  528. {
  529. if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
  530. {
  531. PlayerSettings.Android.forceInternetPermission = true;
  532. }
  533. }
  534. public override bool IsAutoPop()
  535. {
  536. return true;
  537. }
  538. }
  539. //todo...添加最低版本号的检查
  540. private class CheckUnityMinVersion : FixItem
  541. {
  542. string unityVersion;//
  543. public CheckUnityMinVersion(MessageType level) : base(level)
  544. {
  545. key = this.GetType().Name;
  546. unityVersion = Application.unityVersion;
  547. }
  548. public override void DrawGUI()
  549. {
  550. string message = @"The minimum Unity version required is 2020.3.36";
  551. DrawContent("Unity version not valid ", message);
  552. }
  553. public override void Fix()
  554. {
  555. }
  556. public override bool IsAutoPop()
  557. {
  558. return true;
  559. }
  560. public override bool IsFixable()
  561. {
  562. return unityVersion.CompareTo(minUnityVersion) == 1;
  563. }
  564. public override bool IsValid()
  565. {
  566. return unityVersion.CompareTo(minUnityVersion) == 1;
  567. }
  568. }
  569. private class CheckOptimizedFramePacing : FixItem
  570. {
  571. public CheckOptimizedFramePacing(MessageType level) : base(level)
  572. {
  573. key = this.GetType().Name;
  574. }
  575. public override void DrawGUI()
  576. {
  577. string message = @"The optimizedFramePacing need to unselect";
  578. DrawContent("OptimizedFramePacing", message);
  579. }
  580. public override void Fix()
  581. {
  582. PlayerSettings.Android.optimizedFramePacing = false;
  583. }
  584. public override bool IsAutoPop()
  585. {
  586. return true;
  587. }
  588. public override bool IsFixable()
  589. {
  590. return true;
  591. }
  592. public override bool IsValid()
  593. {
  594. return PlayerSettings.Android.optimizedFramePacing == false;
  595. }
  596. }
  597. private class CheckMinmumAPILevel : FixItem
  598. {
  599. public CheckMinmumAPILevel(MessageType level) : base(level)
  600. {
  601. key = this.GetType().Name;
  602. }
  603. public override void DrawGUI()
  604. {
  605. string message = @"The minSdkVersion needs to be " + minSdkVersion.ToString();
  606. DrawContent("MinSDKVersion", message);
  607. }
  608. public override void Fix()
  609. {
  610. PlayerSettings.Android.minSdkVersion = minSdkVersion;
  611. }
  612. public override bool IsAutoPop()
  613. {
  614. return true;
  615. }
  616. public override bool IsFixable()
  617. {
  618. return true;
  619. }
  620. public override bool IsValid()
  621. {
  622. return PlayerSettings.Android.minSdkVersion >= minSdkVersion;
  623. }
  624. }
  625. private class CheckArchitecture : FixItem
  626. {
  627. public CheckArchitecture(MessageType level) : base(level)
  628. {
  629. key = this.GetType().Name;
  630. }
  631. public override void DrawGUI()
  632. {
  633. string message = @"The Target Architecture should be " + targetArchitecture;
  634. DrawContent("Target Architecture", message);
  635. }
  636. public override void Fix()
  637. {
  638. PlayerSettings.SetScriptingBackend(BuildTargetGroup.Android, ScriptingImplementation.IL2CPP);
  639. PlayerSettings.Android.targetArchitectures = targetArchitecture;
  640. }
  641. public override bool IsAutoPop()
  642. {
  643. return true;
  644. }
  645. public override bool IsFixable()
  646. {
  647. return true;
  648. }
  649. public override bool IsValid()
  650. {
  651. return PlayerSettings.GetScriptingBackend(BuildTargetGroup.Android) == ScriptingImplementation.IL2CPP &&
  652. PlayerSettings.Android.targetArchitectures == targetArchitecture;
  653. }
  654. }
  655. private class CheckBuildTarget : FixItem
  656. {
  657. public CheckBuildTarget(MessageType level) : base(level)
  658. {
  659. key = this.GetType().Name;
  660. }
  661. public override void DrawGUI()
  662. {
  663. string message = @"The Build Target should be Android";
  664. DrawContent("Build Target", message);
  665. }
  666. public override void Fix()
  667. {
  668. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, BuildTarget.Android);
  669. }
  670. public override bool IsAutoPop()
  671. {
  672. return true;
  673. }
  674. public override bool IsFixable()
  675. {
  676. return true;
  677. }
  678. public override bool IsValid()
  679. {
  680. return EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android;
  681. }
  682. }
  683. }
  684. }