NxrQualitySettingsEditor.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace Nxr.Internal
  5. {
  6. public class NxrQualitySettingsEditor : EditorWindow
  7. {
  8. public const string PRODUCT_NAME_VR = "NibiruXRDemo_VR";
  9. public const string PRODUCT_NAME_AR = "NibiruXRDemo_AR";
  10. public const string PACKAGE_NAME_XR = "com.nibiru.xr.unitydemo";
  11. const string NXR_ANDROID_ARM64_PATH = "/Plugins/Android/libs/arm64-v8a/";
  12. const string NXR_ANDROID_SLAM_ARM64_PATH = "/NXRSlam/Plugins/Android/libs/arm64-v8a/";
  13. string[] NXR_ANDROID_ARM64_SO = new string[] {
  14. "libnvr_local.so",
  15. "libnvr_unity.so",
  16. "libtensorflow_inference.so",
  17. "libnxr_slam.so"
  18. };
  19. string[] NXR_ANDROID_SLAM_ARM64_SO = new string[] {
  20. "libnxr_slam.so"
  21. };
  22. const string TMP_SUFFIX = ".tmpa";
  23. public enum QualityLevel
  24. {
  25. Low = 0,
  26. Middle,
  27. High
  28. }
  29. const string NRRPath = "NibiruXR/Android Remote NRR";
  30. // ANDROID_REMOTE_NRR
  31. [MenuItem(NRRPath, false, 89)]
  32. public static void MenuCheckRemoteNRRMode()
  33. {
  34. #if UNITY_ANDROID && UNITY_EDITOR
  35. bool flag = Menu.GetChecked(NRRPath);
  36. flag = !flag;
  37. Menu.SetChecked(NRRPath, flag);
  38. if(flag)
  39. {
  40. string symbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
  41. PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, symbols + ";ANDROID_REMOTE_NRR");
  42. } else
  43. {
  44. string symbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
  45. if(symbols.Contains("ANDROID_REMOTE_NRR"))
  46. {
  47. symbols = symbols.Replace("ANDROID_REMOTE_NRR", "");
  48. }
  49. PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, symbols);
  50. }
  51. #endif
  52. }
  53. [MenuItem(NRRPath, true)]
  54. public static bool RemoteNRRMenuCheckBefore()
  55. {
  56. bool isChecked = false;
  57. string symbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
  58. if (symbols.Contains("ANDROID_REMOTE_NRR"))
  59. {
  60. isChecked = true;
  61. }
  62. Menu.SetChecked(NRRPath, isChecked);
  63. return true;
  64. }
  65. [MenuItem("NibiruXR/XR Settings", false, 80)]
  66. public static void createMenu()
  67. {
  68. NxrQualitySettingsEditor window = (NxrQualitySettingsEditor)EditorWindow.GetWindow(typeof(NxrQualitySettingsEditor));
  69. window.qualityLevel = (QualityLevel) EditorPrefs.GetInt("qualitylevel");
  70. window.hmdType = window.GetHmdType();
  71. window.RefreshTargetArchitectures();
  72. window.Show();
  73. }
  74. QualityLevel qualityLevel = QualityLevel.Middle;
  75. HMD_TYPE hmdType = HMD_TYPE.NONE;
  76. Target_Architectures targetArchitectures = Target_Architectures.ARMV7;
  77. bool IsARM64Enabled()
  78. {
  79. FileInfo fi = new FileInfo(Application.dataPath + NXR_ANDROID_ARM64_PATH + NXR_ANDROID_ARM64_SO[0]); //xx/xx/aa.rar
  80. return fi.Exists;
  81. }
  82. void RefreshTargetArchitectures()
  83. {
  84. targetArchitectures = IsARM64Enabled() ? Target_Architectures.ARMV7_AND_ARM64 : Target_Architectures.ARMV7;
  85. }
  86. private void OnGUI()
  87. {
  88. GUILayout.Space(20);
  89. GUIStyle firstLevelStyle = new GUIStyle(GUI.skin.label);
  90. firstLevelStyle.fontStyle = FontStyle.Bold;
  91. EditorGUILayout.LabelField("Quality settings :", firstLevelStyle);
  92. qualityLevel = (QualityLevel)EditorGUILayout.EnumPopup("", qualityLevel);
  93. GUILayout.Space(20);
  94. if (qualityLevel == QualityLevel.Low)
  95. {
  96. QualitySettings.antiAliasing = 0;
  97. QualitySettings.shadowResolution = ShadowResolution.Low;
  98. }
  99. else if (qualityLevel == QualityLevel.Middle)
  100. {
  101. QualitySettings.antiAliasing = 0;
  102. QualitySettings.shadowResolution = ShadowResolution.Medium;
  103. }
  104. else if (qualityLevel == QualityLevel.High)
  105. {
  106. QualitySettings.antiAliasing = 0;
  107. QualitySettings.shadowResolution = ShadowResolution.High;
  108. }
  109. EditorGUILayout.LabelField("HMD type :", firstLevelStyle);
  110. hmdType = (HMD_TYPE)EditorGUILayout.EnumPopup("", hmdType);
  111. GUILayout.Space(20);
  112. EditorGUILayout.LabelField("Target Architectures :", firstLevelStyle);
  113. targetArchitectures = (Target_Architectures)EditorGUILayout.EnumPopup("", targetArchitectures);
  114. GUILayout.Space(20);
  115. EditorGUILayout.LabelField("Player settings :", firstLevelStyle);
  116. EditorGUILayout.LabelField("Orientation : LandscapeLeft");
  117. EditorGUILayout.LabelField("GraphicsAPIs : OpenglEs2.0");
  118. EditorGUILayout.LabelField("V Sync Count : Don't Sync");
  119. EditorGUILayout.LabelField("MultiThreadRendering : Disable ");
  120. EditorGUILayout.LabelField("Blit Type : Never");
  121. EditorGUILayout.LabelField("Use 32-bit Display Buffer");
  122. if (GUILayout.Button("Confirm", GUILayout.Width(100), GUILayout.Height(20)))
  123. {
  124. PlayerSettings.defaultInterfaceOrientation = UIOrientation.LandscapeLeft;
  125. PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.Android, false);
  126. PlayerSettings.SetGraphicsAPIs(BuildTarget.Android, new UnityEngine.Rendering.GraphicsDeviceType[] {
  127. UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2});
  128. PlayerSettings.use32BitDisplayBuffer = true;
  129. #if UNITY_2017_4_OR_NEWER
  130. PlayerSettings.SetMobileMTRendering(BuildTargetGroup.Android, false);
  131. #else
  132. PlayerSettings.mobileMTRendering = false;
  133. #endif
  134. #if UNITY_2017_4_OR_NEWER
  135. PlayerSettings.Android.blitType = AndroidBlitType.Never;
  136. PlayerSettings.Android.targetSdkVersion = AndroidSdkVersions.AndroidApiLevel22;
  137. #endif
  138. #if UNITY_2018_4_OR_NEWER
  139. PlayerSettings.Android.startInFullscreen = true;
  140. if(targetArchitectures == Target_Architectures.ARMV7)
  141. {
  142. PlayerSettings.Android.targetArchitectures = AndroidArchitecture.ARMv7;
  143. ReleaseARM64So(false);
  144. } else if(targetArchitectures == Target_Architectures.ARMV7_AND_ARM64)
  145. {
  146. PlayerSettings.Android.targetArchitectures = AndroidArchitecture.All;
  147. ReleaseARM64So(true);
  148. }
  149. #endif
  150. PlayerSettings.Android.minSdkVersion = AndroidSdkVersions.AndroidApiLevel21;
  151. PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, "NIBIRU_" + hmdType.ToString());
  152. if(hmdType == HMD_TYPE.AR)
  153. {
  154. PlayerSettings.productName = PRODUCT_NAME_AR;
  155. } else if(hmdType == HMD_TYPE.VR)
  156. {
  157. PlayerSettings.productName = PRODUCT_NAME_VR;
  158. }
  159. #if UNITY_2017_1_OR_NEWER
  160. PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, PACKAGE_NAME_XR);
  161. #else
  162. PlayerSettings.bundleIdentifier = PACKAGE_NAME_XR;
  163. #endif
  164. QualitySettings.pixelLightCount = 1;
  165. QualitySettings.masterTextureLimit = 0;
  166. QualitySettings.anisotropicFiltering = AnisotropicFiltering.ForceEnable;
  167. #if UNITY_5_5_OR_NEWER
  168. QualitySettings.shadows = ShadowQuality.Disable;
  169. #endif
  170. // Don't Sync
  171. QualitySettings.vSyncCount = 0;
  172. SaveHmdType();
  173. Close();
  174. }
  175. }
  176. void ReleaseARM64So(bool release)
  177. {
  178. if(release)
  179. {
  180. // 释放64位so
  181. foreach(string soName in NXR_ANDROID_ARM64_SO)
  182. {
  183. string curPath = Application.dataPath + NXR_ANDROID_ARM64_PATH + soName + TMP_SUFFIX;
  184. string newPath = Application.dataPath + NXR_ANDROID_ARM64_PATH + soName;
  185. FileInfo fi = new FileInfo(curPath); //xx/xx/aa.rar
  186. if (fi.Exists) fi.MoveTo(newPath); //xx/xx/xx.rar
  187. }
  188. foreach (string soName in NXR_ANDROID_SLAM_ARM64_SO)
  189. {
  190. string curPath = Application.dataPath + NXR_ANDROID_SLAM_ARM64_PATH + soName + TMP_SUFFIX;
  191. string newPath = Application.dataPath + NXR_ANDROID_SLAM_ARM64_PATH + soName;
  192. FileInfo fi = new FileInfo(curPath); //xx/xx/aa.rar
  193. if (fi.Exists) fi.MoveTo(newPath); //xx/xx/xx.rar
  194. }
  195. }
  196. else
  197. {
  198. // 注释64位so
  199. foreach (string soName in NXR_ANDROID_ARM64_SO)
  200. {
  201. string curPath = Application.dataPath + NXR_ANDROID_ARM64_PATH + soName;
  202. string newPath = Application.dataPath + NXR_ANDROID_ARM64_PATH + soName + TMP_SUFFIX;
  203. FileInfo fi = new FileInfo(curPath); //xx/xx/aa.rar
  204. if(fi.Exists) fi.MoveTo(newPath); //xx/xx/xx.rar
  205. }
  206. foreach (string soName in NXR_ANDROID_SLAM_ARM64_SO)
  207. {
  208. string curPath = Application.dataPath + NXR_ANDROID_SLAM_ARM64_PATH + soName;
  209. string newPath = Application.dataPath + NXR_ANDROID_SLAM_ARM64_PATH + soName + TMP_SUFFIX;
  210. FileInfo fi = new FileInfo(curPath); //xx/xx/aa.rar
  211. if (fi.Exists) fi.MoveTo(newPath); //xx/xx/xx.rar
  212. }
  213. }
  214. }
  215. HMD_TYPE GetHmdType()
  216. {
  217. string data = NxrPluginEditor.Read("AndroidManifest.xml");
  218. string[] lines = data.Split('\n');
  219. for (int i = 0, l = lines.Length; i < l; i++)
  220. {
  221. string lineContent = lines[i];
  222. if (lineContent.Contains("HMD_TYPE"))
  223. {
  224. return lineContent.Contains("VR") ? HMD_TYPE.VR : (lineContent.Contains("AR") ? HMD_TYPE.AR : HMD_TYPE.NONE);
  225. }
  226. }
  227. return HMD_TYPE.NONE;
  228. }
  229. void SaveHmdType()
  230. {
  231. string data = NxrPluginEditor.Read("AndroidManifest.xml");
  232. string[] lines = data.Split('\n');
  233. string newdata = "";
  234. for (int i = 0, l = lines.Length; i < l; i++)
  235. {
  236. string lineContent = lines[i];
  237. if (lineContent.Contains("HMD_TYPE"))
  238. {
  239. lineContent = " <meta-data android:value=\"" + hmdType + "\" android:name=\"HMD_TYPE\"/>";
  240. }
  241. newdata = newdata + lineContent + "\n";
  242. }
  243. NxrPluginEditor.Write("AndroidManifest.xml", newdata);
  244. }
  245. private void OnDestroy()
  246. {
  247. EditorPrefs.SetInt("qualitylevel", (int) qualityLevel);
  248. }
  249. public class InputManagerAssistant
  250. {
  251. public enum AxisType
  252. {
  253. KeyOrMouseButton = 0,
  254. MouseMovement = 1,
  255. JoystickAxis = 2
  256. };
  257. public class InputAxis
  258. {
  259. public string name;
  260. public string descriptiveName;
  261. public string descriptiveNegativeName;
  262. public string negativeButton;
  263. public string positiveButton;
  264. public string altNegativeButton;
  265. public string altPositiveButton;
  266. public float gravity;
  267. public float dead=0.19f;
  268. public float sensitivity=1.0f;
  269. public bool snap = false;
  270. public bool invert = false;
  271. public AxisType type;
  272. public int axis;
  273. public int joyNum;
  274. }
  275. SerializedObject serializedObject = null;
  276. private SerializedProperty GetChildProperty(SerializedProperty parent, string name)
  277. {
  278. SerializedProperty child = parent.Copy();
  279. child.Next(true);
  280. do
  281. {
  282. if (child.name == name) return child;
  283. }
  284. while (child.Next(false));
  285. return null;
  286. }
  287. private bool AxisDefined(string axisName)
  288. {
  289. if(serializedObject == null) serializedObject = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0]);
  290. SerializedProperty axesProperty = serializedObject.FindProperty("m_Axes");
  291. axesProperty.Next(true);
  292. axesProperty.Next(true);
  293. while (axesProperty.Next(false))
  294. {
  295. SerializedProperty axis = axesProperty.Copy();
  296. axis.Next(true);
  297. if (axis.stringValue == axisName) return true;
  298. }
  299. return false;
  300. }
  301. public void AddAxis(InputAxis axis)
  302. {
  303. if (AxisDefined(axis.name)) return;
  304. SerializedProperty axesProperty = serializedObject.FindProperty("m_Axes");
  305. axesProperty.arraySize++;
  306. serializedObject.ApplyModifiedProperties();
  307. SerializedProperty axisProperty = axesProperty.GetArrayElementAtIndex(axesProperty.arraySize - 1);
  308. GetChildProperty(axisProperty, "m_Name").stringValue = axis.name;
  309. GetChildProperty(axisProperty, "descriptiveName").stringValue = axis.descriptiveName;
  310. GetChildProperty(axisProperty, "descriptiveNegativeName").stringValue = axis.descriptiveNegativeName;
  311. GetChildProperty(axisProperty, "negativeButton").stringValue = axis.negativeButton;
  312. GetChildProperty(axisProperty, "positiveButton").stringValue = axis.positiveButton;
  313. GetChildProperty(axisProperty, "altNegativeButton").stringValue = axis.altNegativeButton;
  314. GetChildProperty(axisProperty, "altPositiveButton").stringValue = axis.altPositiveButton;
  315. GetChildProperty(axisProperty, "gravity").floatValue = axis.gravity;
  316. GetChildProperty(axisProperty, "dead").floatValue = axis.dead;
  317. GetChildProperty(axisProperty, "sensitivity").floatValue = axis.sensitivity;
  318. GetChildProperty(axisProperty, "snap").boolValue = axis.snap;
  319. GetChildProperty(axisProperty, "invert").boolValue = axis.invert;
  320. GetChildProperty(axisProperty, "type").intValue = (int)axis.type;
  321. GetChildProperty(axisProperty, "axis").intValue = axis.axis - 1;
  322. GetChildProperty(axisProperty, "joyNum").intValue = axis.joyNum;
  323. serializedObject.ApplyModifiedProperties();
  324. }
  325. }
  326. }
  327. }