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?
Saving a RectTransform by value?
Re: Saving a RectTransform by value?
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
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
-
- Posts: 3
- Joined: Wed Jan 04, 2023 6:15 am
Re: Saving a RectTransform by value?
Will ES3.LoadInto work for dictionaries if the number of elements don't match? I'm currently saving this dictionary:
ActorRuntimeData has the RectTransform instance:
The dictionary will vary though, so the number of elements or what kind of ActorRuntimeData is stored in there is never guaranteed.
Code: Select all
public Dictionary<string, ActorRuntimeData> activeActors;
Code: Select all
public class ActorRuntimeData {
public RectTransform rectTransform;
}
Re: Saving a RectTransform by value?
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
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
-
- Posts: 3
- Joined: Wed Jan 04, 2023 6:15 am
Re: Saving a RectTransform by value?
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:
Then, in the class that contains the dictionary with the runtime data, I saved another dictionary containing the references along with that original dictionary:
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.
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;
}
}
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);
}
Re: Saving a RectTransform by value?
Glad you managed to find a solution which works for you.
All the best,
Joel
All the best,
Joel