TriLibVersionNotes.cs 5.3 KB

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