ReadmeEditor.cs 4.0 KB

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