ShadowSDKSettings.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 ShadowSDKSettings : 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("ShadowSDK/ProjectSettings")]
  21. static void Init() {
  22. if(window == null) {
  23. window = EditorWindow.GetWindow(typeof(ShadowSDKSettings));
  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/ShadowSDK/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 ShadowSDK", 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:API24(or higher)");
  127. if(PlayerSettings.Android.minSdkVersion < AndroidSdkVersions.AndroidApiLevel24) {
  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:False");
  153. if(PlayerSettings.GetMobileMTRendering(BuildTargetGroup.Android)==true) {
  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.AndroidApiLevel24) {
  234. isApply = false;
  235. }
  236. if(PlayerSettings.GetApiCompatibilityLevel(BuildTargetGroup.Android) != ApiCompatibilityLevel.NET_4_6) {
  237. isApply = false;
  238. }
  239. if(PlayerSettings.GetMobileMTRendering(BuildTargetGroup.Android) == true) {
  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.AndroidApiLevel24;
  265. PlayerSettings.Android.targetSdkVersion = AndroidSdkVersions.AndroidApiLevelAuto;
  266. PlayerSettings.SetApiCompatibilityLevel(BuildTargetGroup.Android, ApiCompatibilityLevel.NET_4_6);
  267. PlayerSettings.SetMobileMTRendering(BuildTargetGroup.Android, false);
  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 = "shadowcreator";
  286. }
  287. //if(EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android) {
  288. //Texture2D tex2D = AssetDatabase.LoadAssetAtPath(@"Assets/ShadowCreator/Plugins/app_icon.png", typeof(Texture2D)) as Texture2D;
  289. //PlatformIconKind[] icons = PlayerSettings.GetSupportedIconKindsForPlatform(BuildTargetGroup.Android);
  290. //PlayerSettings.SetIconsForTargetGroup(BuildTargetGroup.Android, new Texture2D[6] { tex2D, tex2D, tex2D, tex2D, tex2D, tex2D }, IconKind.Application);
  291. //AssetDatabase.SaveAssets();
  292. //}
  293. }
  294. #endregion
  295. #region ProjectSettings->Tags and Layers
  296. //[InitializeOnLoadMethod]
  297. //static void TagsAndLayerSet() {
  298. // bool isExist = false;
  299. // foreach(var item in UnityEditorInternal.InternalEditorUtility.layers) {
  300. // if(item == "focus") {
  301. // isExist = true;
  302. // break;
  303. // }
  304. // }
  305. // if(isExist == false) {
  306. // AutoAddLayer("focus");
  307. // }
  308. // isExist = false;
  309. // foreach(var item in UnityEditorInternal.InternalEditorUtility.layers) {
  310. // if(item == "no light") {
  311. // isExist = true;
  312. // break;
  313. // }
  314. // }
  315. // if(isExist == false) {
  316. // AutoAddLayer("no light");
  317. // }
  318. // isExist = false;
  319. // foreach(var item in UnityEditorInternal.InternalEditorUtility.tags) {
  320. // if(item == "SvrCamera") {
  321. // isExist = true;
  322. // break;
  323. // }
  324. // }
  325. // if(isExist == false) {
  326. // UnityEditorInternal.InternalEditorUtility.AddTag("SvrCamera");
  327. // }
  328. //}
  329. static void AutoAddLayer(string layer) {
  330. SerializedObject tagMagager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/Tagmanager.asset"));
  331. SerializedProperty it = tagMagager.GetIterator();
  332. while(it.NextVisible(true)) {
  333. if(it.name.Equals("layers")) {
  334. for(int i = 0; i < it.arraySize; i++) {
  335. if(i <= 7) {
  336. continue;
  337. }
  338. SerializedProperty sp = it.GetArrayElementAtIndex(i);
  339. if(string.IsNullOrEmpty(sp.stringValue)) {
  340. sp.stringValue = layer;
  341. tagMagager.ApplyModifiedProperties();
  342. return;
  343. }
  344. }
  345. }
  346. }
  347. }
  348. //static void AutoAddLayer(string layer) {
  349. // if(!HasThisLayer(layer)) {
  350. // SerializedObject tagMagager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/Tagmanager.asset"));
  351. // SerializedProperty it = tagMagager.GetIterator();
  352. // while(it.NextVisible(true)) {
  353. // if(it.name.Equals("layers")) {
  354. // for(int i = 0; i < it.arraySize; i++) {
  355. // if(i <= 7) {
  356. // continue;
  357. // }
  358. // SerializedProperty sp = it.GetArrayElementAtIndex(i);
  359. // if(string.IsNullOrEmpty(sp.stringValue)) {
  360. // sp.stringValue = layer;
  361. // tagMagager.ApplyModifiedProperties();
  362. // return;
  363. // }
  364. // }
  365. // }
  366. // }
  367. // }
  368. //}
  369. //static bool HasThisLayer(string layer) {
  370. // //先清除已保存的
  371. // SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/Tagmanager.asset"));
  372. // SerializedProperty it = tagManager.GetIterator();
  373. // while(it.NextVisible(true)) {
  374. // if(it.name.Equals("layers")) {
  375. // for(int i = 0; i < it.arraySize; i++) {
  376. // if(i <= 7) {
  377. // continue;
  378. // }
  379. // SerializedProperty sp = it.GetArrayElementAtIndex(i);
  380. // if(!string.IsNullOrEmpty(sp.stringValue)) {
  381. // if(sp.stringValue.Equals(layer)) {
  382. // sp.stringValue = string.Empty;
  383. // tagManager.ApplyModifiedProperties();
  384. // }
  385. // }
  386. // }
  387. // }
  388. // }
  389. // for(int i = 0; i < UnityEditorInternal.InternalEditorUtility.layers.Length; i++) {
  390. // if(UnityEditorInternal.InternalEditorUtility.layers[i].Contains(layer)) {
  391. // return true;
  392. // }
  393. // }
  394. // return false;
  395. //}
  396. #endregion
  397. #region ProjectSettings->Quality
  398. static bool QualityWindow() {
  399. bool isApply = true;
  400. GUILayout.Label("QulitySettings", EditorStyles.boldLabel);
  401. EditorGUILayout.BeginHorizontal();
  402. GUILayout.Label("QulityLevel:Middle");
  403. if(QualitySettings.GetQualityLevel() != 2) {
  404. GUIStyle styleSlide = new GUIStyle();
  405. styleSlide.normal.textColor = Color.red;
  406. GUILayout.Label("Failed", styleSlide);
  407. isApply = false;
  408. } else {
  409. GUIStyle styleApplied = new GUIStyle();
  410. styleApplied.normal.textColor = Color.green;
  411. GUILayout.Label("Applied", styleApplied);
  412. }
  413. EditorGUILayout.EndHorizontal();
  414. return isApply;
  415. }
  416. static bool QualityCheck() {
  417. bool isApply = true;
  418. if(QualitySettings.GetQualityLevel() != 2) {
  419. isApply = false;
  420. }
  421. return isApply;
  422. }
  423. static void QualitySet() {
  424. QualitySettings.SetQualityLevel(2);
  425. }
  426. #endregion
  427. }