Tanoda
TriLibVersionNotes.cs
Go to the documentation of this file.
1using System.IO;
2using UnityEditor;
3using UnityEngine;
4
5namespace TriLibCore.Editor
6{
7 public class TriLibVersionNotes : EditorWindow
8 {
9 private class Styles
10 {
11 public const int WindowWidth = 720;
12 public const int WindowHeight = 500;
13 public static readonly GUIStyle HeaderStyle = new GUIStyle("label") { fontSize = 19, fontStyle = FontStyle.Bold, margin = new RectOffset(10, 10, 5, 5) };
14 public static readonly GUIStyle SubHeaderStyle = new GUIStyle("label") { margin = new RectOffset(10, 10, 5, 5), fontStyle = FontStyle.Bold };
15 public static readonly GUIStyle TextStyle = new GUIStyle("label") { margin = new RectOffset(20, 20, 5, 5) };
16 public static readonly GUIStyle TextAreaStyle = new GUIStyle(TextStyle) { wordWrap = true };
17 public static readonly GUIStyle ButtonStyle = new GUIStyle("button") { margin = new RectOffset(10, 10, 5, 5) };
18 }
19
20 private const string SkipVersionInfoKey = "TriLibSkipVersionInfo";
21
22 private string _text;
23 private bool _loaded;
24 private Vector2 _changeLogScrollPosition;
25 private Vector2 _notesScrollPosition;
26
27 private static TriLibVersionNotes Instance
28 {
29 get
30 {
31 var window = GetWindow<TriLibVersionNotes>();
32 window.titleContent = new GUIContent("TriLib Version Notes");
33 window.minSize = new Vector2(Styles.WindowWidth, Styles.WindowHeight);
34 return window;
35 }
36 }
37
38 [InitializeOnLoadMethod]
39 public static void Initialize()
40 {
41 if (!EditorPrefs.GetBool(SkipVersionInfoKey))
42 {
43 ShowWindow();
44 }
45 }
46
47 public static void ShowWindow()
48 {
49 Instance.Show();
50 }
51
52 private void OnDestroy()
53 {
54 EditorPrefs.SetBool(SkipVersionInfoKey, true);
55 }
56
57 private void OnGUI()
58 {
59 if (!_loaded)
60 {
61 var guids = AssetDatabase.FindAssets("TriLibReleaseNotes");
62 if (guids.Length > 0)
63 {
64 var guid = guids[0];
65 var assetPath = AssetDatabase.GUIDToAssetPath(guid);
66 var textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(assetPath);
67 if (textAsset == null || textAsset.text == null)
68 {
69 AssetDatabase.Refresh();
70 textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(assetPath);
71 if (textAsset == null)
72 {
73 Close();
74 }
75 return;
76 }
77 _text = textAsset.text.Replace("\\n", "\n");
78 }
79 else
80 {
81 Close();
82 }
83 _loaded = true;
84 }
85 EditorGUILayout.BeginVertical();
86 using (var stringReader = new StringReader(_text))
87 {
88 var changeLogOpen = false;
89 var version = stringReader.ReadLine();
90 GUILayout.Label($"TriLib {version}", Styles.HeaderStyle);
91 for (; ; )
92 {
93 var line = stringReader.ReadLine();
94 if (line == null)
95 {
96 break;
97 }
98 if (line.ToLowerInvariant() == "changelog:")
99 {
100 EditorGUILayout.Space();
101 GUILayout.Label("Changelog", Styles.SubHeaderStyle);
102 _changeLogScrollPosition = GUILayout.BeginScrollView(_changeLogScrollPosition, GUILayout.Height(260f));
103 changeLogOpen = true;
104 }
105 else if (line.ToLowerInvariant() == "version notes:")
106 {
107 if (changeLogOpen)
108 {
109 GUILayout.EndScrollView();
110 changeLogOpen = false;
111 }
112 EditorGUILayout.Space();
113 GUILayout.Label("Version Notes", Styles.SubHeaderStyle);
114 var versionInfo = stringReader.ReadToEnd();
115 _notesScrollPosition = EditorGUILayout.BeginScrollView(_notesScrollPosition);
116 EditorGUILayout.TextArea(versionInfo, Styles.TextAreaStyle);
117 EditorGUILayout.EndScrollView();
118 break;
119 }
120 else
121 {
122 GUILayout.Label(line, Styles.TextStyle);
123 }
124 }
125 if (changeLogOpen)
126 {
127 GUILayout.EndScrollView();
128 }
129 EditorGUILayout.EndVertical();
130 EditorGUILayout.Space();
131 GUILayout.Label("You can show this window on the Project Settings/TriLib area", Styles.SubHeaderStyle);
132 EditorGUILayout.BeginHorizontal();
133 GUILayout.FlexibleSpace();
134 if (GUILayout.Button("Close", Styles.ButtonStyle))
135 {
136 Close();
137 }
138 EditorGUILayout.EndHorizontal();
139 }
140 }
141 }
142}