Chocolade19721309

Untitled

Jun 10th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 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. [MenuItem("Window/Game Notes")]
  28. public static void ShowWindow()
  29. {
  30. const int width = 800;
  31. const int height = 800;
  32.  
  33. var x = (Screen.currentResolution.width - width) / 2;
  34. var y = (Screen.currentResolution.height - height) / 2;
  35.  
  36. GetWindow<GameNotesEditor>("Game Notes").position = new Rect(x, y, width, height);
  37. }
  38.  
  39. private void OnEnable()
  40. {
  41. savePath = Path.Combine(Application.persistentDataPath, saveFileName);
  42. LoadFromFile();
  43.  
  44. if (addLineNumbers && string.IsNullOrEmpty(notesText))
  45. {
  46. AddNewLine();
  47. }
  48.  
  49. EditorApplication.update += AutoSave;
  50. }
  51.  
  52. private void OnDisable()
  53. {
  54. EditorApplication.update -= AutoSave;
  55. }
  56.  
  57. private void OnGUI()
  58. {
  59. EditorGUILayout.BeginVertical();
  60. scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.ExpandHeight(true));
  61.  
  62. notesText = EditorGUILayout.TextArea(notesText, GUILayout.ExpandHeight(true));
  63.  
  64. EditorGUILayout.EndScrollView();
  65.  
  66. EditorGUILayout.Space();
  67.  
  68. EditorGUILayout.BeginHorizontal();
  69. if (GUILayout.Button("Add Line"))
  70. {
  71. AddNewLine();
  72. }
  73. if (GUILayout.Button("Save Notes"))
  74. {
  75. SaveToFile(notesText);
  76. }
  77. EditorGUILayout.EndHorizontal();
  78.  
  79. EditorGUILayout.Space();
  80.  
  81. EditorGUILayout.LabelField("Settings", EditorStyles.boldLabel);
  82. addLineNumbers = EditorGUILayout.Toggle("Add Line Numbers", addLineNumbers);
  83. addEmptyLines = EditorGUILayout.Toggle("Add Empty Lines", addEmptyLines);
  84. saveToFile = EditorGUILayout.Toggle("Save to File", saveToFile);
  85. autoSave = EditorGUILayout.Toggle("Auto Save", autoSave);
  86.  
  87. EditorGUI.BeginChangeCheck();
  88.  
  89. this._selected = EditorGUILayout.Popup("My Simple Dropdown", _selected, _options);
  90.  
  91. if (EditorGUI.EndChangeCheck())
  92. {
  93. Debug.Log(_options[_selected]);
  94. }
  95.  
  96. EditorGUILayout.Space();
  97.  
  98. if (showSaveIndicator)
  99. {
  100. EditorGUILayout.LabelField("Notes saved successfully!", EditorStyles.boldLabel);
  101. if ((DateTime.Now - lastSaveTime).TotalSeconds > 2)
  102. {
  103. showSaveIndicator = false;
  104. }
  105. }
  106.  
  107. EditorGUILayout.EndVertical();
  108. }
  109.  
  110. private void AddNewLine()
  111. {
  112. if (addLineNumbers)
  113. {
  114. notesText += $"{(addEmptyLines ? "\n" : "")}{currentLineNumber}. ";
  115. currentLineNumber++;
  116. }
  117. else
  118. {
  119. notesText += addEmptyLines ? "\n" : "";
  120. }
  121. }
  122.  
  123. private void SaveToFile(string text)
  124. {
  125. if (saveToFile)
  126. {
  127. File.WriteAllText(savePath, text);
  128. showSaveIndicator = true;
  129. lastSaveTime = DateTime.Now;
  130. Repaint();
  131. }
  132. }
  133.  
  134. private void LoadFromFile()
  135. {
  136. if (File.Exists(savePath))
  137. {
  138. notesText = File.ReadAllText(savePath);
  139. UpdateLineNumber(notesText);
  140. }
  141. }
  142.  
  143. private void UpdateLineNumber(string text)
  144. {
  145. var lines = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
  146. currentLineNumber = lines.Length;
  147.  
  148. for (int i = lines.Length - 1; i >= 0; i--)
  149. {
  150. if (lines[i].StartsWith($"{currentLineNumber - 1}. "))
  151. {
  152. currentLineNumber++;
  153. break;
  154. }
  155. }
  156. }
  157.  
  158. private void AutoSave()
  159. {
  160. if (autoSave && saveToFile)
  161. {
  162. double currentTime = EditorApplication.timeSinceStartup;
  163. if (currentTime - lastAutoSaveTime >= autoSaveInterval)
  164. {
  165. SaveToFile(notesText);
  166. lastAutoSaveTime = currentTime;
  167. }
  168. }
  169. }
  170.  
  171. [InitializeOnLoadMethod]
  172. private static void OnProjectLoadedInEditor()
  173. {
  174. Selection.selectionChanged += OnSelectionChange;
  175. }
  176.  
  177. private static void OnSelectionChange()
  178. {
  179. GameObject selectedObject = Selection.activeGameObject;
  180. if (selectedObject != null)
  181. {
  182. GameNotesEditor window = GetWindow<GameNotesEditor>();
  183. window.notesText += $"\nSelected Object: {selectedObject.name}\n";
  184. window.DisplayComponentDetails(selectedObject);
  185. }
  186. }
  187.  
  188. private void DisplayComponentDetails(GameObject selectedObject)
  189. {
  190. Component[] components = selectedObject.GetComponents<Component>();
  191. notesText += "Components:\n";
  192. foreach (var component in components)
  193. {
  194. notesText += $"- {component.GetType().Name}\n";
  195. }
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment