Chocolade19721309

Untitled

Jun 10th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6.  
  7. public class GameNotesEditor : EditorWindow
  8. {
  9. private string notesText = "";
  10. private bool addLineNumbers = true;
  11. private bool addEmptyLines = false;
  12. private bool saveToFile = true;
  13. private bool autoSave = true;
  14. private int currentLineNumber = 1;
  15. private string savePath;
  16. private Vector2 scrollPos;
  17.  
  18. private static readonly string saveFileName = "EditorNotes.txt";
  19. private double lastAutoSaveTime;
  20. private double autoSaveInterval = 10.0; // Auto save every 10 seconds
  21. private bool showSaveIndicator = false;
  22. private DateTime lastSaveTime;
  23.  
  24. int _selected = 0;
  25. string[] _options = new string[3] { "Item1", "Item2", "Item3" };
  26.  
  27. // Scaling factor to adjust UI size
  28. private float uiScale = 1.5f;
  29.  
  30. [MenuItem("Window/Game Notes")]
  31. public static void ShowWindow()
  32. {
  33. const int width = 800;
  34. const int height = 800;
  35.  
  36. var x = (Screen.currentResolution.width - width) / 2;
  37. var y = (Screen.currentResolution.height - height) / 2;
  38.  
  39. var window = GetWindow<GameNotesEditor>("Game Notes");
  40. window.minSize = new Vector2(width, height);
  41. window.position = new Rect(x, y, width, height);
  42. }
  43.  
  44. private void OnEnable()
  45. {
  46. savePath = Path.Combine(Application.persistentDataPath, saveFileName);
  47. LoadFromFile();
  48.  
  49. if (addLineNumbers && string.IsNullOrEmpty(notesText))
  50. {
  51. AddNewLine();
  52. }
  53.  
  54. EditorApplication.update += AutoSave;
  55. }
  56.  
  57. private void OnDisable()
  58. {
  59. EditorApplication.update -= AutoSave;
  60. }
  61.  
  62. private void OnGUI()
  63. {
  64. // Adjust the label width and field width based on the scaling factor
  65. EditorGUIUtility.labelWidth = 150 * uiScale;
  66. EditorGUIUtility.fieldWidth = 100 * uiScale;
  67.  
  68. // Begin the main vertical layout
  69. EditorGUILayout.BeginVertical(GUILayout.Width(position.width), GUILayout.Height(position.height));
  70.  
  71. // Scroll view for notes text with dynamic scaling for text size
  72. scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.ExpandHeight(true));
  73.  
  74. GUIStyle textAreaStyle = new GUIStyle(GUI.skin.textArea)
  75. {
  76. fontSize = Mathf.RoundToInt(14 * uiScale),
  77. wordWrap = true
  78. };
  79. notesText = EditorGUILayout.TextArea(notesText, textAreaStyle, GUILayout.ExpandHeight(true), GUILayout.Width(position.width - 20));
  80.  
  81. EditorGUILayout.EndScrollView();
  82.  
  83. // Space between elements
  84. EditorGUILayout.Space();
  85.  
  86. // Horizontal layout for buttons with dynamic scaling for height
  87. EditorGUILayout.BeginHorizontal();
  88. if (GUILayout.Button("Add Line", GUILayout.Height(30 * uiScale)))
  89. {
  90. AddNewLine();
  91. }
  92. if (GUILayout.Button("Save Notes", GUILayout.Height(30 * uiScale)))
  93. {
  94. SaveToFile(notesText);
  95. }
  96. EditorGUILayout.EndHorizontal();
  97.  
  98. // Space between elements
  99. EditorGUILayout.Space();
  100.  
  101. // Settings with dynamic scaling for label font size
  102. GUIStyle labelStyle = new GUIStyle(GUI.skin.label) { fontSize = Mathf.RoundToInt(14 * uiScale) };
  103. EditorGUILayout.LabelField("Settings", labelStyle);
  104.  
  105. GUIStyle customToggleStyle = new GUIStyle(GUI.skin.toggle)
  106. {
  107. fontSize = Mathf.RoundToInt(14 * uiScale)
  108. };
  109.  
  110. // Use the custom style for each Toggle
  111. addLineNumbers = EditorGUILayout.Toggle("Add Line Numbers", addLineNumbers, customToggleStyle);
  112. addEmptyLines = EditorGUILayout.Toggle("Add Empty Lines", addEmptyLines, customToggleStyle);
  113. saveToFile = EditorGUILayout.Toggle("Save to File", saveToFile, customToggleStyle);
  114. autoSave = EditorGUILayout.Toggle("Auto Save", autoSave, customToggleStyle);
  115.  
  116. // Dropdown menu
  117. GUIStyle popupStyle = new GUIStyle(EditorStyles.popup) { fontSize = Mathf.RoundToInt(14 * uiScale) };
  118. EditorGUI.BeginChangeCheck();
  119. this._selected = EditorGUILayout.Popup("My Simple Dropdown", _selected, _options, popupStyle, GUILayout.Height(30 * uiScale));
  120. if (EditorGUI.EndChangeCheck())
  121. {
  122. Debug.Log(_options[_selected]);
  123. }
  124.  
  125. // Space between elements
  126. EditorGUILayout.Space();
  127.  
  128. // Show save indicator if necessary
  129. if (showSaveIndicator)
  130. {
  131. EditorGUILayout.LabelField("Notes saved successfully!", labelStyle);
  132. if ((DateTime.Now - lastSaveTime).TotalSeconds > 2)
  133. {
  134. showSaveIndicator = false;
  135. }
  136. }
  137.  
  138. EditorGUILayout.EndVertical();
  139. }
  140.  
  141. private void AddNewLine()
  142. {
  143. if (addLineNumbers)
  144. {
  145. notesText += $"{(addEmptyLines ? "\n" : "")}{currentLineNumber}. ";
  146. currentLineNumber++;
  147. }
  148. else
  149. {
  150. notesText += addEmptyLines ? "\n" : "";
  151. }
  152. }
  153.  
  154. private void SaveToFile(string text)
  155. {
  156. if (saveToFile)
  157. {
  158. File.WriteAllText(savePath, text);
  159. showSaveIndicator = true;
  160. lastSaveTime = DateTime.Now;
  161. Repaint();
  162. }
  163. }
  164.  
  165. private void LoadFromFile()
  166. {
  167. if (File.Exists(savePath))
  168. {
  169. notesText = File.ReadAllText(savePath);
  170. UpdateLineNumber(notesText);
  171. }
  172. }
  173.  
  174. private void UpdateLineNumber(string text)
  175. {
  176. var lines = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
  177. currentLineNumber = lines.Length;
  178.  
  179. for (int i = lines.Length - 1; i >= 0; i--)
  180. {
  181. if (lines[i].StartsWith($"{currentLineNumber - 1}. "))
  182. {
  183. currentLineNumber++;
  184. break;
  185. }
  186. }
  187. }
  188.  
  189. private void AutoSave()
  190. {
  191. if (autoSave && saveToFile)
  192. {
  193. double currentTime = EditorApplication.timeSinceStartup;
  194. if (currentTime - lastAutoSaveTime >= autoSaveInterval)
  195. {
  196. SaveToFile(notesText);
  197. lastAutoSaveTime = currentTime;
  198. }
  199. }
  200. }
  201.  
  202. [InitializeOnLoadMethod]
  203. private static void OnProjectLoadedInEditor()
  204. {
  205. Selection.selectionChanged += OnSelectionChange;
  206. }
  207.  
  208. private static void OnSelectionChange()
  209. {
  210. GameObject selectedObject = Selection.activeGameObject;
  211. if (selectedObject != null)
  212. {
  213. GameNotesEditor window = GetWindow<GameNotesEditor>();
  214. window.notesText += $"\nSelected Object: {selectedObject.name}\n";
  215. window.DisplayComponentDetails(selectedObject);
  216. }
  217. }
  218.  
  219. private void DisplayComponentDetails(GameObject selectedObject)
  220. {
  221. Component[] components = selectedObject.GetComponents<Component>();
  222. notesText += "Components:\n";
  223. foreach (var component in components)
  224. {
  225. notesText += $"- {component.GetType().Name}\n";
  226. }
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment