Reputation: 51
There is a folder in Unity project, where some material files are located. These materials can be assigned to various meshes etc. I want to have a list of these materials and to loop over this list in Update().
Is there a way to maintain this list automatically - I add a material file to folder, and corresponding Material object is added to this list?
Upvotes: 0
Views: 3971
Reputation: 367
You need to cycle through folders and find every file with .mat
extension and next load it manually.
Take a look at c# File
and Directory
classes in System.IO
for getting names of material files.
Next load them by Resources.Load()
i-e:
Resources.Load("Material/Night_Sky", typeof(Material)) as Material;
EDIT:
The above solution will only works in EDITOR as you can't get names of materials with System.IO
in builds. Resources.Load
will work fine in builds but you will need names of materials.
As mentioned in comments Resources.LoadAll()
will do the job.
Additionally for both Resources.Load()
and Resources.LoadAll()
, you need to put all materials you want to load into Resources
folders i-e:
If path to your materials is: Assets\Resources\Materials
you need to use:
Resources.LoadAll("Materials")
Helpful links to documentation:
Resources.LoadAll
LoadingResourcesatRuntime
Upvotes: 2