|
| 1 | +// Copyright (c) Unity Technologies. |
| 2 | +// Based on the "URP TutorialInfo ReadmeEditor.cs" script included in URP template projects. |
| 3 | +// |
| 4 | +// Original asset by Unity Technologies. This version has been modified to display |
| 5 | +// a custom Readme.asset in the Inspector about how to use the Singletons package |
| 6 | +// |
| 7 | +// Modified by github.com/ThatAmuzak - 2025 |
| 8 | + |
| 9 | +using UnityEngine; |
| 10 | +using UnityEditor; |
| 11 | +using System.IO; |
| 12 | + |
| 13 | +[CustomEditor(typeof(Readme))] |
| 14 | +public class ReadmeEditor : Editor |
| 15 | +{ |
| 16 | + static string s_ReadmeSourceDirectory = "Assets/Samples/Unity Singleton/3.0.0/Samples/SamplesGuideInfo"; |
| 17 | + |
| 18 | + const float k_Space = 8f; |
| 19 | + |
| 20 | + static void RemoveTutorial() |
| 21 | + { |
| 22 | + if (EditorUtility.DisplayDialog("Remove Readme Assets", |
| 23 | + |
| 24 | + $"All contents under {s_ReadmeSourceDirectory} will be removed, are you sure you want to proceed?", |
| 25 | + "Proceed", |
| 26 | + "Cancel")) |
| 27 | + { |
| 28 | + if (Directory.Exists(s_ReadmeSourceDirectory)) |
| 29 | + { |
| 30 | + FileUtil.DeleteFileOrDirectory(s_ReadmeSourceDirectory); |
| 31 | + FileUtil.DeleteFileOrDirectory(s_ReadmeSourceDirectory + ".meta"); |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + Debug.Log($"Could not find the Readme folder at {s_ReadmeSourceDirectory}"); |
| 36 | + } |
| 37 | + |
| 38 | + var readmeAsset = SelectReadme(); |
| 39 | + if (readmeAsset != null) |
| 40 | + { |
| 41 | + var path = AssetDatabase.GetAssetPath(readmeAsset); |
| 42 | + FileUtil.DeleteFileOrDirectory(path + ".meta"); |
| 43 | + FileUtil.DeleteFileOrDirectory(path); |
| 44 | + } |
| 45 | + |
| 46 | + AssetDatabase.Refresh(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + static Readme SelectReadme() |
| 51 | + { |
| 52 | + var ids = AssetDatabase.FindAssets("Readme t:Readme"); |
| 53 | + if (ids.Length == 1) |
| 54 | + { |
| 55 | + var readmeObject = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(ids[0])); |
| 56 | + |
| 57 | + Selection.objects = new[] { readmeObject }; |
| 58 | + |
| 59 | + return (Readme)readmeObject; |
| 60 | + } |
| 61 | + |
| 62 | + Debug.Log("Couldn't find a readme"); |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + protected override void OnHeaderGUI() |
| 67 | + { |
| 68 | + var readme = (Readme)target; |
| 69 | + Init(); |
| 70 | + |
| 71 | + GUILayout.BeginHorizontal("In BigTitle"); |
| 72 | + { |
| 73 | + GUILayout.Space(k_Space); |
| 74 | + GUILayout.BeginVertical(); |
| 75 | + { |
| 76 | + |
| 77 | + GUILayout.FlexibleSpace(); |
| 78 | + GUILayout.Label(readme.title, TitleStyle); |
| 79 | + GUILayout.FlexibleSpace(); |
| 80 | + } |
| 81 | + GUILayout.EndVertical(); |
| 82 | + GUILayout.FlexibleSpace(); |
| 83 | + } |
| 84 | + GUILayout.EndHorizontal(); |
| 85 | + } |
| 86 | + |
| 87 | + public override void OnInspectorGUI() |
| 88 | + { |
| 89 | + var readme = (Readme)target; |
| 90 | + Init(); |
| 91 | + Rect rect; |
| 92 | + foreach (var section in readme.sections) |
| 93 | + { |
| 94 | + GUILayout.Space(k_Space); |
| 95 | + rect = EditorGUILayout.GetControlRect(false, 2 ); |
| 96 | + rect.height = 2; |
| 97 | + EditorGUI.DrawRect(rect, new Color ( 0.5f,0.5f,0.5f, 1 ) ); |
| 98 | + GUILayout.Space(k_Space); |
| 99 | + |
| 100 | + if (!string.IsNullOrEmpty(section.heading)) |
| 101 | + { |
| 102 | + GUILayout.Label(section.heading, HeadingStyle); |
| 103 | + } |
| 104 | + |
| 105 | + foreach (var subtext in section.text) |
| 106 | + { |
| 107 | + if (!string.IsNullOrEmpty(subtext)) |
| 108 | + { |
| 109 | + GUILayout.Label(subtext, BodyStyle); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + GUILayout.Space(k_Space); |
| 114 | + rect = EditorGUILayout.GetControlRect(false, 2 ); |
| 115 | + rect.height = 2; |
| 116 | + GUILayout.Space(k_Space); |
| 117 | + if (GUILayout.Button("Remove Readme Assets", ButtonStyle)) |
| 118 | + { |
| 119 | + RemoveTutorial(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + bool m_Initialized; |
| 124 | + |
| 125 | + GUIStyle TitleStyle => m_TitleStyle; |
| 126 | + |
| 127 | + [SerializeField] |
| 128 | + GUIStyle m_TitleStyle; |
| 129 | + |
| 130 | + GUIStyle HeadingStyle => m_HeadingStyle; |
| 131 | + |
| 132 | + [SerializeField] |
| 133 | + GUIStyle m_HeadingStyle; |
| 134 | + |
| 135 | + GUIStyle BodyStyle => m_BodyStyle; |
| 136 | + |
| 137 | + [SerializeField] |
| 138 | + GUIStyle m_BodyStyle; |
| 139 | + |
| 140 | + GUIStyle ButtonStyle => m_ButtonStyle; |
| 141 | + |
| 142 | + [SerializeField] |
| 143 | + GUIStyle m_ButtonStyle; |
| 144 | + |
| 145 | + void Init() |
| 146 | + { |
| 147 | + if (m_Initialized) |
| 148 | + return; |
| 149 | + m_BodyStyle = new GUIStyle(EditorStyles.label); |
| 150 | + m_BodyStyle.wordWrap = true; |
| 151 | + m_BodyStyle.fontSize = 14; |
| 152 | + m_BodyStyle.richText = true; |
| 153 | + |
| 154 | + m_TitleStyle = new GUIStyle(m_BodyStyle); |
| 155 | + m_TitleStyle.fontSize = 26; |
| 156 | + |
| 157 | + m_HeadingStyle = new GUIStyle(m_BodyStyle); |
| 158 | + m_HeadingStyle.fontStyle = FontStyle.Bold; |
| 159 | + m_HeadingStyle.fontSize = 18; |
| 160 | + |
| 161 | + m_ButtonStyle = new GUIStyle(EditorStyles.miniButton); |
| 162 | + m_ButtonStyle.fontStyle = FontStyle.Bold; |
| 163 | + |
| 164 | + m_Initialized = true; |
| 165 | + } |
| 166 | +} |
0 commit comments