ExampleScenesPage.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 ExampleScenesPage : WizardPage
  12. {
  13. const string OVERVIEW_PACK = "TheraBytes/BetterUI/packages/Example_Overiew.unitypackage";
  14. InstallPackageSelectionWizardPageElement customScreenTagExample;
  15. public override string NameId { get { return "ExampleScenesPage"; } }
  16. protected override string NextButtonText
  17. {
  18. get
  19. {
  20. var packages = base.elements.OfType<InstallPackageSelectionWizardPageElement>();
  21. bool installAny = packages.Any(o => o.SelectionState == InstallSelectionState.Install);
  22. bool removeAny = packages.Any(o => o.SelectionState == InstallSelectionState.Remove);
  23. if(installAny && removeAny)
  24. {
  25. return "Install / Remove selected Example Scenes";
  26. }
  27. else if(installAny)
  28. {
  29. return "Install selected Example Scenes";
  30. }
  31. else if(removeAny)
  32. {
  33. return "Remove selected Example Scenes";
  34. }
  35. return base.NextButtonText;
  36. }
  37. }
  38. public ExampleScenesPage(IWizard wizard)
  39. : base(wizard)
  40. {
  41. }
  42. protected override void OnInitialize()
  43. {
  44. Add(new InfoWizardPageElement("Example Scenes", InfoType.Header));
  45. Add(new InfoWizardPageElement("This page allows you to install example scenes.", InfoType.InfoBox));
  46. // Overview
  47. Add(new SeparatorWizardPageElement());
  48. Add(CreateSelection("Overview"));
  49. Add(new InfoWizardPageElement("This is the oldest example scene of Better UI. It shows the most releant features and is devided into categories: " +
  50. "Transitions, Better Image, Better Text and Layout"));
  51. if(IsFallbackLandscape())
  52. {
  53. Add(new InfoWizardPageElement("This Example scene was created for Portrait resolutions as fallback. " +
  54. "Your fallback is Landscape, so the example may look broken.", InfoType.ErrorBox));
  55. }
  56. // Ghost
  57. Add(new SeparatorWizardPageElement());
  58. Add(CreateSelection("Ghost"));
  59. Add(new InfoWizardPageElement("This example shows some animation features as well as a movable mask where the masked sprite in the hierarchy below stays at a certain position. " +
  60. "This is achieved using Anchor Overrides."));
  61. // ResolutionTracking
  62. Add(new SeparatorWizardPageElement());
  63. Add(CreateSelection("ResolutionTracking"));
  64. Add(new InfoWithButtonWizardPageElement("This example shows realtime detection of screen sizes and portrait / landscape detection. " +
  65. "You may preview this example in your browser.",
  66. "Preview", () => Application.OpenURL("https://documentation.therabytes.de/better-ui-webgl-dpi-test/container.html")));
  67. if (IsFallbackPortrait())
  68. {
  69. Add(new InfoWizardPageElement("This Example scene was created for Landscape resolutions as fallback. " +
  70. "Your fallback is Portrait, so the example may not work as intended.", InfoType.ErrorBox));
  71. }
  72. else if(!ResolutionMonitor.Instance.OptimizedScreens.Any(o => o.Name == "Portrait"))
  73. {
  74. Add(new InfoWizardPageElement("For this example to work properly, it is required to have a screen configuration named 'Portrait'.", InfoType.WarningBox));
  75. }
  76. // AnchorAnimations
  77. Add(new SeparatorWizardPageElement());
  78. Add(CreateSelection("AnchorAnimations"));
  79. Add(new InfoWizardPageElement("This example shows how you can use AnchorOverrides to create animated UIs. " +
  80. "The example contains a visually resorting leaderboard as well as an object which can transform into other objects per click."));
  81. if (IsFallbackLandscape())
  82. {
  83. Add(new InfoWizardPageElement("This Example scene was created for Portrait resolutions as fallback. " +
  84. "Your fallback is Landscape, so the example may look broken.", InfoType.WarningBox));
  85. }
  86. // CustomScreenTag
  87. Add(new SeparatorWizardPageElement());
  88. customScreenTagExample = CreateSelection("CustomScreenTag");
  89. Add(customScreenTagExample);
  90. Add(new InfoWizardPageElement("You will find just a long text and a toggle in this exampke. If you hit the toggle everything becomes big. " +
  91. "This is achieved through custom screen tags in screen configurations."));
  92. Add(new InfoWizardPageElement("If you install this example, another screen configuration 'Accessibility Mode' is added to the Resolution Monitor. " +
  93. "Note that it will not be removed again if you remove this example. You may want to remove it manually later.", InfoType.WarningBox));
  94. }
  95. protected override void NextButtonClicked()
  96. {
  97. var items = elements.OfType<InstallPackageSelectionWizardPageElement>();
  98. if (items.All(o => o.SelectionState == InstallSelectionState.None))
  99. {
  100. base.NextButtonClicked();
  101. return;
  102. }
  103. wizard.DoReloadOperation(this, () =>
  104. {
  105. // add accessibility mode if custom screen tag example is installed
  106. if (customScreenTagExample.SelectionState == InstallSelectionState.Install
  107. && !ResolutionMonitor.Instance.OptimizedScreens.Any(x => x.Name == "Accessibility Mode"))
  108. {
  109. var screenCondition = new ScreenTypeConditions("Accessibility Mode");
  110. screenCondition.CheckScreenTag.IsActive = true;
  111. screenCondition.CheckScreenTag.ScreenTag = "Accessibility_Mode";
  112. ResolutionMonitor.Instance.OptimizedScreens.Add(screenCondition);
  113. EditorUtility.SetDirty(ResolutionMonitor.Instance);
  114. AssetDatabase.SaveAssets();
  115. }
  116. foreach (var itm in items)
  117. {
  118. switch (itm.SelectionState)
  119. {
  120. case InstallSelectionState.Install:
  121. try
  122. {
  123. AssetDatabase.ImportPackage(itm.PathToPackage, false);
  124. }
  125. catch
  126. {
  127. Debug.LogWarningFormat("Could not import package '{0}'", itm.PathToPackage);
  128. }
  129. break;
  130. case InstallSelectionState.Remove:
  131. try
  132. {
  133. File.Delete(itm.PathToFolder + ".meta");
  134. Directory.Delete(itm.PathToFolder, true);
  135. }
  136. catch
  137. {
  138. Debug.LogWarningFormat("Could not properly delete '{0}'", itm.PathToFolder);
  139. }
  140. break;
  141. }
  142. }
  143. AssetDatabase.Refresh();
  144. });
  145. }
  146. InstallPackageSelectionWizardPageElement CreateSelection(string exampleName)
  147. {
  148. string package = Path.Combine(Application.dataPath, string.Format("TheraBytes/BetterUI/packages/Example_{0}.unitypackage", exampleName));
  149. string folder = Path.Combine(Application.dataPath, string.Format("TheraBytes/BetterUI/Example/{0}", exampleName));
  150. var result = new InstallPackageSelectionWizardPageElement(exampleName, package, folder);
  151. result.MarkComplete();
  152. return result;
  153. }
  154. bool IsFallbackLandscape()
  155. {
  156. return !IsFallbackPortrait();
  157. }
  158. bool IsFallbackPortrait()
  159. {
  160. var fallback = ResolutionMonitor.OptimizedResolutionFallback;
  161. return fallback.x < fallback.y;
  162. }
  163. }
  164. }