How to save a material at run time?

I have a large number of assets, all with multiple materials that I generate on the fly. I’d like to save these materials to disc (basically caching them). I’m doing all of this in the editor - in the end game, I’d just like to swap materials around, not generate and manually load textures.
So my question is - how do you save a material? Is it as simple as a binary serialization of the object to a “.mat” file?

edit: no, you can’t serialize it. (not marked as serializable). With that said, anyone know a way to save a dynamically create material?

I don’t think you can. I don’t understand why you need to, either.

You can use ULIB for save material from runtime or editor

Thanks, I’ll check it out.

So I can generate them in the editor, and so I don’t have to create thousands of them by hand.

Ohhhhhh, I think I understand your question now. You’ve created a material in the editor and you’re trying to figure out how to save it to a .mat file?

Just use AssetDatabase.CreateAsset to put the Material object into a file ending with “.mat” and then AssetDatabase.SaveAssets to commit the results.

If you don’t want to have hundreds of files, then I think it should also be possible to save multiple materials into a single .asset file using AddObjectToAsset. I’ve not tried that though.

1 Like

Thanks, Superpig! That’s exactly the information I needed.

1 Like

I’ve used assetdatabase.createasset save assets, but new material resets texture.

var atlasMaterial = new Material(mainShader);
atlasMaterial.mainTexture = atlasTexture;
Utils.SaveObjectToFile(atlasMaterial, "Assets/testMat.mat");

in Utils:

/// save object(material, mesh, etc) to file
    public static void SaveObjectToFile(Object obj, string fileName)
    {
        AssetDatabase.CreateAsset(obj, fileName);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }

I have a problem there two i have matrial with reset texture any body have a solution for that ?

  • var atlasMaterial = new Material(mainShader);

  • atlasMaterial.mainTexture = atlasTexture;

  • Utils.SaveObjectToFile(atlasTexture, “Assets/testTex.asset”);

  • Utils.SaveObjectToFile(atlasMaterial, “Assets/testMat.mat”);

Give it a try. In my script,this works.

In case someone still needs a solution for that, you need to save the textures as assets first, and then save the materials

Hi all bellow is my code that basically allows me to add four material to a game Object, I assign the method NextColor() to a button to move the materials forward and the method RevertColor to moved back, given the effect of changing colour to the game object, everything works fine! My question if someone has the knowledge to help me pls, I need to save the new material, so whenever I start the game the last material chose stills there, and save me the time to pick the material again. Many Thanks

{

public Material[ ] material;
public int x;
Renderer rend;

// Start is called before the first frame update
void Start()
{
x = 0;
rend = GetComponent();
rend.enabled = true;
rend.sharedMaterial = material[×];

}

// Update is called once per frame
void Update()
{
rend.sharedMaterial = material[×];

}

public void NextColor()
{
if(x<4)
{
x++;
}
else
{
x=4;
}

}

public void RevertColor()
{
if (x>0)
{
x–;
}

}

Please create your own threads rather than hijacking/necro existing ones.

Also, here’s how to post code so other devs can follow it: https://discussions.unity.com/t/481379

Thanks.

1 Like