Saving material Instances

Discussion and help for Easy Save 3, The Complete Save Game & Data Serializer System for the Unity Engine
noob_vulcan
Posts: 4
Joined: Mon Apr 25, 2022 4:38 pm

Saving material Instances

Post by noob_vulcan »

Hi,

I'm working a game where you can spawn different objects like table, chair etc.

Each object has multiple materials, and user can change the color, tiling of texture and even change whole material from a given material list.

So my all objects have instanced material as i'm changing them at runtime. So what would be the best approach to save these materials? As i can change color,tiling and even a new material (like if i want to make table wooden or metal).

Kinda stuck here for a while, would be great if you can help.
User avatar
Joel
Moodkie Staff
Posts: 5036
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving material Instances

Post by Joel »

Hi there,

For saving the instantiated prefabs themselves you would follow this guide:
https://docs.moodkie.com/easy-save-3/es ... s-prefabs/

With regards to saving the Materials, this section explains what you will need to do:
https://docs.moodkie.com/easy-save-3/es ... nd-warning

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
lockhighjumpingvenus
Posts: 15
Joined: Wed Oct 11, 2023 9:52 pm

Re: Saving material Instances

Post by lockhighjumpingvenus »

Hi, Is there a Example for doing this? Trying to do something similar with the materials
User avatar
Joel
Moodkie Staff
Posts: 5036
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving material Instances

Post by Joel »

lockhighjumpingvenus wrote: Thu Aug 08, 2024 7:50 pm Hi, Is there a Example for doing this? Trying to do something similar with the materials
Hi there,

If you haven't done so, read the following section of the following guide:
https://docs.moodkie.com/easy-save-3/es ... nd-warning

So as the guide suggests, you would just save all of your Materials separately to any data which references it. For example:

Code: Select all

List<Material> materials = GetAllOfYourMaterialsAsAList();
ES3.Save("materials", materials);
// Now save anything which uses these Materials.
ES3.Save<List<GameObject>>("gameObjects", myGameObjects);
And then when loading, load these Materials before loading anything which uses them. E.g.

Code: Select all

ES3.Load<List<Material>>("materials");
ES3.Load<List<GameObject>>("gameObjects");
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
lockhighjumpingvenus
Posts: 15
Joined: Wed Oct 11, 2023 9:52 pm

Re: Saving material Instances

Post by lockhighjumpingvenus »

Is there a simpler way to reassign all the materials to the gameobjects in the scene, or does it just need to be done for each object. Thinking if there is 50> objects, finding the correct material and assigning it to the correct object doesnt sound too appealing. :lol:
User avatar
Joel
Moodkie Staff
Posts: 5036
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving material Instances

Post by Joel »

lockhighjumpingvenus wrote: Fri Aug 09, 2024 4:38 pm Is there a simpler way to reassign all the materials to the gameobjects in the scene, or does it just need to be done for each object. Thinking if there is 50> objects, finding the correct material and assigning it to the correct object doesnt sound too appealing. :lol:
Hi there,

Sorry I think you may misunderstand, the example above doesn't require you to manually reassign the materials to each of the GameObjects. When you load the GameObjects (or the renderers of the GameObjects specifically), it will load the Materials which you previously loaded by reference ... you don't need to manually assign them.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
lockhighjumpingvenus
Posts: 15
Joined: Wed Oct 11, 2023 9:52 pm

Re: Saving material Instances

Post by lockhighjumpingvenus »

mmm I seem to be having issues with the materials been loaded and "attached" again to the correct gameobjects. If I instead use the autosave feature for the gameobjects, and just es3.save for the material, should it still work that way? Or do the gameobjects then need to be saved as es3.save
User avatar
Joel
Moodkie Staff
Posts: 5036
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving material Instances

Post by Joel »

Hi there,

It can work that way, but you would need to ensure that you're loading your materials before Auto Save loads it's GameObjects.

Are you getting any errors or warnings?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
lockhighjumpingvenus
Posts: 15
Joined: Wed Oct 11, 2023 9:52 pm

Re: Saving material Instances

Post by lockhighjumpingvenus »

Hi. I have been debugging the issue over the last few days and have managed to get it somewhat working. I can colour whatever object I want, save and leave the saved scene all fine. When I come back into the scene load up the material and the gameobject, that also works fine. However, if i then try and recolour the same object, or a different object and save the material list again, exit and re-enter the scene (and load in the material list) the newly coloured materials are not loaded, and they dont even seem to be loaded from the save file.

I check what is being saved via (save script)

Code: Select all

        ES3.Save("materials", materials, loadScene);
        foreach (var key in ES3.Load<List<Material>>("materials", loadScene))
            Debug.Log(" key name " + key.name);
and all is fine, but when i check what is loaded via this code
(Load script)

Code: Select all

            foreach (var key in ES3.Load<List<Material>>("materials", loadScene))
                Debug.Log(" load - key name " + key.name);
only the initial material is returned. The material that was first created via the first save. Do you possibly know any reason this could be occuring?
User avatar
Joel
Moodkie Staff
Posts: 5036
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving material Instances

Post by Joel »

Are you getting any errors or warnings?

Also Easy Save will only save what you tell it to and won't make any modifications to the things you're saving. If your Materials aren't in the List you've saved, it means that they weren't in there when you saved, so you might want to check your logic that the Materials are getting added to the List.
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply