Skip to content

Instantly share code, notes, and snippets.

@terokorp
Created July 4, 2023 03:55
Show Gist options
  • Save terokorp/439be1cc266d14696333f75537effe25 to your computer and use it in GitHub Desktop.
Save terokorp/439be1cc266d14696333f75537effe25 to your computer and use it in GitHub Desktop.
using System.IO;
using UnityEditor;
using UnityEngine;
// Unity editor script that adds .txt to asset creation list
public class CreateTxt : Editor
{
private static string filename = "NewTextAsset";
[MenuItem("Assets/Create/Text/.txt", priority = 1000, secondaryPriority = 10000)]
private static void CreateNewTextAsset()
{
// Getting current directory
string directory = AssetDatabase.GetAssetPath(Selection.activeObject);
if (Path.HasExtension(directory))
directory = Path.GetDirectoryName(directory);
// Generating filename
string file = Path.Combine(directory, filename + ".txt");
if(File.Exists(file))
{
for (int i = 1; i < 1000; i++)
{
file = Path.Combine(directory, string.Format("{0} {1}.txt", filename, i));
if (!File.Exists(file))
break;
}
}
// Creating file
using (File.Create(file)) { };
//Importing created asset
AssetDatabase.ImportAsset(file);
TextAsset text = (TextAsset)AssetDatabase.LoadAssetAtPath(file, typeof(TextAsset));
Selection.activeObject = text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment