123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819 |
- using System.Net.Http.Headers;
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.Rendering;
- using UnityEngine.XR.Management;
- using UnityEditor.XR.Management;
- /*
- namespace Rokid.UXR.Editor
- {
- [InitializeOnLoad]
- public class UXREnvFix : EditorWindow
- {
- #region Config
- private const string ignorePrefix = "UXREnvFix";
- private static FixItem[] fixItems;
- private static UXREnvFix window;
- private Vector2 scrollPosition;
- private static string cardboardVersion = "1.21.4";
- private static string minUnityVersion = "2020.3.26";
- private static AndroidSdkVersions minSdkVersion = AndroidSdkVersions.AndroidApiLevel26;
- private static AndroidArchitecture targetArchitecture = AndroidArchitecture.ARM64;
- #endregion
- static UXREnvFix()
- {
- EditorApplication.update -= OnUpdate;
- EditorApplication.update += OnUpdate;
- }
- private static void OnUpdate()
- {
- bool show = false;
- if (fixItems == null) { RegisterItems(); }
- foreach (var item in fixItems)
- {
- if (!item.IsIgnored() && !item.IsValid() && item.Level > MessageType.Warning && item.IsAutoPop())
- {
- show = true;
- }
- }
- if (show)
- {
- ShowWindow();
- }
- EditorApplication.update -= OnUpdate;
- }
- private static void RegisterItems()
- {
- fixItems = new FixItem[]
- {
- new CheckBuildTarget(MessageType.Error),
- new CheckUnityMinVersion(MessageType.Error),
- // #if UNITY_2020_3_OR_NEWER
- // new CheckCardboardPackage(MessageType.Error),
- // #endif
- new CkeckMTRendering(MessageType.Error),
- new CkeckAndroidGraphicsAPI(MessageType.Error),
- new CkeckAndroidOrientation(MessageType.Warning),
- // new CkeckColorSpace(MessageType.Warning),
- new CheckOptimizedFramePacing(MessageType.Warning),
- new CheckMinmumAPILevel(MessageType.Error),
- new CheckArchitecture(MessageType.Error)
- };
- }
- [MenuItem("UXR/Env/Project Environment Fix", false)]
- public static void ShowWindow()
- {
- RegisterItems();
- window = GetWindow<UXREnvFix>(true);
- window.minSize = new Vector2(320, 300);
- window.maxSize = new Vector2(320, 800);
- window.titleContent = new GUIContent("UXR SDK | Environment Fix");
- }
- //[MenuItem("UXR/Env/Delete Ignore", false)]
- public static void DeleteIgonre()
- {
- foreach (var item in fixItems)
- {
- item.DeleteIgonre();
- }
- }
- public void OnGUI()
- {
- string resourcePath = GetResourcePath();
- Texture2D logo = AssetDatabase.LoadAssetAtPath<Texture2D>(resourcePath + "RokidIcon.png");
- Rect rect = GUILayoutUtility.GetRect(position.width, 80, GUI.skin.box);
- GUI.DrawTexture(rect, logo, ScaleMode.ScaleToFit);
- string aboutText = "This window provides tips to help fix common issues with UXR SDK and your project.";
- EditorGUILayout.LabelField(aboutText, EditorStyles.textArea);
- int ignoredCount = 0;
- int fixableCount = 0;
- int invalidNotIgnored = 0;
- for (int i = 0; i < fixItems.Length; i++)
- {
- FixItem item = fixItems[i];
- bool ignored = item.IsIgnored();
- bool valid = item.IsValid();
- bool fixable = item.IsFixable();
- if (!valid && !ignored && fixable)
- {
- fixableCount++;
- }
- if (!valid && !ignored)
- {
- invalidNotIgnored++;
- }
- if (ignored)
- {
- ignoredCount++;
- }
- }
- Rect issuesRect = EditorGUILayout.GetControlRect();
- GUI.Box(new Rect(issuesRect.x - 4, issuesRect.y, issuesRect.width + 8, issuesRect.height), "Tips", EditorStyles.toolbarButton);
- if (invalidNotIgnored > 0)
- {
- scrollPosition = GUILayout.BeginScrollView(scrollPosition);
- {
- for (int i = 0; i < fixItems.Length; i++)
- {
- FixItem item = fixItems[i];
- if (!item.IsIgnored() && !item.IsValid())
- {
- invalidNotIgnored++;
- GUILayout.BeginVertical("box");
- {
- item.DrawGUI();
- EditorGUILayout.BeginHorizontal();
- {
- // Aligns buttons to the right
- GUILayout.FlexibleSpace();
- if (item.IsFixable())
- {
- if (GUILayout.Button("Fix"))
- item.Fix();
- }
- //if (GUILayout.Button("Ignore"))
- // check.Ignore();
- }
- EditorGUILayout.EndHorizontal();
- }
- GUILayout.EndVertical();
- }
- }
- }
- GUILayout.EndScrollView();
- }
- GUILayout.FlexibleSpace();
- if (invalidNotIgnored == 0)
- {
- GUILayout.BeginHorizontal();
- {
- GUILayout.FlexibleSpace();
- GUILayout.BeginVertical();
- {
- GUILayout.Label("No issue found");
- if (GUILayout.Button("Close Window"))
- Close();
- }
- GUILayout.EndVertical();
- GUILayout.FlexibleSpace();
- }
- GUILayout.EndHorizontal();
- GUILayout.FlexibleSpace();
- }
- EditorGUILayout.BeginHorizontal("box");
- {
- if (fixableCount > 0)
- {
- if (GUILayout.Button("Accept All"))
- {
- if (EditorUtility.DisplayDialog("Accept All", "Are you sure?", "Yes, Accept All", "Cancel"))
- {
- for (int i = 0; i < fixItems.Length; i++)
- {
- FixItem item = fixItems[i];
- if (!item.IsIgnored() &&
- !item.IsValid())
- {
- if (item.IsFixable())
- item.Fix();
- }
- }
- }
- }
- }
- }
- GUILayout.EndHorizontal();
- }
- private string GetResourcePath()
- {
- var ms = MonoScript.FromScriptableObject(this);
- var path = AssetDatabase.GetAssetPath(ms);
- path = Path.GetDirectoryName(path);
- return path + "\\Textures\\";
- }
- private abstract class FixItem
- {
- protected string key;
- protected MessageType level;
- public MessageType Level
- {
- get
- {
- return level;
- }
- }
- public FixItem(MessageType level)
- {
- this.level = level;
- }
- public void Ignore()
- {
- EditorPrefs.SetBool(ignorePrefix + key, true);
- }
- public bool IsIgnored()
- {
- return EditorPrefs.HasKey(ignorePrefix + key);
- }
- public void DeleteIgonre()
- {
- RKLog.Info("DeleteIgnore" + ignorePrefix + key);
- EditorPrefs.DeleteKey(ignorePrefix + key);
- }
- public abstract bool IsValid();
- public abstract bool IsAutoPop();
- public abstract void DrawGUI();
- public abstract bool IsFixable();
- public abstract void Fix();
- protected void DrawContent(string title, string msg)
- {
- EditorGUILayout.HelpBox(title, level);
- EditorGUILayout.LabelField(msg, EditorStyles.textArea);
- }
- }
- private class CheckCardboardPackage : FixItem
- {
- private enum PackageStates
- {
- None,
- WaitingForList,
- Failed,
- Added,
- }
- UnityEditor.PackageManager.Requests.ListRequest request;
- PackageStates packageState = PackageStates.None;
- bool isvalid = true;
- bool initOnStart;
- bool hasLoader;
- public CheckCardboardPackage(MessageType level) : base(level)
- {
- key = this.GetType().Name;
- request = null;
- isvalid = true;
- #if UNITY_2020_3_OR_NEWER
- EditorApplication.update -= CheckPackageUpdate;
- EditorApplication.update += CheckPackageUpdate;
- #endif
- }
- #if UNITY_2020_3_OR_NEWER
- void CheckPackageUpdate()
- {
- switch (packageState)
- {
- case PackageStates.None:
- request = UnityEditor.PackageManager.Client.List(true, false);
- packageState = PackageStates.WaitingForList;
- break;
- case PackageStates.WaitingForList:
- if (request.IsCompleted)
- {
- if (request.Error != null || request.Status == UnityEditor.PackageManager.StatusCode.Failure)
- {
- packageState = PackageStates.Failed;
- break;
- }
- UnityEditor.PackageManager.PackageCollection col = request.Result;
- foreach (var info in col)
- {
- if (info.name == "com.google.xr.cardboard" && info.version == cardboardVersion)
- {
- packageState = PackageStates.Added;
- isvalid = true;
- break;
- }
- }
- if (packageState != PackageStates.Added) isvalid = false;
- }
- break;
- default:
- break;
- }
- }
- #endif
- public override bool IsValid()
- {
- return (isvalid && initOnStart && hasLoader);
- }
- public override void DrawGUI()
- {
- string message = @"You must upgrade Cardboard XR Plugin to version:" + cardboardVersion + @",
- in dropdown list of Window> Package Manager >'+' Button >Add package from disk >Select the package.json file in the root directory of the local Cardboard XR Plugin .
- Select XR Plug-in Management in the Project Settings window>Initialize XR on Startup checked>Cardboard XR Plugin Checked";
- DrawContent("Cardboard XR Plugin is not setup correctly", message);
- }
- public override bool IsFixable()
- {
- if (isvalid)
- {
- XRGeneralSettings sets = XRGeneralSettingsPerBuildTarget.XRGeneralSettingsForBuildTarget(BuildTargetGroup.Android);
- if (sets != null)
- {
- initOnStart = sets.InitManagerOnStart;
- XRManagerSettings plugins = sets.AssignedSettings;
- var loaders = sets.Manager.activeLoaders;
- hasLoader = false;
- for (int i = 0; i < loaders.Count; i++)
- {
- if (loaders[i].GetType() == typeof(Rokid.XR.Core.XRLoader))
- hasLoader = true;
- break;
- }
- if (!hasLoader || !initOnStart) return true;
- }
- }
- return false;
- }
- public override void Fix()
- {
- var sets = XRGeneralSettingsPerBuildTarget.XRGeneralSettingsForBuildTarget(BuildTargetGroup.Android);
- bool initOnStart = sets.InitManagerOnStart;
- if (!initOnStart) sets.InitManagerOnStart = true;
- XRManagerSettings plugins = sets.AssignedSettings;
- var loaders = sets.Manager.activeLoaders;
- bool hasLoader = false;
- for (int i = 0; i < loaders.Count; i++)
- {
- if (loaders[i].GetType() == typeof(Rokid.XR.Core.XRLoader))
- hasLoader = true;
- break;
- }
- if (!hasLoader)
- {
- var xrLoader = new Rokid.XR.Core.XRLoader();
- plugins.TryAddLoader(xrLoader);
- }
- }
- public override bool IsAutoPop()
- {
- return false;
- }
- }
- private class CkeckAndroidGraphicsAPI : FixItem
- {
- public CkeckAndroidGraphicsAPI(MessageType level) : base(level)
- {
- key = this.GetType().Name;
- }
- public override bool IsValid()
- {
- if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
- {
- if (PlayerSettings.GetUseDefaultGraphicsAPIs(BuildTarget.Android))
- {
- return false;
- }
- var graphics = PlayerSettings.GetGraphicsAPIs(BuildTarget.Android);
- if (graphics != null && graphics.Length >= 1 &&
- graphics[0] == UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3)
- {
- return true;
- }
- return false;
- }
- else
- {
- return false;
- }
- }
- public override void DrawGUI()
- {
- string message = @"Graphics APIs should be set to OpenGLES. Player Settings > Other Settings > Graphics APIs , choose 'OpenGLES3'.";
- DrawContent("Graphics APIs is not OpenGLES", message);
- }
- public override bool IsFixable()
- {
- return true;
- }
- public override void Fix()
- {
- if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
- {
- PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.Android, false);
- PlayerSettings.SetGraphicsAPIs(BuildTarget.Android, new GraphicsDeviceType[1] { GraphicsDeviceType.OpenGLES3 });
- }
- }
- public override bool IsAutoPop()
- {
- return true;
- }
- }
- private class CkeckMTRendering : FixItem
- {
- public CkeckMTRendering(MessageType level) : base(level)
- {
- key = this.GetType().Name;
- }
- public override bool IsValid()
- {
- if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
- {
- return !PlayerSettings.GetMobileMTRendering(BuildTargetGroup.Android);
- }
- else
- {
- return false;
- }
- }
- public override void DrawGUI()
- {
- string message = @"In order to run correct on mobile devices, the RenderingThreadingMode should be set.
- in dropdown list of Player Settings > Other Settings > Multithreaded Rendering, close toggle.";
- DrawContent("Multithreaded Rendering not close", message);
- }
- public override bool IsFixable()
- {
- return true;
- }
- public override void Fix()
- {
- if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
- {
- PlayerSettings.SetMobileMTRendering(BuildTargetGroup.Android, false);
- }
- }
- public override bool IsAutoPop()
- {
- return true;
- }
- }
- private class CkeckAndroidOrientation : FixItem
- {
- public CkeckAndroidOrientation(MessageType level) : base(level)
- {
- key = this.GetType().Name;
- }
- public override bool IsValid()
- {
- return PlayerSettings.defaultInterfaceOrientation == UIOrientation.Portrait;
- }
- public override void DrawGUI()
- {
- string message = @"In order to display correct on mobile devices, the orientation should be set to portrait.
- in dropdown list of Player Settings > Resolution and Presentation > Default Orientation, choose 'Portrait'.";
- DrawContent("Orientation is not portrait", message);
- }
- public override bool IsFixable()
- {
- return true;
- }
- public override void Fix()
- {
- if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
- {
- PlayerSettings.defaultInterfaceOrientation = UIOrientation.Portrait;
- }
- }
- public override bool IsAutoPop()
- {
- return true;
- }
- }
- private class CkeckColorSpace : FixItem
- {
- public CkeckColorSpace(MessageType level) : base(level)
- {
- key = this.GetType().Name;
- }
- public override bool IsValid()
- {
- return PlayerSettings.colorSpace == ColorSpace.Gamma;
- }
- public override void DrawGUI()
- {
- string message = @"In order to display correct on mobile devices, the colorSpace should be set to gamma.
- in dropdown list of Player Settings > Other Settings > Color Space, choose 'Gamma'.";
- DrawContent("ColorSpace is not Linear", message);
- }
- public override bool IsFixable()
- {
- return true;
- }
- public override void Fix()
- {
- if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
- {
- PlayerSettings.colorSpace = ColorSpace.Gamma;
- }
- }
- public override bool IsAutoPop()
- {
- return true;
- }
- }
- private class CkeckAndroidPermission : FixItem
- {
- public CkeckAndroidPermission(MessageType level) : base(level)
- {
- key = this.GetType().Name;
- }
- public override bool IsValid()
- {
- if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
- {
- return PlayerSettings.Android.forceInternetPermission;
- }
- else
- {
- return false;
- }
- }
- public override void DrawGUI()
- {
- string message = @"In order to run correct on mobile devices, the internet access premission should be set.
- in dropdown list of Player Settings > Other Settings > Internet Access, choose 'Require'.";
- DrawContent("internet access permission not available", message);
- }
- public override bool IsFixable()
- {
- return true;
- }
- public override void Fix()
- {
- if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
- {
- PlayerSettings.Android.forceInternetPermission = true;
- }
- }
- public override bool IsAutoPop()
- {
- return true;
- }
- }
- //todo...添加最低版本号的检查
- private class CheckUnityMinVersion : FixItem
- {
- string unityVersion;//
- public CheckUnityMinVersion(MessageType level) : base(level)
- {
- key = this.GetType().Name;
- unityVersion = Application.unityVersion;
- }
- public override void DrawGUI()
- {
- string message = @"The minimum Unity version required is 2020.3.36";
- DrawContent("Unity version not valid ", message);
- }
- public override void Fix()
- {
- }
- public override bool IsAutoPop()
- {
- return true;
- }
- public override bool IsFixable()
- {
- return unityVersion.CompareTo(minUnityVersion) == 1;
- }
- public override bool IsValid()
- {
- return unityVersion.CompareTo(minUnityVersion) == 1;
- }
- }
- private class CheckOptimizedFramePacing : FixItem
- {
- public CheckOptimizedFramePacing(MessageType level) : base(level)
- {
- key = this.GetType().Name;
- }
- public override void DrawGUI()
- {
- string message = @"The optimizedFramePacing need to unselect";
- DrawContent("OptimizedFramePacing", message);
- }
- public override void Fix()
- {
- PlayerSettings.Android.optimizedFramePacing = false;
- }
- public override bool IsAutoPop()
- {
- return true;
- }
- public override bool IsFixable()
- {
- return true;
- }
- public override bool IsValid()
- {
- return PlayerSettings.Android.optimizedFramePacing == false;
- }
- }
- private class CheckMinmumAPILevel : FixItem
- {
- public CheckMinmumAPILevel(MessageType level) : base(level)
- {
- key = this.GetType().Name;
- }
- public override void DrawGUI()
- {
- string message = @"The minSdkVersion needs to be " + minSdkVersion.ToString();
- DrawContent("MinSDKVersion", message);
- }
- public override void Fix()
- {
- PlayerSettings.Android.minSdkVersion = minSdkVersion;
- }
- public override bool IsAutoPop()
- {
- return true;
- }
- public override bool IsFixable()
- {
- return true;
- }
- public override bool IsValid()
- {
- return PlayerSettings.Android.minSdkVersion >= minSdkVersion;
- }
- }
- private class CheckArchitecture : FixItem
- {
- public CheckArchitecture(MessageType level) : base(level)
- {
- key = this.GetType().Name;
- }
- public override void DrawGUI()
- {
- string message = @"The Target Architecture should be " + targetArchitecture;
- DrawContent("Target Architecture", message);
- }
- public override void Fix()
- {
- PlayerSettings.SetScriptingBackend(BuildTargetGroup.Android, ScriptingImplementation.IL2CPP);
- PlayerSettings.Android.targetArchitectures = targetArchitecture;
- }
- public override bool IsAutoPop()
- {
- return true;
- }
- public override bool IsFixable()
- {
- return true;
- }
- public override bool IsValid()
- {
- return PlayerSettings.GetScriptingBackend(BuildTargetGroup.Android) == ScriptingImplementation.IL2CPP &&
- PlayerSettings.Android.targetArchitectures == targetArchitecture;
- }
- }
- private class CheckBuildTarget : FixItem
- {
- public CheckBuildTarget(MessageType level) : base(level)
- {
- key = this.GetType().Name;
- }
- public override void DrawGUI()
- {
- string message = @"The Build Target should be Android";
- DrawContent("Build Target", message);
- }
- public override void Fix()
- {
- EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, BuildTarget.Android);
- }
- public override bool IsAutoPop()
- {
- return true;
- }
- public override bool IsFixable()
- {
- return true;
- }
- public override bool IsValid()
- {
- return EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android;
- }
- }
- }
- }*/
|