Saving a RectTransform by value?

Discussion and help for Easy Save 3
Post Reply
Willy Wonka
Posts: 3
Joined: Wed Jan 04, 2023 6:15 am

Saving a RectTransform by value?

Post by Willy Wonka »

Hello,

I'm currently trying to save a RectTransform without saving it to a reference in the scene, as the RectTransform is part of a GameObject that is dynamically created at runtime, so the reference will not be there upon load. One thing I thought of is to create a temporary class or struct that holds the properties of the RectTransform I want (anchoredPosition, localPosition, localScale, localRotation) and save that instead. Is this the best approach or is there a better way?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving a RectTransform by value?

Post by Joel »

Hi there,

Generally in this case you should use ES3.LoadInto to load the data into your own RectTransform instance

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

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Willy Wonka
Posts: 3
Joined: Wed Jan 04, 2023 6:15 am

Re: Saving a RectTransform by value?

Post by Willy Wonka »

Will ES3.LoadInto work for dictionaries if the number of elements don't match? I'm currently saving this dictionary:

Code: Select all

public Dictionary<string, ActorRuntimeData> activeActors;
ActorRuntimeData has the RectTransform instance:

Code: Select all

public class ActorRuntimeData {
    public RectTransform rectTransform;
}
The dictionary will vary though, so the number of elements or what kind of ActorRuntimeData is stored in there is never guaranteed.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving a RectTransform by value?

Post by Joel »

Hi there,

In that example you're not calling ES3.LoadInto on the RectTransform, you're calling it on a class which has a RectTransform as a field. This doesn't give Easy Save an instance to load the data into.

Instead you would need to save the RectTransforms separately (individually or in an array), and call ES3.LoadInto on this.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Willy Wonka
Posts: 3
Joined: Wed Jan 04, 2023 6:15 am

Re: Saving a RectTransform by value?

Post by Willy Wonka »

Hi Joel,

Thanks for the advice! I managed to get it working after some pain. For anyone curious about how I did this, here's a fraction of the code that shows the general idea:

I added this to the ActorRuntimeData class:

Code: Select all

public class ActorRuntimeData {
    public RectTransform rectTransform;
    
    public void SaveReferences(out UnityEngine.Object[] references)
    {
        references = new UnityEngine.Object[2];
        references[0] = rectTransform;
        references[1] = transform;
    }
}
Then, in the class that contains the dictionary with the runtime data, I saved another dictionary containing the references along with that original dictionary:

Code: Select all

private void OnSave(ISaveService service)
{
    var references = new Dictionary<string, UnityEngine.Object[]>();

    foreach (var (name, actor) in activeActors) {
        // This just makes sure all the runtime data have references assigned to them
        actor.SyncFromActor();
        actor.SaveReferences(out var objRefs);

        references.Add(name, objRefs);
    }

    var activeActorsCopy = new Dictionary<string, ActorRuntimeData>(activeActors);

    // These just call ES3.Save(), if you're curious
    service.AddOrUpdateSaveData("activeActors", activeActorsCopy);
    service.AddOrUpdateSaveData("activeActorsRefs", references);
}
Then on load, I make another dictionary with references that I can load into with ES3.LoadInto. Not sure if there's a better way as this is pretty long-winded way to do it, but I guess that's the price of saving and loading dynamically created objects.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving a RectTransform by value?

Post by Joel »

Glad you managed to find a solution which works for you.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply