Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- public class GameNotesEditor : EditorWindow
- {
- private string notesText = "";
- private bool addLineNumbers = true;
- private bool addEmptyLines = false;
- private bool saveToFile = true;
- private bool autoSave = true;
- private int currentLineNumber = 1;
- private string savePath;
- private Vector2 scrollPos;
- private static readonly string saveFileName = "EditorNotes.txt";
- private double lastAutoSaveTime;
- private double autoSaveInterval = 10.0; // Auto save every 10 seconds
- private bool showSaveIndicator = false;
- private DateTime lastSaveTime;
- int _selected = 0;
- string[] _options = new string[3] { "Item1", "Item2", "Item3" };
- [MenuItem("Window/Game Notes")]
- public static void ShowWindow()
- {
- const int width = 800;
- const int height = 800;
- var x = (Screen.currentResolution.width - width) / 2;
- var y = (Screen.currentResolution.height - height) / 2;
- GetWindow<GameNotesEditor>("Game Notes").position = new Rect(x, y, width, height);
- }
- private void OnEnable()
- {
- savePath = Path.Combine(Application.persistentDataPath, saveFileName);
- LoadFromFile();
- if (addLineNumbers && string.IsNullOrEmpty(notesText))
- {
- AddNewLine();
- }
- EditorApplication.update += AutoSave;
- }
- private void OnDisable()
- {
- EditorApplication.update -= AutoSave;
- }
- private void OnGUI()
- {
- EditorGUILayout.BeginVertical();
- scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.ExpandHeight(true));
- notesText = EditorGUILayout.TextArea(notesText, GUILayout.ExpandHeight(true));
- EditorGUILayout.EndScrollView();
- EditorGUILayout.Space();
- EditorGUILayout.BeginHorizontal();
- if (GUILayout.Button("Add Line"))
- {
- AddNewLine();
- }
- if (GUILayout.Button("Save Notes"))
- {
- SaveToFile(notesText);
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("Settings", EditorStyles.boldLabel);
- addLineNumbers = EditorGUILayout.Toggle("Add Line Numbers", addLineNumbers);
- addEmptyLines = EditorGUILayout.Toggle("Add Empty Lines", addEmptyLines);
- saveToFile = EditorGUILayout.Toggle("Save to File", saveToFile);
- autoSave = EditorGUILayout.Toggle("Auto Save", autoSave);
- EditorGUI.BeginChangeCheck();
- this._selected = EditorGUILayout.Popup("My Simple Dropdown", _selected, _options);
- if (EditorGUI.EndChangeCheck())
- {
- Debug.Log(_options[_selected]);
- }
- EditorGUILayout.Space();
- if (showSaveIndicator)
- {
- EditorGUILayout.LabelField("Notes saved successfully!", EditorStyles.boldLabel);
- if ((DateTime.Now - lastSaveTime).TotalSeconds > 2)
- {
- showSaveIndicator = false;
- }
- }
- EditorGUILayout.EndVertical();
- }
- private void AddNewLine()
- {
- if (addLineNumbers)
- {
- notesText += $"{(addEmptyLines ? "\n" : "")}{currentLineNumber}. ";
- currentLineNumber++;
- }
- else
- {
- notesText += addEmptyLines ? "\n" : "";
- }
- }
- private void SaveToFile(string text)
- {
- if (saveToFile)
- {
- File.WriteAllText(savePath, text);
- showSaveIndicator = true;
- lastSaveTime = DateTime.Now;
- Repaint();
- }
- }
- private void LoadFromFile()
- {
- if (File.Exists(savePath))
- {
- notesText = File.ReadAllText(savePath);
- UpdateLineNumber(notesText);
- }
- }
- private void UpdateLineNumber(string text)
- {
- var lines = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
- currentLineNumber = lines.Length;
- for (int i = lines.Length - 1; i >= 0; i--)
- {
- if (lines[i].StartsWith($"{currentLineNumber - 1}. "))
- {
- currentLineNumber++;
- break;
- }
- }
- }
- private void AutoSave()
- {
- if (autoSave && saveToFile)
- {
- double currentTime = EditorApplication.timeSinceStartup;
- if (currentTime - lastAutoSaveTime >= autoSaveInterval)
- {
- SaveToFile(notesText);
- lastAutoSaveTime = currentTime;
- }
- }
- }
- [InitializeOnLoadMethod]
- private static void OnProjectLoadedInEditor()
- {
- Selection.selectionChanged += OnSelectionChange;
- }
- private static void OnSelectionChange()
- {
- GameObject selectedObject = Selection.activeGameObject;
- if (selectedObject != null)
- {
- GameNotesEditor window = GetWindow<GameNotesEditor>();
- window.notesText += $"\nSelected Object: {selectedObject.name}\n";
- window.DisplayComponentDetails(selectedObject);
- }
- }
- private void DisplayComponentDetails(GameObject selectedObject)
- {
- Component[] components = selectedObject.GetComponents<Component>();
- notesText += "Components:\n";
- foreach (var component in components)
- {
- notesText += $"- {component.GetType().Name}\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment