PXR_SDKSettingEditor.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*******************************************************************************
  2. Copyright © 2015-2022 PICO Technology Co., Ltd.All rights reserved.
  3. NOTICE:All information contained herein is, and remains the property of
  4. PICO Technology Co., Ltd. The intellectual and technical concepts
  5. contained herein are proprietary to PICO Technology Co., Ltd. and may be
  6. covered by patents, patents in process, and are protected by trade secret or
  7. copyright law. Dissemination of this information or reproduction of this
  8. material is strictly forbidden unless prior written permission is obtained from
  9. PICO Technology Co., Ltd.
  10. *******************************************************************************/
  11. using System.IO;
  12. using Unity.XR.PXR;
  13. using UnityEditor;
  14. using UnityEditor.Build;
  15. using UnityEngine;
  16. namespace Unity.XR.PXR.Editor
  17. {
  18. [InitializeOnLoad]
  19. public class PXR_SDKSettingEditor : EditorWindow
  20. {
  21. public static PXR_SDKSettingEditor window;
  22. public static string assetPath = "Assets/Resources/";
  23. GUIContent myTitleContent = new GUIContent();
  24. static Language language = Language.English;
  25. const BuildTarget recommendedBuildTarget = BuildTarget.Android;
  26. const UIOrientation recommendedOrientation = UIOrientation.LandscapeLeft;
  27. public bool toggleBuildTarget = true;
  28. public bool toggleOrientation = true;
  29. GUIStyle styleApply;
  30. static string[] strWindowName = { "PXR SDK Setting", "PXR SDK 设置" };
  31. string strseparate = "______________________________________________________________________________________________________________________________________________";
  32. string[] strNoticeText = { "Notice: Recommended project settings for PXR SDK", "注意:PXR SDK 推荐项目配置" };
  33. string[] strBtnChange = { "切换至中文", "Switch to English" };
  34. string[] strApplied = { "Applied", "已应用" };
  35. string[] strInformationText = { "Information:", "信息说明" };
  36. string[] strInfo1Text =
  37. {
  38. "1 Support Unity Version: Unity2020.3.21 and above",
  39. "1 支持Unity版本:Unity2020.3.21及以上版本"
  40. };
  41. string[] strInfo2Text =
  42. {
  43. "2 Player Setting: " + " Default Orientation setting Landscape Left",
  44. "2 Player Setting: " + " Default Orientation setting Landscape Left"
  45. };
  46. string[] strInfo5Text = { "3 Get the lastest version of SDK:", "3 获取最新版本的SDK:" };
  47. string[] strInfoURL = { "https://developer-global.pico-interactive.com/", "https://developer-global.pico-interactive.com/" };
  48. string[] strConfigurationText = { "Configuration:", "配置" };
  49. string[] strConfiguration1Text =
  50. {
  51. "1 current: Build Target = {0}\n" +
  52. " Recommended: Build Target = {1}\n",
  53. "1 当前: Build Target = {0}\n" +
  54. " 推荐: Build Target = {1}\n"
  55. };
  56. string[] strConfiguration3Text =
  57. {
  58. "3 current: Orientation = {0}\n" +
  59. " Recommended: Orientation = {1}\n",
  60. "3 当前: Orientation = {0}\n" +
  61. " 推荐: Orientation = {1}\n"
  62. };
  63. string[] strBtnApply = { "Apply", "应用" };
  64. string[] strBtnClose = { "Close", "关闭" };
  65. static PXR_SDKSettingEditor()
  66. {
  67. EditorApplication.update += Update;
  68. }
  69. static void Init()
  70. {
  71. IsIgnoreWindow();
  72. ShowSettingWindow();
  73. }
  74. static void Update()
  75. {
  76. bool allApplied = IsAllApplied();
  77. bool showWindow = !allApplied;
  78. bool isIgnoreWindow = IsIgnoreWindow();
  79. if (isIgnoreWindow)
  80. {
  81. showWindow = false;
  82. }
  83. if (showWindow)
  84. {
  85. ShowSettingWindow();
  86. }
  87. EditorApplication.update -= Update;
  88. }
  89. public static bool IsIgnoreWindow()
  90. {
  91. string path = assetPath + typeof(PXR_SDKSettingAsset).ToString() + ".asset";
  92. if (File.Exists(path))
  93. {
  94. PXR_SDKSettingAsset asset = AssetDatabase.LoadAssetAtPath<PXR_SDKSettingAsset>(path);
  95. return asset.ignoreSDKSetting;
  96. }
  97. return false;
  98. }
  99. public void OnDisable()
  100. {
  101. PXR_SDKSettingAsset asset;
  102. string assetPath = PXR_SDKSettingEditor.assetPath + typeof(PXR_SDKSettingAsset).ToString() + ".asset";
  103. if (File.Exists(assetPath))
  104. {
  105. asset = AssetDatabase.LoadAssetAtPath<PXR_SDKSettingAsset>(assetPath);
  106. }
  107. else
  108. {
  109. asset = new PXR_SDKSettingAsset();
  110. ScriptableObjectUtility.CreateAsset<PXR_SDKSettingAsset>(asset, PXR_SDKSettingEditor.assetPath);
  111. }
  112. PXR_ProjectSetting.GetProjectConfig();
  113. }
  114. static void ShowSettingWindow()
  115. {
  116. if (window != null)
  117. return;
  118. window = (PXR_SDKSettingEditor)GetWindow(typeof(PXR_SDKSettingEditor), true, strWindowName[(int)language], true);
  119. window.autoRepaintOnSceneChange = true;
  120. window.minSize = new Vector2(960, 620);
  121. }
  122. string GetResourcePath()
  123. {
  124. var ms = MonoScript.FromScriptableObject(this);
  125. var path = AssetDatabase.GetAssetPath(ms);
  126. path = Path.GetDirectoryName(path);
  127. return path.Substring(0, path.Length - "Editor".Length) + "Textures/";
  128. }
  129. public void OnGUI()
  130. {
  131. myTitleContent.text = strWindowName[(int)language];
  132. if (window != null)
  133. {
  134. window.titleContent = myTitleContent;
  135. }
  136. ShowNoticeTextAndChangeBtn();
  137. GUIStyle styleSlide = new GUIStyle();
  138. styleSlide.normal.textColor = Color.white;
  139. GUILayout.Label(strseparate, styleSlide);
  140. GUILayout.Label(strInformationText[(int)language]);
  141. GUILayout.Label(strInfo1Text[(int)language]);
  142. GUILayout.Label(strInfo2Text[(int)language]);
  143. GUILayout.Label(strInfo5Text[(int)language]);
  144. string strURL = strInfoURL[(int)language];
  145. GUIStyle style = new GUIStyle();
  146. style.normal.textColor = new Color(0, 122f / 255f, 204f / 255f);
  147. if (GUILayout.Button(" " + strURL, style, GUILayout.Width(200)))
  148. {
  149. Application.OpenURL(strURL);
  150. }
  151. GUILayout.Label(strseparate, styleSlide);
  152. GUILayout.Label(strConfigurationText[(int)language]);
  153. string strinfo1 = string.Format(strConfiguration1Text[(int)language], EditorUserBuildSettings.activeBuildTarget, recommendedBuildTarget);
  154. EditorConfigurations(strinfo1, EditorUserBuildSettings.activeBuildTarget == recommendedBuildTarget, ref toggleBuildTarget);
  155. string strinfo3 = string.Format(strConfiguration3Text[(int)language],
  156. PlayerSettings.defaultInterfaceOrientation, recommendedOrientation);
  157. EditorConfigurations(strinfo3, PlayerSettings.defaultInterfaceOrientation == recommendedOrientation,
  158. ref toggleOrientation);
  159. EditorGUILayout.Space();
  160. EditorGUILayout.BeginHorizontal();
  161. EditorGUILayout.LabelField("", GUILayout.Width(200));
  162. if (IsAllApplied())
  163. {
  164. styleApply = new GUIStyle("ObjectPickerBackground");
  165. styleApply.alignment = TextAnchor.MiddleCenter;
  166. }
  167. else
  168. {
  169. styleApply = new GUIStyle("LargeButton");
  170. styleApply.alignment = TextAnchor.MiddleCenter;
  171. }
  172. if (GUILayout.Button(strBtnApply[(int)language], styleApply, GUILayout.Width(100), GUILayout.Height(30)))
  173. {
  174. EditorApplication.delayCall += OnClickApply;
  175. }
  176. styleApply = null;
  177. EditorGUILayout.LabelField("", GUILayout.Width(200));
  178. if (GUILayout.Button(strBtnClose[(int)language], GUILayout.Width(100), GUILayout.Height(30)))
  179. {
  180. OnClickClose();
  181. }
  182. EditorGUILayout.EndHorizontal();
  183. }
  184. public void OnClickApply()
  185. {
  186. if (toggleOrientation && PlayerSettings.defaultInterfaceOrientation != recommendedOrientation)
  187. {
  188. PlayerSettings.defaultInterfaceOrientation = recommendedOrientation;
  189. }
  190. if (toggleBuildTarget && EditorUserBuildSettings.activeBuildTarget != recommendedBuildTarget)
  191. {
  192. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, recommendedBuildTarget);
  193. EditorUserBuildSettings.selectedBuildTargetGroup = BuildTargetGroup.Android;
  194. }
  195. PXR_SDKSettingAsset asset;
  196. string assetPath = PXR_SDKSettingEditor.assetPath + typeof(PXR_SDKSettingAsset).ToString() + ".asset";
  197. if (File.Exists(assetPath))
  198. {
  199. asset = AssetDatabase.LoadAssetAtPath<PXR_SDKSettingAsset>(assetPath);
  200. }
  201. else
  202. {
  203. asset = new PXR_SDKSettingAsset();
  204. ScriptableObjectUtility.CreateAsset<PXR_SDKSettingAsset>(asset, PXR_SDKSettingEditor.assetPath);
  205. }
  206. PXR_ProjectSetting.GetProjectConfig();
  207. }
  208. void OnClickClose()
  209. {
  210. bool allApplied = IsAllApplied();
  211. if (allApplied)
  212. {
  213. Close();
  214. }
  215. else
  216. {
  217. PXR_SettingMessageBoxEditor.Init(language);
  218. }
  219. PXR_ProjectSetting.GetProjectConfig();
  220. }
  221. public static bool IsAllApplied()
  222. {
  223. bool notApplied = (EditorUserBuildSettings.activeBuildTarget != recommendedBuildTarget) ||
  224. (PlayerSettings.defaultInterfaceOrientation != recommendedOrientation);
  225. return !notApplied;
  226. }
  227. void EditorConfigurations(string strConfiguration, bool enable, ref bool toggle)
  228. {
  229. EditorGUILayout.BeginHorizontal();
  230. GUILayout.Label(strConfiguration, GUILayout.Width(500));
  231. GUIStyle styleApplied = new GUIStyle();
  232. styleApplied.normal.textColor = Color.green;
  233. if (enable)
  234. {
  235. GUILayout.Label(strApplied[(int)language], styleApplied);
  236. }
  237. else
  238. {
  239. toggle = EditorGUILayout.Toggle(toggle);
  240. }
  241. EditorGUILayout.EndHorizontal();
  242. }
  243. void ShowLogo()
  244. {
  245. var resourcePath = GetResourcePath();
  246. var logo = AssetDatabase.LoadAssetAtPath<Texture2D>(resourcePath + "logo.png");
  247. if (logo)
  248. {
  249. var rect = GUILayoutUtility.GetRect(position.width, 150, GUI.skin.box);
  250. GUI.DrawTexture(rect, logo, ScaleMode.ScaleToFit);
  251. }
  252. }
  253. void ShowNoticeTextAndChangeBtn()
  254. {
  255. EditorGUILayout.BeginHorizontal();
  256. GUIStyle styleNoticeText = new GUIStyle();
  257. styleNoticeText.alignment = TextAnchor.UpperCenter;
  258. styleNoticeText.fontSize = 20;
  259. GUILayout.Label(strNoticeText[(int)language], styleNoticeText);
  260. if (GUILayout.Button(strBtnChange[(int)language], GUILayout.Width(150), GUILayout.Height(30)))
  261. {
  262. SwitchLanguage();
  263. }
  264. EditorGUILayout.EndHorizontal();
  265. }
  266. void SwitchLanguage()
  267. {
  268. if (language == Language.Chinese)
  269. language = Language.English;
  270. else if (language == Language.English)
  271. language = Language.Chinese;
  272. }
  273. private void SaveAssetAppIDChecked()
  274. {
  275. PXR_SDKSettingAsset asset;
  276. string assetPath = PXR_SDKSettingEditor.assetPath + typeof(PXR_SDKSettingAsset).ToString() + ".asset";
  277. if (File.Exists(assetPath))
  278. {
  279. asset = AssetDatabase.LoadAssetAtPath<PXR_SDKSettingAsset>(assetPath);
  280. }
  281. else
  282. {
  283. asset = new PXR_SDKSettingAsset();
  284. ScriptableObjectUtility.CreateAsset<PXR_SDKSettingAsset>(asset, PXR_SDKSettingEditor.assetPath);
  285. }
  286. asset.appIDChecked = true;
  287. EditorUtility.SetDirty(asset);
  288. AssetDatabase.SaveAssets();
  289. AssetDatabase.Refresh();//must Refresh
  290. }
  291. }
  292. public enum Language
  293. {
  294. English,
  295. Chinese,
  296. }
  297. public class PXR_SettingMessageBoxEditor : EditorWindow
  298. {
  299. static PXR_SettingMessageBoxEditor myWindow;
  300. static Language language = Language.English;
  301. static string[] strWindowName = { "Ignore the recommended configuration", "忽略推荐配置" };
  302. string[] strTipInfo = { " No more prompted \n" +
  303. " You can get recommended configuration from \n" +
  304. " Development documentation.",
  305. " 点击\"忽略\"后,不再提示!\n"+
  306. " 可从开发者文档中获取推荐配置说明 \n"};
  307. string[] strBtnIgnore = { "Ignore", "忽略" };
  308. string[] strBtnCancel = { "Cancel", "取消" };
  309. public static void Init(Language language)
  310. {
  311. PXR_SettingMessageBoxEditor.language = language;
  312. myWindow = (PXR_SettingMessageBoxEditor)GetWindow(typeof(PXR_SettingMessageBoxEditor), true, strWindowName[(int)language], true);
  313. myWindow.autoRepaintOnSceneChange = true;
  314. myWindow.minSize = new Vector2(360, 200);
  315. myWindow.Show(true);
  316. Rect pos;
  317. if (PXR_SDKSettingEditor.window != null)
  318. {
  319. Rect frect = PXR_SDKSettingEditor.window.position;
  320. pos = new Rect(frect.x + 300, frect.y + 200, 200, 140);
  321. }
  322. else
  323. {
  324. pos = new Rect(700, 400, 200, 140);
  325. }
  326. myWindow.position = pos;
  327. }
  328. void OnGUI()
  329. {
  330. for (int i = 0; i < 10; i++)
  331. {
  332. EditorGUILayout.Space();
  333. }
  334. GUILayout.Label(strTipInfo[(int)language]);
  335. for (int i = 0; i < 3; i++)
  336. {
  337. EditorGUILayout.Space();
  338. }
  339. EditorGUILayout.BeginHorizontal();
  340. EditorGUILayout.LabelField("", GUILayout.Width(20));
  341. if (GUILayout.Button(strBtnIgnore[(int)language], GUILayout.Width(100), GUILayout.Height(30)))
  342. {
  343. OnClickIgnore();
  344. }
  345. EditorGUILayout.LabelField("", GUILayout.Width(50));
  346. if (GUILayout.Button(strBtnCancel[(int)language], GUILayout.Width(130), GUILayout.Height(30)))
  347. {
  348. OnClickCancel();
  349. }
  350. EditorGUILayout.EndHorizontal();
  351. }
  352. void OnClickIgnore()
  353. {
  354. SaveAssetDataBase();
  355. PXR_SDKSettingEditor.window.Close();
  356. Close();
  357. }
  358. private void SaveAssetDataBase()
  359. {
  360. PXR_SDKSettingAsset asset;
  361. string assetPath = PXR_SDKSettingEditor.assetPath + typeof(PXR_SDKSettingAsset).ToString() + ".asset";
  362. if (File.Exists(assetPath))
  363. {
  364. asset = AssetDatabase.LoadAssetAtPath<PXR_SDKSettingAsset>(assetPath);
  365. }
  366. else
  367. {
  368. asset = new PXR_SDKSettingAsset();
  369. ScriptableObjectUtility.CreateAsset<PXR_SDKSettingAsset>(asset, PXR_SDKSettingEditor.assetPath);
  370. }
  371. asset.ignoreSDKSetting = true;
  372. EditorUtility.SetDirty(asset);
  373. AssetDatabase.SaveAssets();
  374. AssetDatabase.Refresh();
  375. }
  376. void OnClickCancel()
  377. {
  378. Close();
  379. }
  380. }
  381. public static class ScriptableObjectUtility
  382. {
  383. public static void CreateAsset<T>(T classdata, string path) where T : ScriptableObject
  384. {
  385. if (!Directory.Exists(path))
  386. {
  387. Directory.CreateDirectory(path);
  388. }
  389. string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + typeof(T).ToString() + ".asset");
  390. AssetDatabase.CreateAsset(classdata, assetPathAndName);
  391. AssetDatabase.SaveAssets();
  392. AssetDatabase.Refresh();
  393. }
  394. }
  395. }
粤ICP备19079148号