SetupWizard.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using TheraBytes.BetterUi;
  7. using UnityEditor;
  8. using UnityEngine;
  9. namespace TheraBytes.BetterUi.Editor
  10. {
  11. [InitializeOnLoad]
  12. public class SetupWizard : EditorWindow, IWizard
  13. {
  14. // static >
  15. const string PageToLoadKey = "page_to_load_key";
  16. static PersistentWizardData spwd; // "static persistent wizard data" - don't access directly.
  17. static PersistentWizardData StaticPersistentWizardData
  18. {
  19. get
  20. {
  21. if(spwd == null)
  22. {
  23. string filePath = string.Format("{0}/TheraBytes/BetterUI/Editor/setup~.wizard", Application.dataPath);
  24. spwd = new PersistentWizardData(filePath);
  25. }
  26. return spwd;
  27. }
  28. }
  29. [MenuItem("Tools/Better UI/Settings/Setup Wizard", false, -100)]
  30. public static void ShowWindow()
  31. {
  32. EditorApplication.delayCall -= ShowWindow;
  33. var wnd = EditorWindow.GetWindow(typeof(SetupWizard), true, "Better UI - Setup Wizard");
  34. wnd.minSize = new Vector2(524, 460);
  35. wnd.maxSize = new Vector2(524, 2000);
  36. }
  37. [InitializeOnLoadMethod]
  38. static void Init()
  39. {
  40. if (StaticPersistentWizardData.FileExists())
  41. return;
  42. EditorApplication.delayCall += ShowWindow;
  43. }
  44. // member >
  45. public int CurrentPageNumber { get { return TotalPageCount - pages.Count; } }
  46. public int TotalPageCount { get; private set; }
  47. Queue<WizardPage> pages;
  48. public PersistentWizardData PersistentData { get { return StaticPersistentWizardData; } }
  49. void OnEnable()
  50. {
  51. pages = new Queue<WizardPage>();
  52. pages.Enqueue(new WelcomePage(this));
  53. pages.Enqueue(new ResolutionMonitorPage(this));
  54. pages.Enqueue(new ThirdPartySupportPage(this));
  55. #if UNITY_2017_3_OR_NEWER
  56. // Assembly definitions were introduced in Unity 2017.3
  57. pages.Enqueue(new AssemblyDefinitionsPage(this));
  58. #endif
  59. pages.Enqueue(new ExampleScenesPage(this));
  60. pages.Enqueue(new ToolsPage(this));
  61. pages.Enqueue(new FinalPage(this));
  62. TotalPageCount = pages.Count;
  63. string pageToLoad;
  64. if(PersistentData.TryGetValue(PageToLoadKey, out pageToLoad))
  65. {
  66. while(pages.Count > 0 && pages.Peek().NameId != pageToLoad)
  67. {
  68. pages.Dequeue();
  69. }
  70. PersistentData.RemoveEntry(PageToLoadKey);
  71. PersistentData.Save();
  72. }
  73. pages.Peek().Initialize();
  74. Focus();
  75. }
  76. void OnGUI()
  77. {
  78. pages.Peek().DrawGui();
  79. }
  80. public void PageFinished(WizardPage page)
  81. {
  82. if (pages.Count > 1)
  83. {
  84. pages.Dequeue();
  85. pages.Peek().Initialize();
  86. }
  87. else
  88. {
  89. Close();
  90. }
  91. }
  92. public void JumpToPage<T>()
  93. where T : WizardPage
  94. {
  95. while (pages.Count > 0 && pages.Peek().GetType() != typeof(T))
  96. {
  97. pages.Dequeue();
  98. }
  99. Debug.Assert(pages.Count > 0);
  100. pages.Peek().Initialize();
  101. }
  102. public void DoReloadOperation(WizardPage page, Action operation)
  103. {
  104. if(operation != null)
  105. {
  106. PersistentData.RegisterValue(PageToLoadKey, page.NameId);
  107. PersistentData.Save();
  108. pages.Clear();
  109. pages.Enqueue(new RefreshingPage(this));
  110. pages.Peek().Initialize();
  111. operation();
  112. Focus();
  113. }
  114. }
  115. }
  116. }