SDKSettings.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection;
  6. using UnityEditor;
  7. using UnityEditor.Build;
  8. using UnityEditor.Build.Reporting;
  9. using UnityEngine;
  10. public class SDKSettings : EditorWindow {
  11. [InitializeOnLoadMethod]
  12. static void CheckSettings() {
  13. if(EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android) {
  14. if(!IsIgnor && IsApply() == false) {
  15. Init();
  16. }
  17. }
  18. }
  19. static EditorWindow window;
  20. [MenuItem("SDK/ProjectSettings")]
  21. static void Init() {
  22. if(window == null) {
  23. // window = EditorWindow.GetWindow(typeof(SDKSettings));
  24. // window.autoRepaintOnSceneChange = true;
  25. // window.minSize = new Vector2(720, 420);
  26. // window.maxSize = new Vector2(720, 420);
  27. }
  28. }
  29. static bool isQualityApply = true;
  30. static bool isPlayerApply = true;
  31. void OnGUI() {
  32. GUILayout.Space(10);
  33. NoticWindow();
  34. if(EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android) {
  35. return;
  36. }
  37. GUILayout.Space(10);
  38. isQualityApply = QualityWindow();
  39. GUILayout.Space(10);
  40. isPlayerApply = PlayerWindow();
  41. if(IsApply() == false) {
  42. GUILayout.Space(60);
  43. ApplyWindow();
  44. }
  45. }
  46. static bool IsApply() {
  47. if(EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android) {
  48. return true;
  49. }
  50. if(PlayerCheck() && QualityCheck())
  51. return true;
  52. return false;
  53. }
  54. static bool IsIgnor { get { return GetAssetDataBase(); } }
  55. static void Ignor() {
  56. SaveAssetDataBase(true);
  57. }
  58. static string assetPath = "Assets/SDK/Common/Base/Editor/BuildSettings/Parm.asset";
  59. static void SaveAssetDataBase(bool isIgnor) {
  60. ParmScriptableObject asset;
  61. if(File.Exists(assetPath)) {
  62. asset = AssetDatabase.LoadAssetAtPath<ParmScriptableObject>(assetPath);
  63. } else {
  64. asset =(ParmScriptableObject) CreateInstance("ParmScriptableObject");
  65. AssetDatabase.CreateAsset(asset, assetPath);
  66. }
  67. asset.IsIgnor = isIgnor;
  68. AssetDatabase.SaveAssets();
  69. AssetDatabase.Refresh();//must Refresh
  70. }
  71. static bool GetAssetDataBase() {
  72. ParmScriptableObject asset;
  73. if(File.Exists(assetPath)) {
  74. asset = AssetDatabase.LoadAssetAtPath<ParmScriptableObject>(assetPath);
  75. return asset.IsIgnor;
  76. }
  77. return false;
  78. }
  79. static void NoticWindow() {
  80. GUILayout.Space(20);
  81. EditorGUILayout.BeginHorizontal();
  82. GUIStyle styleNoticeText = new GUIStyle();
  83. styleNoticeText.alignment = TextAnchor.MiddleCenter;
  84. styleNoticeText.fontSize = 14;
  85. styleNoticeText.fontStyle = FontStyle.Bold;
  86. if(EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android) {
  87. GUILayout.Label("Notice: Recommended Project settings for SDK", styleNoticeText);
  88. } else {
  89. GUILayout.Label("This Only Effect When Platform Select Android", styleNoticeText);
  90. }
  91. EditorGUILayout.EndHorizontal();
  92. GUILayout.Space(20);
  93. }
  94. void ApplyWindow() {
  95. EditorGUILayout.BeginHorizontal();
  96. EditorGUILayout.LabelField("", GUILayout.Width(100));
  97. GUIStyle styleApply = new GUIStyle("LargeButton");
  98. styleApply.alignment = TextAnchor.MiddleCenter;
  99. if(GUILayout.Button("Apply", styleApply)) {
  100. QualitySet();
  101. PlayerSet();
  102. }
  103. EditorGUILayout.LabelField("", GUILayout.Width(100));
  104. GUIStyle style1Apply = new GUIStyle("LargeButton");
  105. styleApply.alignment = TextAnchor.MiddleCenter;
  106. if(GUILayout.Button("Ignor", style1Apply)) {
  107. Ignor();
  108. window.Close();
  109. Close();
  110. }
  111. EditorGUILayout.LabelField("", GUILayout.Width(100));
  112. //if(GUILayout.Button(strBtnApply[(int)elanguage], styleApply, GUILayout.Width(100), GUILayout.Height(30))) {
  113. // EditorApplication.delayCall += OnClickApply;
  114. //}
  115. //styleApply = null;
  116. //EditorGUILayout.LabelField("", GUILayout.Width(200));
  117. //if(GUILayout.Button("xxx", GUILayout.Width(100), GUILayout.Height(30))) {
  118. //}
  119. EditorGUILayout.EndHorizontal();
  120. }
  121. #region ProjectSettings->Player
  122. static bool PlayerWindow() {
  123. bool isApply = true;
  124. GUILayout.Label("PlayerSettings", EditorStyles.boldLabel);
  125. EditorGUILayout.BeginHorizontal();
  126. GUILayout.Label("Minimum API Level:API26(or higher)");
  127. if(PlayerSettings.Android.minSdkVersion < AndroidSdkVersions.AndroidApiLevel26) {
  128. GUIStyle styleSlide = new GUIStyle();
  129. styleSlide.normal.textColor = Color.red;
  130. GUILayout.Label("Failed", styleSlide);
  131. isApply = false;
  132. } else {
  133. GUIStyle styleApplied = new GUIStyle();
  134. styleApplied.normal.textColor = Color.green;
  135. GUILayout.Label("Applied", styleApplied);
  136. }
  137. EditorGUILayout.EndHorizontal();
  138. EditorGUILayout.BeginHorizontal();
  139. GUILayout.Label("APICompatibilityLevel:.NET4.x");
  140. if(PlayerSettings.GetApiCompatibilityLevel(BuildTargetGroup.Android) != ApiCompatibilityLevel.NET_4_6) {
  141. GUIStyle styleSlide = new GUIStyle();
  142. styleSlide.normal.textColor = Color.red;
  143. GUILayout.Label("Failed", styleSlide);
  144. isApply = false;
  145. } else {
  146. GUIStyle styleApplied = new GUIStyle();
  147. styleApplied.normal.textColor = Color.green;
  148. GUILayout.Label("Applied", styleApplied);
  149. }
  150. EditorGUILayout.EndHorizontal();
  151. EditorGUILayout.BeginHorizontal();
  152. GUILayout.Label("Multithreaded Rendering:True");
  153. if(PlayerSettings.GetMobileMTRendering(BuildTargetGroup.Android)==false) {
  154. GUIStyle styleSlide = new GUIStyle();
  155. styleSlide.normal.textColor = Color.red;
  156. GUILayout.Label("Failed", styleSlide);
  157. isApply = false;
  158. } else {
  159. GUIStyle styleApplied = new GUIStyle();
  160. styleApplied.normal.textColor = Color.green;
  161. GUILayout.Label("Applied", styleApplied);
  162. }
  163. EditorGUILayout.EndHorizontal();
  164. EditorGUILayout.BeginHorizontal();
  165. GUILayout.Label("Graphics APIs:Only OpenGLES3");
  166. UnityEngine.Rendering.GraphicsDeviceType[] gapi = PlayerSettings.GetGraphicsAPIs(BuildTarget.Android);
  167. if(gapi.Length != 1 || gapi[0] != UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3) {
  168. GUIStyle styleSlide = new GUIStyle();
  169. styleSlide.normal.textColor = Color.red;
  170. GUILayout.Label("Failed", styleSlide);
  171. isApply = false;
  172. } else {
  173. GUIStyle styleApplied = new GUIStyle();
  174. styleApplied.normal.textColor = Color.green;
  175. GUILayout.Label("Applied", styleApplied);
  176. }
  177. EditorGUILayout.EndHorizontal();
  178. EditorGUILayout.BeginHorizontal();
  179. int staticBatchingValue = 0;
  180. int dynamicBatchingValue = 0;
  181. Type playerSettingsType = typeof(PlayerSettings);
  182. MethodInfo method = playerSettingsType.GetMethod("GetBatchingForPlatform",BindingFlags.NonPublic | BindingFlags.Static);
  183. object[] param = new object[] { BuildTarget.Android, staticBatchingValue, dynamicBatchingValue };
  184. method.Invoke(null, param);
  185. GUILayout.Label("Dynamic Batching:Enable");
  186. if ((int)param[2] != 1)
  187. {
  188. GUIStyle styleSlide = new GUIStyle();
  189. styleSlide.normal.textColor = Color.red;
  190. GUILayout.Label("Failed", styleSlide);
  191. isApply = false;
  192. }
  193. else
  194. {
  195. GUIStyle styleApplied = new GUIStyle();
  196. styleApplied.normal.textColor = Color.green;
  197. GUILayout.Label("Applied", styleApplied);
  198. }
  199. EditorGUILayout.EndHorizontal();
  200. EditorGUILayout.BeginHorizontal();
  201. GUILayout.Label("Asset Serialization:ForceText");
  202. if (EditorSettings.serializationMode != SerializationMode.ForceText)
  203. {
  204. GUIStyle styleSlide = new GUIStyle();
  205. styleSlide.normal.textColor = Color.red;
  206. GUILayout.Label("Failed", styleSlide);
  207. isApply = false;
  208. }
  209. else
  210. {
  211. GUIStyle styleApplied = new GUIStyle();
  212. styleApplied.normal.textColor = Color.green;
  213. GUILayout.Label("Applied", styleApplied);
  214. }
  215. EditorGUILayout.EndHorizontal();
  216. EditorGUILayout.BeginHorizontal();
  217. GUILayout.Label("Orientation:Landscape Left");
  218. if(PlayerSettings.defaultInterfaceOrientation != UIOrientation.LandscapeLeft) {
  219. GUIStyle styleSlide = new GUIStyle();
  220. styleSlide.normal.textColor = Color.red;
  221. GUILayout.Label("Failed", styleSlide);
  222. isApply = false;
  223. } else {
  224. GUIStyle styleApplied = new GUIStyle();
  225. styleApplied.normal.textColor = Color.green;
  226. GUILayout.Label("Applied", styleApplied);
  227. }
  228. EditorGUILayout.EndHorizontal();
  229. return isApply;
  230. }
  231. static bool PlayerCheck() {
  232. bool isApply = true;
  233. if(PlayerSettings.Android.minSdkVersion < AndroidSdkVersions.AndroidApiLevel26) {
  234. isApply = false;
  235. }
  236. if(PlayerSettings.GetApiCompatibilityLevel(BuildTargetGroup.Android) != ApiCompatibilityLevel.NET_4_6) {
  237. isApply = false;
  238. }
  239. if(PlayerSettings.GetMobileMTRendering(BuildTargetGroup.Android) == false) {
  240. isApply = false;
  241. }
  242. UnityEngine.Rendering.GraphicsDeviceType[] gapi = PlayerSettings.GetGraphicsAPIs(BuildTarget.Android);
  243. if(gapi.Length != 1 || gapi[0] != UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3) {
  244. isApply = false;
  245. }
  246. if(PlayerSettings.defaultInterfaceOrientation != UIOrientation.LandscapeLeft) {
  247. isApply = false;
  248. }
  249. if(EditorSettings.serializationMode != SerializationMode.ForceText) {
  250. isApply = false;
  251. }
  252. int staticBatchingValue = 0;
  253. int dynamicBatchingValue = 0;
  254. Type playerSettingsType = typeof(PlayerSettings);
  255. MethodInfo method = playerSettingsType.GetMethod("GetBatchingForPlatform", BindingFlags.NonPublic | BindingFlags.Static);
  256. object[] param = new object[] { BuildTarget.Android, staticBatchingValue, dynamicBatchingValue };
  257. method.Invoke(null, param);
  258. if((int)param[2] != 1) {
  259. isApply = false;
  260. }
  261. return isApply;
  262. }
  263. static void PlayerSet() {
  264. PlayerSettings.Android.minSdkVersion = AndroidSdkVersions.AndroidApiLevel26;
  265. PlayerSettings.Android.targetSdkVersion = AndroidSdkVersions.AndroidApiLevelAuto;
  266. PlayerSettings.SetApiCompatibilityLevel(BuildTargetGroup.Android, ApiCompatibilityLevel.NET_4_6);
  267. PlayerSettings.SetMobileMTRendering(BuildTargetGroup.Android, true);
  268. PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.Android,false);
  269. PlayerSettings.SetGraphicsAPIs(BuildTarget.Android,new UnityEngine.Rendering.GraphicsDeviceType[1] { UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3 });
  270. PlayerSettings.defaultInterfaceOrientation = UIOrientation.LandscapeLeft;
  271. EditorSettings.serializationMode = SerializationMode.ForceText;
  272. Type playerSettingsType = typeof(PlayerSettings);
  273. MethodInfo method = playerSettingsType.GetMethod("SetBatchingForPlatform", BindingFlags.NonPublic | BindingFlags.Static);
  274. int staticBatchingValue = 1;
  275. int dynamicBatchingValue = 1;
  276. object[] param = new object[] { BuildTarget.Android, staticBatchingValue, dynamicBatchingValue };
  277. method.Invoke(null, param);
  278. }
  279. [InitializeOnLoadMethod]
  280. static void PlayerSetMust() {
  281. if(PlayerSettings.productName == "New Unity Project") {
  282. PlayerSettings.productName = "SDK";
  283. }
  284. if(PlayerSettings.companyName == "DefaultCompany") {
  285. PlayerSettings.companyName = "XR";
  286. }
  287. }
  288. #endregion
  289. #region ProjectSettings->Tags and Layers
  290. //[InitializeOnLoadMethod]
  291. //static void TagsAndLayerSet() {
  292. // bool isExist = false;
  293. // foreach(var item in UnityEditorInternal.InternalEditorUtility.layers) {
  294. // if(item == "focus") {
  295. // isExist = true;
  296. // break;
  297. // }
  298. // }
  299. // if(isExist == false) {
  300. // AutoAddLayer("focus");
  301. // }
  302. // isExist = false;
  303. // foreach(var item in UnityEditorInternal.InternalEditorUtility.layers) {
  304. // if(item == "no light") {
  305. // isExist = true;
  306. // break;
  307. // }
  308. // }
  309. // if(isExist == false) {
  310. // AutoAddLayer("no light");
  311. // }
  312. // isExist = false;
  313. // foreach(var item in UnityEditorInternal.InternalEditorUtility.tags) {
  314. // if(item == "SlamCamera") {
  315. // isExist = true;
  316. // break;
  317. // }
  318. // }
  319. // if(isExist == false) {
  320. // UnityEditorInternal.InternalEditorUtility.AddTag("SlamCamera");
  321. // }
  322. //}
  323. static void AutoAddLayer(string layer) {
  324. SerializedObject tagMagager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/Tagmanager.asset"));
  325. SerializedProperty it = tagMagager.GetIterator();
  326. while(it.NextVisible(true)) {
  327. if(it.name.Equals("layers")) {
  328. for(int i = 0; i < it.arraySize; i++) {
  329. if(i <= 7) {
  330. continue;
  331. }
  332. SerializedProperty sp = it.GetArrayElementAtIndex(i);
  333. if(string.IsNullOrEmpty(sp.stringValue)) {
  334. sp.stringValue = layer;
  335. tagMagager.ApplyModifiedProperties();
  336. return;
  337. }
  338. }
  339. }
  340. }
  341. }
  342. //static void AutoAddLayer(string layer) {
  343. // if(!HasThisLayer(layer)) {
  344. // SerializedObject tagMagager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/Tagmanager.asset"));
  345. // SerializedProperty it = tagMagager.GetIterator();
  346. // while(it.NextVisible(true)) {
  347. // if(it.name.Equals("layers")) {
  348. // for(int i = 0; i < it.arraySize; i++) {
  349. // if(i <= 7) {
  350. // continue;
  351. // }
  352. // SerializedProperty sp = it.GetArrayElementAtIndex(i);
  353. // if(string.IsNullOrEmpty(sp.stringValue)) {
  354. // sp.stringValue = layer;
  355. // tagMagager.ApplyModifiedProperties();
  356. // return;
  357. // }
  358. // }
  359. // }
  360. // }
  361. // }
  362. //}
  363. //static bool HasThisLayer(string layer) {
  364. // //先清除已保存的
  365. // SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/Tagmanager.asset"));
  366. // SerializedProperty it = tagManager.GetIterator();
  367. // while(it.NextVisible(true)) {
  368. // if(it.name.Equals("layers")) {
  369. // for(int i = 0; i < it.arraySize; i++) {
  370. // if(i <= 7) {
  371. // continue;
  372. // }
  373. // SerializedProperty sp = it.GetArrayElementAtIndex(i);
  374. // if(!string.IsNullOrEmpty(sp.stringValue)) {
  375. // if(sp.stringValue.Equals(layer)) {
  376. // sp.stringValue = string.Empty;
  377. // tagManager.ApplyModifiedProperties();
  378. // }
  379. // }
  380. // }
  381. // }
  382. // }
  383. // for(int i = 0; i < UnityEditorInternal.InternalEditorUtility.layers.Length; i++) {
  384. // if(UnityEditorInternal.InternalEditorUtility.layers[i].Contains(layer)) {
  385. // return true;
  386. // }
  387. // }
  388. // return false;
  389. //}
  390. #endregion
  391. #region ProjectSettings->Quality
  392. static bool QualityWindow() {
  393. bool isApply = true;
  394. GUILayout.Label("QulitySettings", EditorStyles.boldLabel);
  395. EditorGUILayout.BeginHorizontal();
  396. GUILayout.Label("QulityLevel:Middle");
  397. if(QualitySettings.GetQualityLevel() != 2) {
  398. GUIStyle styleSlide = new GUIStyle();
  399. styleSlide.normal.textColor = Color.red;
  400. GUILayout.Label("Failed", styleSlide);
  401. isApply = false;
  402. } else {
  403. GUIStyle styleApplied = new GUIStyle();
  404. styleApplied.normal.textColor = Color.green;
  405. GUILayout.Label("Applied", styleApplied);
  406. }
  407. EditorGUILayout.EndHorizontal();
  408. return isApply;
  409. }
  410. static bool QualityCheck() {
  411. bool isApply = true;
  412. if(QualitySettings.GetQualityLevel() != 2) {
  413. isApply = false;
  414. }
  415. return isApply;
  416. }
  417. static void QualitySet() {
  418. QualitySettings.SetQualityLevel(2);
  419. }
  420. #endregion
  421. }