Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 YarnballSoftware · May 19, 2016 at 05:19 PM · editor extensiontextassetresource.loadfilename

Drag and drop files onto string variables in inspector to get the file name?

I have a script that has a string to set a text file name of a file that that the script processes. The text file is in my Resources folder, and I use Resources.Load<TextAsset> to load the file.

What I'd like to be able to do is drag the text file from my project window onto the string field in the inspector, and that sets the string with the file name of the text file I dragged. Is there some way to extend the Unity Editor to do this? I'm not familiar with Editor extending, but I'd like to learn.

Comment
Add comment · Show 2
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 toddisarockstar · May 20, 2016 at 03:14 PM 0
Share

I dont follow excatly what you are doing but here a couple things i found that might help. You cant manually drag and drop files into a text asset variable. its pry something unity should fix. but anyways, you can do it with code like this:

     var mydoc:TextAsset;  
         mydoc = AssetDatabase.LoadAssetAtPath("Assets/rules.txt", typeof(TextAsset)) as TextAsset;
     
     print(mydoc.text);


here is code to grab anyfile anywhere and rename it if you want:

 // some of this stuff doesn't work with builds for certain platforms.
 
 import System.IO;
 import System.Diagnostics;
 
 var txt:String;
 var thefile:bytes[];
 var anypath:String;
 anypath="C:/ToddsGames/$$anonymous$$iller Robots in Outer Space/";
 
 if(!System.IO.Directory.Exists(anypath)){
 System.IO.Directory.CreateDirectory(anypath);}
 
 // will grab anyfile reguarldess of extention 
 thefile=File.ReadAllBytes(anypath+"/anyfile.txt");
 
 //you would delete the file if you wanted to rewrite in the same place
 //with a different name
 File.Delete(anypath+"/anyfile.txt");
 
 //stick it back in the folder or anywhere you want with your new name.
 
 File.WriteAllBytes(anypath+"/newnamefile.txt",thefile);
 
 // if your file is a text file you can read it like this:
 txt=System.Text.Encoding.UTF8.GetString(thefile);
 
avatar image YarnballSoftware toddisarockstar · May 20, 2016 at 03:22 PM 1
Share

Actually you can drag a txt file onto a TextAsset variable in the inspector, as @FortisVenaliter said. I tried it, and it works great.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by FortisVenaliter · May 19, 2016 at 05:40 PM

You can extend the inspector, but you'd basically have to re-do everything built in for that class to do so.

However, there is a better way. Why not just make it a TextAsset reference instead of a String variable? Then you can totally drag-and-drop, and you won't need the Resources call to reference the asset.

Comment
Add comment · Show 3 · Share
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 YarnballSoftware · May 19, 2016 at 05:51 PM 0
Share

Ah, the TextAsset reference does help me a little bit, I should have realized Unity could do that.

The other thing I didn't mention was this could be co$$anonymous$$g from a file that doesn't have a .txt or other supported extension. Right now, I'm looking at .mot files from Lightwave. I wan't to be able to handle .mot files from within the Unity Editor, which is why I was pursuing the extension route. The script would transparently make a copy of the file into a .txt before importing it as a text asset. Do you think it's possible to extend the inspector to allow drag and drop of files onto string variables to take the filename as a string?

avatar image FortisVenaliter YarnballSoftware · May 19, 2016 at 05:56 PM 0
Share

Yes, it is possible. But like I said, you need to make a custom inspector for the whole class, so you lose the default inspector serialization. You would need to create a custom inspector for all fields, not just one. You might also look into property drawers, but I don't think they'd help as much as custom inspectors in this scenario.

Also, do they have to have the .mot extension? If the data is the same, just rename them ahead of time, since the extension doesn't matter in the project; only the data.

avatar image YarnballSoftware FortisVenaliter · May 20, 2016 at 01:13 PM 0
Share

Thanks, I really appreciate the info. I was trying to make the script for mot files as idiot-proof as possible (others besides myself will be using it), which is why I wanted to try and use the mot files directly.

Using a TextAsset ins$$anonymous$$d of a file name string should make the script fairly safe. Before I required the user to type in the file name, but if all the user has to do is change the file extension and drag the text file on the script, that should be a lot less error prone. Thanks again!

avatar image
0

Answer by Astiolo · Feb 22, 2018 at 03:46 AM

I've managed to find a solution for this courtesy of Bunny83. Really simple to add his code in and repurpose it for whatever you need.

https://github.com/Bunny83/UnityWindowsFileDrag-Drop

Comment
Add comment · Share
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

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

6 People are following this question.

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

Related Questions

Change contents of TextAsset 2 Answers

Text file read, random quote C# 1 Answer

Assign public variables in design time... through code! 0 Answers

How am I accessing my Dictionary wrong? 1 Answer

Instantiate prefab using Resources.Load() 2 Answers


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