ReadmeEditor.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. using System.IO;
  7. using System.Reflection;
  8. [CustomEditor(typeof(Readme))]
  9. [InitializeOnLoad]
  10. public class ReadmeEditor : Editor
  11. {
  12. static string kShowedReadmeSessionStateName = "ReadmeEditor.showedReadme";
  13. static float kSpace = 16f;
  14. static ReadmeEditor()
  15. {
  16. EditorApplication.delayCall += SelectReadmeAutomatically;
  17. }
  18. static void SelectReadmeAutomatically()
  19. {
  20. if (!SessionState.GetBool(kShowedReadmeSessionStateName, false))
  21. {
  22. var readme = SelectReadme();
  23. SessionState.SetBool(kShowedReadmeSessionStateName, true);
  24. if (readme && !readme.loadedLayout)
  25. {
  26. LoadLayout();
  27. readme.loadedLayout = true;
  28. }
  29. }
  30. }
  31. static void LoadLayout()
  32. {
  33. var assembly = typeof(EditorApplication).Assembly;
  34. var windowLayoutType = assembly.GetType("UnityEditor.WindowLayout", true);
  35. var method = windowLayoutType.GetMethod("LoadWindowLayout", BindingFlags.Public | BindingFlags.Static);
  36. method.Invoke(null, new object[] {Path.Combine(Application.dataPath, "TutorialInfo/Layout.wlt"), false});
  37. }
  38. [MenuItem("Tutorial/Show Tutorial Instructions")]
  39. static Readme SelectReadme()
  40. {
  41. var ids = AssetDatabase.FindAssets("Readme t:Readme");
  42. if (ids.Length == 1)
  43. {
  44. var readmeObject = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(ids[0]));
  45. Selection.objects = new UnityEngine.Object[] {readmeObject};
  46. return (Readme)readmeObject;
  47. }
  48. else
  49. {
  50. Debug.Log("Couldn't find a readme");
  51. return null;
  52. }
  53. }
  54. protected override void OnHeaderGUI()
  55. {
  56. var readme = (Readme)target;
  57. Init();
  58. var iconWidth = Mathf.Min(EditorGUIUtility.currentViewWidth / 3f - 20f, 128f);
  59. GUILayout.BeginHorizontal("In BigTitle");
  60. {
  61. GUILayout.Label(readme.icon, GUILayout.Width(iconWidth), GUILayout.Height(iconWidth));
  62. GUILayout.Label(readme.title, TitleStyle);
  63. }
  64. GUILayout.EndHorizontal();
  65. }
  66. public override void OnInspectorGUI()
  67. {
  68. var readme = (Readme)target;
  69. Init();
  70. foreach (var section in readme.sections)
  71. {
  72. if (!string.IsNullOrEmpty(section.heading))
  73. {
  74. GUILayout.Label(section.heading, HeadingStyle);
  75. }
  76. if (!string.IsNullOrEmpty(section.text))
  77. {
  78. GUILayout.Label(section.text, BodyStyle);
  79. }
  80. if (!string.IsNullOrEmpty(section.linkText))
  81. {
  82. if (LinkLabel(new GUIContent(section.linkText)))
  83. {
  84. Application.OpenURL(section.url);
  85. }
  86. }
  87. GUILayout.Space(kSpace);
  88. }
  89. }
  90. bool m_Initialized;
  91. GUIStyle LinkStyle { get { return m_LinkStyle; } }
  92. [SerializeField] GUIStyle m_LinkStyle;
  93. GUIStyle TitleStyle { get { return m_TitleStyle; } }
  94. [SerializeField] GUIStyle m_TitleStyle;
  95. GUIStyle HeadingStyle { get { return m_HeadingStyle; } }
  96. [SerializeField] GUIStyle m_HeadingStyle;
  97. GUIStyle BodyStyle { get { return m_BodyStyle; } }
  98. [SerializeField] GUIStyle m_BodyStyle;
  99. void Init()
  100. {
  101. if (m_Initialized)
  102. return;
  103. m_BodyStyle = new GUIStyle(EditorStyles.label);
  104. m_BodyStyle.wordWrap = true;
  105. m_BodyStyle.fontSize = 14;
  106. m_TitleStyle = new GUIStyle(m_BodyStyle);
  107. m_TitleStyle.fontSize = 26;
  108. m_HeadingStyle = new GUIStyle(m_BodyStyle);
  109. m_HeadingStyle.fontSize = 18;
  110. m_LinkStyle = new GUIStyle(m_BodyStyle);
  111. m_LinkStyle.wordWrap = false;
  112. // Match selection color which works nicely for both light and dark skins
  113. m_LinkStyle.normal.textColor = new Color(0x00 / 255f, 0x78 / 255f, 0xDA / 255f, 1f);
  114. m_LinkStyle.stretchWidth = false;
  115. m_Initialized = true;
  116. }
  117. bool LinkLabel(GUIContent label, params GUILayoutOption[] options)
  118. {
  119. var position = GUILayoutUtility.GetRect(label, LinkStyle, options);
  120. Handles.BeginGUI();
  121. Handles.color = LinkStyle.normal.textColor;
  122. Handles.DrawLine(new Vector3(position.xMin, position.yMax), new Vector3(position.xMax, position.yMax));
  123. Handles.color = Color.white;
  124. Handles.EndGUI();
  125. EditorGUIUtility.AddCursorRect(position, MouseCursor.Link);
  126. return GUI.Button(position, label, LinkStyle);
  127. }
  128. }