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