In Editor - Update Material and save it to disk
It seems to be straightforward in runtime mode to update the textures of a material and save the changes to disk. However I currently try to automate setting up materials upon the import of new textures in editor mode.
Basically I am loading a material asset, update some of its textures and try to save the changes back to disk, but I don't seem to be able to find the right command for saving the changes back to the asset.
Here's my code:
// Asset does exist, setup maps
Material material = (Material)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guids[0]), typeof(Material));
if (tex_name.Contains("_col"))
{
material.SetTexture("_MainTex", tex);
}
if (tex_name.Contains("_nor"))
{
material.SetTexture("_BumpMap", tex);
}
if (tex_name.Contains("_par"))
{
material.EnableKeyword("_EMISSION");
material.SetTexture("_EmissionMap", tex);
}
// Save Updated Material to Disk
AssetDatabase.SaveAssets();
I think it create a new material $$anonymous$$aterial material =
. Check GUID for new asset, I guess it is different from disk one,
Try delete AssetAtPath --> Create New asset at Path with changed material. Will work, but you will lose all reference to scene and object
Your answer
![](https://koobas.hobune.stream/wayback/20220612173315im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Good practices for storing readonly data 0 Answers
Material.SetFloat not updating. 0 Answers
Add ScriptableObject to collection upon creation 0 Answers
Update object properties in editor 0 Answers