Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Noxury · Jun 19, 2019 at 11:00 AM · textprojecteditor extensionmenuitem

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.

alt text

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? ;)

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Jun 19, 2019 at 11:47 AM 1
Share

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.

avatar image troien · Jun 19, 2019 at 12:27 PM 0
Share

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.

avatar image Noxury · Jun 19, 2019 at 02:54 PM 0
Share

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.

avatar image Hellium Noxury · Jun 19, 2019 at 03:58 PM 0
Share

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.

avatar image Noxury Hellium · Jun 19, 2019 at 04:38 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Editor scripting - How to show the Assets menu as a context menu 1 Answer

How can I Update HealthUI when the object is destroyed. 2 Answers

How can i split text file in unity and then spawn it as UI TEXT in game? 3 Answers

Text - Negative Color from Background 0 Answers

How can I avoid distorted UI Text in Unity 5? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges