- Home /
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.
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);
Actually you can drag a txt file onto a TextAsset variable in the inspector, as @FortisVenaliter said. I tried it, and it works great.
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.
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?
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.
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!
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.
Your answer
Follow this Question
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