- Home /
Load material from assets
In my Assets folder I have a folder named Material where I stored all the needed materials, One of the materials in the Material folder is Night_Sky, which I want at a certain moment of the game to replace the day_sky and set Night_sky as my default Skybox. I tried many codes all of them return null object, examples :
night = Resources.Load("Material", typeof(Material)) as Material;
or
night = Resources.Load("Material/Night_Sky.mat", typeof(Material)) as Material;
How can I load my Night_Sky material, or if there is an easier way to switch my skybox to night_sky thank you for sharing it
Answer by DNRN · Jan 21, 2015 at 02:49 PM
You need to put your materials in a parent folder named Resources like this: "Resources/Material/Night_Sky.mat"
Just to summerize sniper43s comment, here is an complete example:
Resources.Load("Material/Night_Sky.mat", typeof(Material)) as Material;
Then Unity is aware :)
Gettign resources through code like so
Resources.Load("$$anonymous$$aterial/Night_Sky.mat", typeof($$anonymous$$aterial)) as $$anonymous$$aterial;
is very processor heavy, and should be avoided. YOu can ins$$anonymous$$d assign an inpector object fromt eh resources folder (Objects don't have to be in-scene)
Use can use also Resources.Load<$$anonymous$$aterial>("Night_Sky");
$$anonymous$$aterial name cannot contain file extension.
Answer by sniper43 · Jan 21, 2015 at 02:50 PM
It should be better to have a public Material NightSky at teh begining of your script, and drag and dorp it in the inspector.
You found one of the ONLY ways to load materials from code, but it is usually not recommended.
To switch the skyboxes material modify it's renderer.material property
Answer by Obree · Sep 19, 2016 at 04:58 PM
If you wanted to load and set of Materials into an array you could use
Material[] mats = Resources.LoadAll("Materials", typeof(Material)).Cast<Material>().ToArray();
With the materials saved in Assets/Resources/Materials folder.
I had some trouble getting the cast right so thought I would put if up for anyone stuck like me
That would simply be:
$$anonymous$$aterial[] mats = Resources.LoadAll<$$anonymous$$aterial>("$$anonymous$$aterial/");
Answer by Cursedth · Oct 05, 2017 at 04:12 PM
It works. But you have to leave out the ".mat" extension
material = Resources.Load("/Materials/mymaterial", typeoff(Material)) as Material;
Where "mymaterial" is the material you want to load.
Fix your grammar... jeez... Also, it's typeof
not typeoff
Answer by astracat111 · Aug 05, 2018 at 12:27 PM
I think it has something to do with the path name, not with Resources.Load. As metroidsnes said, you can't have .mat. Still trying myself, will post some results.
In my case there was an error "Resources/$$anonymous$$aterials/Green_grass", ins$$anonymous$$d it should have been "/$$anonymous$$aterials/Green_grass" So as it is written in the documentation, the initial folder name (Resources) should not be in the path.
Your answer
Follow this Question
Related Questions
Skybox showing mostly white 0 Answers
Environment Material Disappears after ingame level restart 0 Answers
change skybox via script help ? 1 Answer