- Home /
How to create a TextFile in ProjectWindow with Rename option?
I want to create assets in the project window that can be named upon creation, just like every other built in menu item asset when rightclicking. As an example I wanted to see how Unity implemented the menuItem for a new script in the project window, NOT on a gameobject. I searched Unitys Git Reference but found no result for the specifing Attribute MenuItem["Assets/Create/C# Script"] to see how they actual create an .cs asset.
This is an example of a new empty text file:
[MenuItem("Assets/Create/TextFile")]
public static void CreatePlainTextFile(){
string projectWindowPath = AssetDataBase.GetAssetPath(Selection.active);
string filename = "NewFile.txt"; //Default name that will be marked to rename
string assetPath = projectWindowPath + "/" + filename;
TextAsset txt = new TextAsset();
ProjectWindowUtil.CreateAsset(txt, assetPath); //This gives actual functionality to name it on creation
AssetDatabase.Refresh ();
}
Now a new option called "TextFile" has appeared in the context menu under "Create"
But this will create a file with no icon and text after pressed Enter. This file is not in the physical Assets folder and cannot be removed in the editor. In order to do so, you have to delete the Library folder to rebuild. Also just reimporting the Assets will not clear this file.
.
Has someone an advice on creating WITH renaming option using menuItem? ;)
Can't you add a file in the /Editor/Data/Resources/ScriptTemplates
folder of Unity and name it 70-Text file-NewTextFile.txt.txt
? Fill this txt file with the default content of your file when created.
After restarting Unity, it will automatically recognise your file and suggest you to create a NewTextFile
just above the C# script
.
I'm not sure what it is you want and whether hellium's comment has anything to do with what you want. But based on Hellium's comment you can find the code in Unity's C# reference, since you mentioned not being able to find it. This is the place in code where they create the C# script asset. Though you'll have to find the part that interests you hoping it isn't internal.
Thanks for your suggestions, maybe i didn't clarify this. I just wanted Unity to create an empty text file from the project window ins$$anonymous$$d of going to the file explorer and creating a text file from there. Therefore I wanted to be able to rename it upon creation, just like other assets that you create when using the context menu. I already had managed to place a new file using File.CreateText() but this approach would be static as it appears already with a default name, not in rename mode.
And I don't believe you have tried what I have suggested.
By placing a new file in the /Editor/Data/Resources/ScriptTemplates
only once, Unity will recognize a new file template and add an entry in the Create $$anonymous$$enu
on every project wihtout the need of your custom script.
Ok thanks, didn't knew that about this. Thought that using $$anonymous$$enuItem is better for portability since the code relies in the project, not in the local Unity installation. But hey, atleast I learned something new and therefore I am thankful :D
Your answer
Follow this Question
Related Questions
Editor scripting - How to show the Assets menu as a context menu 1 Answer
(Editor) How to add Checkmarks to MenuItems 4 Answers
Are there project files for a game like.... 0 Answers
How to change or remove custom MenuItem icon? 0 Answers
Reopening a project causes it to raise a ReflectionTypeLoadException 0 Answers