Skip to content

Commit 1e79b58

Browse files
committed
(feature): Additional text file types
1 parent c255788 commit 1e79b58

14 files changed

Lines changed: 132 additions & 2 deletions

Assets~/additional_text.png

8.52 KB
Loading
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System.IO;
2+
using UnityEditor;
3+
using UnityEngine;
4+
5+
namespace Nomnom.ProjectWindowExtensions.Editor {
6+
internal static class CreateAdditionalTextFiles {
7+
private const string ROOT_FOLDER = "Packages/com.nomnom.project-window-extensions/Editor/TextFilePresets/";
8+
9+
private static string _lastAssetPath;
10+
private static double _renameTime;
11+
private static bool _enableRename;
12+
13+
[MenuItem("Assets/Create/Text Files/Text File", false, 81)]
14+
private static void CreateTXT() {
15+
string path = CollectPath("NewFile.txt");
16+
string text = LoadPreset("TxtPreset");
17+
CreateAsset(text, path);
18+
}
19+
20+
[MenuItem("Assets/Create/Text Files/JSON File", false, 81)]
21+
private static void CreateJSON() {
22+
string path = CollectPath("NewFile.json");
23+
string text = LoadPreset("JsonPreset");
24+
CreateAsset(text, path);
25+
}
26+
27+
[MenuItem("Assets/Create/Text Files/XML File", false, 81)]
28+
private static void CreateXML() {
29+
string path = CollectPath("NewFile.xml");
30+
string text = LoadPreset("XmlPreset");
31+
CreateAsset(text, path);
32+
}
33+
34+
[MenuItem("Assets/Create/Text Files/CSV File", false, 81)]
35+
private static void CreateCSV() {
36+
string path = CollectPath("NewFile.csv");
37+
string text = LoadPreset("CSVPreset");
38+
CreateAsset(text, path);
39+
}
40+
41+
private static string CollectPath(string fileName) {
42+
if (!Selection.activeObject) {
43+
return $"Assets/{fileName}";
44+
}
45+
46+
return $"{AssetDatabase.GetAssetPath(Selection.activeObject)}/{fileName}";
47+
}
48+
49+
private static string LoadPreset(string preset) {
50+
return AssetDatabase.LoadAssetAtPath<TextAsset>($"{ROOT_FOLDER}{preset}.txt").text;
51+
}
52+
53+
private static void CreateAsset(string content, string path) {
54+
string absolutePath = $"{Application.dataPath}{path.Substring(6)}";
55+
using (StreamWriter streamWriter = new StreamWriter(absolutePath)) {
56+
streamWriter.Write(content);
57+
}
58+
59+
_lastAssetPath = path;
60+
61+
AssetDatabase.SaveAssets();
62+
AssetDatabase.Refresh();
63+
64+
_renameTime = EditorApplication.timeSinceStartup + 0.2d;
65+
_enableRename = true;
66+
EditorApplication.update += EngageRenameMode;
67+
}
68+
69+
private static void EngageRenameMode() {
70+
if (EditorApplication.timeSinceStartup >= _renameTime) {
71+
if (!_enableRename) {
72+
EditorApplication.update -= EngageRenameMode;
73+
GetFocusedWindow("General/Project").SendEvent(new Event {
74+
keyCode = KeyCode.F2,
75+
type = EventType.KeyDown
76+
});
77+
return;
78+
}
79+
80+
Selection.activeObject = AssetDatabase.LoadAssetAtPath<TextAsset>(_lastAssetPath);
81+
_enableRename = false;
82+
_renameTime = EditorApplication.timeSinceStartup + 0.2d;
83+
}
84+
}
85+
86+
private static EditorWindow GetFocusedWindow(string window) {
87+
FocusOnWindow(window);
88+
return EditorWindow.focusedWindow;
89+
}
90+
91+
private static void FocusOnWindow(string window) {
92+
EditorApplication.ExecuteMenuItem("Window/" + window);
93+
}
94+
}
95+
}

Editor/CreateAdditionalTextFiles.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/TextFilePresets.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+


Editor/TextFilePresets/CSVPreset.txt.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
3+
}

Editor/TextFilePresets/JsonPreset.txt.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+


Editor/TextFilePresets/TxtPreset.txt.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)