- Home /
How to assign variables like Texture2D, TextAsset, Transform, etc. using code?
I'm trying to make a mission system for my game, and I need to assign multiple Text files into an array of TextAssets. The easiest way to do this would be to loop through the directory which contains the files, and then assign them to the array. How would I do that?
Answer by qJake · Aug 02, 2010 at 07:17 PM
You don't. Everything is done via the Inspector using references to assets. This way, Unity can optimize your game as much as possible (since reading and writing directories like that at runtime can be slow).
If you make a public TextAsset[]
variable, you can fill that in using the Inspector.
But if you really want to read the assets from a directory, you can put everything into folders inside one of the special Assets folder, /Resources/. Everything in there will be included at runtime, so you can grab stuff out of there if you want to, like this:
TextAsset myAsset = Resources.Load("myTextFile.txt") as TextAsset;
"myTextFile.txt" needs to be in the Resources/ folder at that point. You can also use subfolders.
No it shouldn't. Either way is fine, I just prefer using as.
You shouldn't include the "txt" file extension in the Resources.Load call. See: http://answers.unity3d.com/questions/57968/resourcesload-driving-me-a-bit-crazy.html
Your answer
Follow this Question
Related Questions
Get path of editor script? 2 Answers
Resources.FindObjectsOfTypeAll() doesn't return all Textures 2 Answers
Programmatically Exporting an Asset Package 4 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Build from script 3 Answers