ReadmeEditor.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. using UnityEngine.UIElements;
  5. [CustomEditor(typeof(Readme))]
  6. [InitializeOnLoad]
  7. sealed class ReadmeEditor : Editor
  8. {
  9. const string k_ShowedReadmeSessionStateName = "ReadmeEditor.showedReadme";
  10. const string k_ReadmeSourceDirectory = "Assets/TutorialInfo";
  11. static ReadmeEditor()
  12. => EditorApplication.delayCall += SelectReadmeAutomatically;
  13. static void SelectReadmeAutomatically()
  14. {
  15. if (!SessionState.GetBool(k_ShowedReadmeSessionStateName, false))
  16. {
  17. var readme = SelectReadme();
  18. SessionState.SetBool(k_ShowedReadmeSessionStateName, true);
  19. if (readme && !readme.loadedLayout)
  20. {
  21. EditorUtility.LoadWindowLayout(Path.Combine(Application.dataPath, "TutorialInfo/Layout.wlt"));
  22. readme.loadedLayout = true;
  23. }
  24. }
  25. }
  26. static Readme SelectReadme()
  27. {
  28. var ids = AssetDatabase.FindAssets("Readme t:Readme");
  29. if (ids.Length != 1)
  30. {
  31. Debug.Log("Couldn't find a readme");
  32. return null;
  33. }
  34. var readmeObject = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(ids[0]));
  35. Selection.objects = new UnityEngine.Object[] { readmeObject };
  36. return (Readme)readmeObject;
  37. }
  38. void RemoveTutorial()
  39. {
  40. if (EditorUtility.DisplayDialog("Remove Readme Assets",
  41. $"All contents under {k_ReadmeSourceDirectory} will be removed, are you sure you want to proceed?",
  42. "Proceed",
  43. "Cancel"))
  44. {
  45. if (Directory.Exists(k_ReadmeSourceDirectory))
  46. {
  47. FileUtil.DeleteFileOrDirectory(k_ReadmeSourceDirectory);
  48. FileUtil.DeleteFileOrDirectory(k_ReadmeSourceDirectory + ".meta");
  49. }
  50. else
  51. {
  52. Debug.Log($"Could not find the Readme folder at {k_ReadmeSourceDirectory}");
  53. }
  54. var readmeAsset = SelectReadme();
  55. if (readmeAsset != null)
  56. {
  57. var path = AssetDatabase.GetAssetPath(readmeAsset);
  58. FileUtil.DeleteFileOrDirectory(path + ".meta");
  59. FileUtil.DeleteFileOrDirectory(path);
  60. }
  61. AssetDatabase.Refresh();
  62. }
  63. }
  64. //Remove ImGUI
  65. protected sealed override void OnHeaderGUI() { }
  66. public sealed override void OnInspectorGUI() { }
  67. public override VisualElement CreateInspectorGUI()
  68. {
  69. var readme = (Readme)target;
  70. VisualElement root = new();
  71. root.styleSheets.Add(readme.commonStyle);
  72. root.styleSheets.Add(EditorGUIUtility.isProSkin ? readme.darkStyle : readme.lightStyle);
  73. VisualElement ChainWithClass(VisualElement created, string className)
  74. {
  75. created.AddToClassList(className);
  76. return created;
  77. }
  78. //Header
  79. VisualElement title = new();
  80. title.AddToClassList("title");
  81. title.Add(ChainWithClass(new Image() { image = readme.icon }, "title__icon"));
  82. title.Add(ChainWithClass(new Label(readme.title), "title__text"));
  83. root.Add(title);
  84. //Content
  85. foreach (var section in readme.sections)
  86. {
  87. VisualElement part = new();
  88. part.AddToClassList("section");
  89. if (!string.IsNullOrEmpty(section.heading))
  90. part.Add(ChainWithClass(new Label(section.heading), "section__header"));
  91. if (!string.IsNullOrEmpty(section.text))
  92. part.Add(ChainWithClass(new Label(section.text), "section__body"));
  93. if (!string.IsNullOrEmpty(section.linkText))
  94. {
  95. var link = ChainWithClass(new Label(section.linkText), "section__link");
  96. link.RegisterCallback<ClickEvent>(evt => Application.OpenURL(section.url));
  97. part.Add(link);
  98. }
  99. root.Add(part);
  100. }
  101. var button = new Button(RemoveTutorial) { text = "Remove Readme Assets" };
  102. button.AddToClassList("remove-readme-button");
  103. root.Add(button);
  104. return root;
  105. }
  106. }