Saving current instance of prefab

Discussion and help for Easy Save 3
Post Reply
syu15
Posts: 16
Joined: Thu May 13, 2021 1:05 am

Saving current instance of prefab

Post by syu15 »

Hello,
I know this question has been asked before but I can't seem to get this to work for me. I have a prefab that I instantiate at runtime and have a script that has initially undefined values (shortened examples for clarity):

Code: Select all

public Bug : MonoBehaviour
{
    public System.DateTime spawnStartTime;
}
 
I then instantiate the prefab and set the values in another script:

Code: Select all

    public void SpawnNewEgg()
    {
        InstantiatedBug = Instantiate(BugPrefab);
        InstantiatedBug.GetComponent<Bug>().spawnStartTime = System.DateTime.UtcNow;
    }
I am currently saving it like so:

Code: Select all

    void OnApplicationQuit()
    {
        ES3.Save("bug", gameObject);
    }
and loading it like this:

Code: Select all

InstantiatedBug = ES3.Load<GameObject>("bug");
Like others, I want to be able to save the prefab with the values I've set. I current have the `ES3 Prefab` script attached, so it is saving, but I want that instance to be saved. I have a workaround where I am saving a separate class with all the set prefab values but it is becoming unwieldy.

I saw that it might be possible to use `ES3ReferenceMgr.Current.Add(object, id)` to save the exact instance of the prefab but I haven't gotten it to work for me. Can I get some guidance as to how to make that work?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving current instance of prefab

Post by Joel »

Hi there,

When saving prefab instances and GameObjects, you need to explicitly add support for custom Components in the Types window. For more information please see the Saving and Loading GameObjects guide:

https://docs.moodkie.com/easy-save-3/es ... s-prefabs/

To add explicit support for your Type you simply go to Tools > Easy Save 3 > Types, search for your type, ensure the fields you want serialised are selected and then press 'Create ES3Type script'.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
syu15
Posts: 16
Joined: Thu May 13, 2021 1:05 am

Re: Saving current instance of prefab

Post by syu15 »

Great, that seemed to work for me. Thanks!
syu15
Posts: 16
Joined: Thu May 13, 2021 1:05 am

Re: Saving current instance of prefab

Post by syu15 »

Hi,
I am running into another issue related to saving fields on prefabs. Basically I set a sprite at runtime on the prefab. I have created an es3 type for the SpriteRenderer where I save the sprite field (screenshot below). However, when I load up my game again, the sprite is still not being saved. I am wondering if it is because I am generating the sprite from a 2D texture (code below) or if there is something else I need to do to get it to work.

Code: Select all

        Addressables.LoadAssetsAsync<Object>(bugType, obj =>
        {
            if (obj.name == BugSpriteName)
            {
                Texture2D NewTexture = (Texture2D)obj;
                Rect rect = new Rect(0, 0, NewTexture.width, NewTexture.height);
                Vector2 pivot = new Vector2(0.5f, 0.5f);
                Sprite NewSprite = Sprite.Create(NewTexture, rect, pivot);
                GetComponent<SpriteRenderer>().sprite = NewSprite;
            }
        });
Attachments
Screen Shot 2021-05-24 at 1.34.35 PM.png
Screen Shot 2021-05-24 at 1.34.35 PM.png (93.5 KiB) Viewed 1164 times
syu15
Posts: 16
Joined: Thu May 13, 2021 1:05 am

Re: Saving current instance of prefab

Post by syu15 »

Nvm, was able to find a workaround when I explicitly asked for Sprites to be loaded as opposed to general objects and that seems to be saved correctly.
Post Reply