Page 1 of 1

Unique ID

Posted: Sat Feb 11, 2023 7:58 pm
by nFighter
Hey! I'm not using any autosave features, but I'm trying to save some custom data for particular objects in a specific scene. Let's say it's a "Point Of Interest" GameObjects that must save just one bool value "ifVisited". What is the best/correct method to save and load it? I thought about something like
1. Generate a unique ID for each GameObject I want to save
2. Save a pair: ID + boolValue in a file
3. On scene load read from a file and assign boolValue for each GameObject with a certain ID

BUT! I'm pretty sure it's a task that EasySave probably already solve for me in some way. Like, do I really need to invent my own unique ID generation or EasySave already have something?

Re: Unique ID

Posted: Sat Feb 11, 2023 8:02 pm
by nFighter
For now, I'm thinking about generating an ID with unity's native GUID like this

Code: Select all

public string _id = System.Guid.NewGuid().ToString();
but is it a legit move? Can I save references to GameObjects in a scene in some other way?

Re: Unique ID

Posted: Sun Feb 12, 2023 9:47 am
by Joel
Hi there,

Components are saved by reference, so you can simply save the Component containing your bool value and Easy Save will do the work for you (it will prompt you to add an Easy Save 3 Manager to your scene if you don't already have one).

I.e.

Code: Select all

ES3.Save("myComponent", component);

Code: Select all

// This will load the Component by reference, assuming the Component isn't generated at runtime.
ES3.Load<ComponentType>("myComponent");

Code: Select all

// Alternatively you can tell it what Component you want to load the data into.
ES3.Load<ComponentType>("myComponent"); // This will load the Component by reference.
If there are fields which you don't want to be saved, you can mark them with the [ES3NonSerialized] attribute or go to Tools > Easy Save 3 > Types, and select which fields you want to be saved for the type there.

All the best,
Joel

Re: Unique ID

Posted: Tue Feb 14, 2023 2:36 am
by nFighter
Oh, nice! It makes everything easier!