-
-
Save dfischer/04630e05b7882c76259e593fb09e38f4 to your computer and use it in GitHub Desktop.
A really really simple text editor for Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is a really simple text editor to be used within Unity | |
It's useful to write memos, notes or ideas inside your Unity Editor window | |
Kinda like Blender's Text Editor | |
*/ | |
using UnityEngine; | |
using UnityEditor; | |
using System; | |
using System.IO; | |
public class TextEditor : EditorWindow | |
{ | |
string text; | |
string path; | |
[MenuItem("Window/Text Editor")] | |
static void Init() | |
{ | |
GetWindow<TextEditor>(false, "Text Editor"); | |
} | |
void OnGUI() | |
{ | |
var toolbar_rect = DrawToolbar(); | |
float y_offset = toolbar_rect.height + toolbar_rect.y; | |
var text_rect = new Rect(toolbar_rect.x, y_offset, position.width, position.height - y_offset - 4); | |
var style = EditorStyles.textArea; | |
style.richText = true; | |
text = EditorGUI.TextArea(text_rect, text, style); | |
} | |
Rect DrawToolbar() | |
{ | |
var rect = EditorGUILayout.BeginHorizontal(); | |
EditorGUI.DrawRect(rect, Color.white * 0.5f); | |
Button(new GUIContent("New"), NewFile, GUILayout.Width(48)); | |
Button(new GUIContent("Open"), OpenFile, GUILayout.Width(48)); | |
Button(new GUIContent("Save"), SaveFile, GUILayout.Width(48)); | |
EditorGUILayout.LabelField(Path.GetFileName(path), EditorStyles.miniLabel, GUILayout.ExpandWidth(true)); | |
EditorGUILayout.EndHorizontal(); | |
return rect; | |
} | |
void NewFile() | |
{ | |
text = ""; | |
path = ""; | |
DefocusAndRepaint(); | |
} | |
void OpenFile() | |
{ | |
path = EditorUtility.OpenFilePanel("Open text file", "", "*"); | |
text = File.ReadAllText(path); | |
DefocusAndRepaint(); | |
} | |
void SaveFile() | |
{ | |
if (string.IsNullOrEmpty(path)) | |
{ | |
path = EditorUtility.SaveFilePanel("Save text file", "", "", "*"); | |
} | |
File.WriteAllText(path, text); | |
DefocusAndRepaint(); | |
} | |
void DefocusAndRepaint() | |
{ | |
GUI.FocusControl(null); | |
Repaint(); | |
} | |
// Function used to draw buttons in one line, Copy it and use it elsewhere if you want ;) | |
void Button(GUIContent content, Action action, params GUILayoutOption[] options) | |
{ | |
if (GUILayout.Button(content, EditorStyles.miniButtonLeft, options)) | |
{ | |
action(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment