- Home /
How to extract every material from a scene inside an editor script?
I have to write an editor script which saves the contents of an imported scene (which uses an internal process) onto Unity. The import itself works fine, I have every single GameObject with their respective materials, some of which have been imported via an AssetBundle.
So, when I save the scene, I lose some of my materials since they come from AssetBundles.
In order to circumvent this issue, I must find a way to save all AssetBundle ressources into my Unity Project in order to keep my material references.
For the moment, I've tried getting all Renderers via FindObjectsOfType. Here's the snippet :
foreach (var renderer in FindObjectsOfType<Renderer>())
{
var materials = renderer.sharedMaterials;
for (var i = 0; i < renderer.sharedMaterials.Length; i++)
{
var materialPath = materialsPath + "/Materials/" + materials[i].name + ".mat";
if (!AssetDatabase.Contains(materials[i]))
{
AssetDatabase.CreateAsset(materials[i], materialPath);
}
}
}
This results in keeping the materials, but discarding the textures. If you want more details in order to help, feel free to ask.
Your answer
![](https://koobas.hobune.stream/wayback/20220613010120im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Missing textures & materials in asset bundle 1 Answer
How to save emissive Material assets using "_EMISSION" keyword by script? 1 Answer
Find and replace material in Scene in editor in C# 1 Answer
Can AssetDatabase.LoadAllAssetsAtPath Load All Assets Recursively? 2 Answers
Materials with built-in shaders from assetbundle objects not assigned 1 Answer