ThirdPartySupportPage.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using UnityEditor;
  8. using UnityEngine;
  9. namespace TheraBytes.BetterUi.Editor
  10. {
  11. public class ThirdPartySupportPage : WizardPage
  12. {
  13. const string PACKAGE_PATH_TMP_OLD = "TheraBytes/BetterUI/packages/BetterUI_TextMeshPro_UiEditorPanel.unitypackage";
  14. const string PACKAGE_PATH_TMP_NEW = "TheraBytes/BetterUI/packages/BetterUI_TextMeshPro_EditorPanelUI.unitypackage";
  15. private const string TMP_KEY = "TextMeshPro";
  16. public override string NameId { get { return "ThirdPartySupportPage"; } }
  17. public ThirdPartySupportPage(IWizard wizard)
  18. : base(wizard)
  19. {
  20. }
  21. protected override void OnInitialize()
  22. {
  23. Add(new InfoWizardPageElement("Additional Packages", InfoType.Header));
  24. // TextMesh Pro
  25. Add(new SeparatorWizardPageElement());
  26. Add(new InfoWizardPageElement("TextMeth Pro", InfoType.Header));
  27. string tmpAddOnPath = Path.Combine(Application.dataPath, "TheraBytes/BetterUI_TextMeshPro");
  28. if (Directory.Exists(tmpAddOnPath))
  29. {
  30. Add(new InfoWizardPageElement("TextMesh Pro add-on is already installed."));
  31. Add(new ValueWizardPageElement<string>(TMP_KEY,
  32. (o, v) =>
  33. {
  34. if(GUILayout.Button("Remove TextMesh Pro add-on"))
  35. {
  36. if(EditorUtility.DisplayDialog("Remove TextMesh Pro add-on?",
  37. "Are you sure you want to remove Better UI support for TextMesh Pro? (some UIs could break)",
  38. "Remove it!", "Cancel"))
  39. {
  40. wizard.DoReloadOperation(this, () =>
  41. {
  42. try
  43. {
  44. File.Delete(tmpAddOnPath + ".meta");
  45. Directory.Delete(tmpAddOnPath, true);
  46. }
  47. catch
  48. {
  49. Debug.LogError("Could not properly delete " + tmpAddOnPath);
  50. }
  51. AssetDatabase.Refresh();
  52. v = null;
  53. });
  54. }
  55. }
  56. return v;
  57. }).MarkComplete());
  58. }
  59. else
  60. {
  61. int v1, v2, v3;
  62. bool foundTmpVersion = TryGetTextMethProPackageVersion(out v1, out v2, out v3);
  63. if (foundTmpVersion)
  64. {
  65. string versionString = string.Format("{0}.{1}.{2}", v1, v2, v3);
  66. Add(new InfoWizardPageElement(string.Format("TextMesh pro version {0} detected.", versionString)));
  67. if (v1 > 3 || (v1 == 3 && v2 > 0) || (v1 == 3 && v2 == 0 && v3 > 6))
  68. {
  69. Add(new InfoWizardPageElement("The latest tested TextMesh Pro version is 3.0.6.\n" +
  70. "It is very likely that the Better UI add-on for TextMesh Pro will also work with version " + versionString +
  71. " but it cannot be guaranteed. If it doesnt work with this version, please write a mail to info@therabytes.de.",
  72. InfoType.WarningBox));
  73. }
  74. if (v1 < 1 || (v1 == 1 && v2 == 0 && v3 < 54))
  75. {
  76. Add(new InfoWizardPageElement("Your version of TextMesh Pro is too old and not supported. Please upgrade at least to version 1.0.54.",
  77. InfoType.ErrorBox));
  78. }
  79. else
  80. {
  81. const string assertionText = "You have a TextMesh Pro package installed which is not supported in your version of Unity. You should upgrade TextMesh Pro.";
  82. bool isNewBaseClass =
  83. #if UNITY_2020_1_OR_NEWER
  84. (v1 >= 3); // should always be the case
  85. Debug.Assert(v1 >= 3, assertionText);
  86. #elif UNITY_2019_4_OR_NEWER
  87. (v1 == 2 && v2 >= 1) || (v1 > 2);
  88. Debug.Assert(v1 == 2, assertionText);
  89. #else
  90. (v1 == 1 && v2 >= 5) || (v1 > 1);
  91. Debug.Assert(v1 == 1, assertionText);
  92. #endif
  93. string packageName = (isNewBaseClass)
  94. ? PACKAGE_PATH_TMP_NEW
  95. : PACKAGE_PATH_TMP_OLD;
  96. Add(new ValueWizardPageElement<string>(TMP_KEY,
  97. (o, v) =>
  98. {
  99. if (GUILayout.Button("Import TextMesh Pro Add-On"))
  100. {
  101. wizard.DoReloadOperation(this, () =>
  102. {
  103. AssetDatabase.ImportPackage(Path.Combine(Application.dataPath, packageName), false);
  104. v = packageName;
  105. });
  106. }
  107. return v;
  108. }).MarkComplete());
  109. }
  110. }
  111. else
  112. {
  113. Add(new InfoWizardPageElement("TextMesh Pro could not be detected."));
  114. Add(new InfoWizardPageElement("You may install the right Better UI add-on now " +
  115. "but if you don't have TextMesh Pro installed in your project, " +
  116. "you will face compile errors until TextMesh Pro is installed or the add on folder is deleted again.",
  117. InfoType.WarningBox));
  118. Add(new InfoWizardPageElement("Please select the add on for the text mesh pro version you have installed."));
  119. #if UNITY_2020_1_OR_NEWER
  120. string textNewVersion = "v3.0 or above";
  121. string textOldVersion = "below v3.0";
  122. #elif UNITY_2019_4_OR_NEWER
  123. string textNewVersion = "v2.1 or above";
  124. string textOldVersion = "below v2.1";
  125. #else
  126. string textNewVersion = "v1.5 or above";
  127. string textOldVersion = "below v1.5 and above v1.0.54";
  128. #endif
  129. Add(new ValueWizardPageElement<string>(TMP_KEY,
  130. (o, v) =>
  131. {
  132. if (GUILayout.Button("Import Add-On (TextMesh Pro " + textNewVersion + ")"))
  133. {
  134. wizard.DoReloadOperation(this, () =>
  135. {
  136. AssetDatabase.ImportPackage(Path.Combine(Application.dataPath, PACKAGE_PATH_TMP_NEW), false);
  137. v = PACKAGE_PATH_TMP_NEW;
  138. });
  139. }
  140. if (GUILayout.Button("Import Add-On (TextMesh Pro " + textOldVersion + ")"))
  141. {
  142. wizard.DoReloadOperation(this, () =>
  143. {
  144. AssetDatabase.ImportPackage(Path.Combine(Application.dataPath, PACKAGE_PATH_TMP_OLD), false);
  145. v = PACKAGE_PATH_TMP_OLD;
  146. });
  147. }
  148. return v;
  149. }).MarkComplete());
  150. }
  151. }
  152. }
  153. private bool TryGetTextMethProPackageVersion(out int v1, out int v2, out int v3)
  154. {
  155. v1 = 0;
  156. v2 = 0;
  157. v3 = 0;
  158. string folder = Path.Combine(Path.GetDirectoryName(Application.dataPath), "Packages");
  159. if (!Directory.Exists(folder))
  160. return false;
  161. string file = Path.Combine(folder, "manifest.json");
  162. if (!File.Exists(file))
  163. return false;
  164. string json = File.ReadAllText(file);
  165. var match = Regex.Match(json, @"""com.unity.textmeshpro"":\s?""(?<v1>\d *).(?<v2>\d *).(?<v3>\d *)""");
  166. if (!match.Success)
  167. return false;
  168. try
  169. {
  170. return int.TryParse(match.Groups["v1"].Value, out v1)
  171. && int.TryParse(match.Groups["v2"].Value, out v2)
  172. && int.TryParse(match.Groups["v3"].Value, out v3);
  173. }
  174. catch
  175. {
  176. return false;
  177. }
  178. }
  179. }
  180. }