- Home /
How to save variables in editor, to use them during play mode
I have an editor tool that instantiates objects (around 1000 of them). I then need to store a list of references to those objects, as well as some properties (variables).
For this, I have a class that stores a reference to the object, and other variables. This class is in a prefab's component. However, If i serialize this, it takes approximately one minute when I click 'apply' to save the prefab. It also takes too much time to load the scene. However, instantiating them in runtime takes ~.2 seconds (on a PC).
Is there a better way to save the values of the list while in the editor, so that I can load them faster?
Thanks in advance
[EDIT]
Some code, as requested:
[System.Serializable]
public class GraphNode :ScriptableObject
{
public GameObject tileObject;
public int movementCost;
public bool occupied;
public bool walkable;
public bool IsFree {
get{ return !occupied && walkable;}
}
public bool waterTile;
public List<GraphNode> connections = new List<GraphNode> ();
}
[SerializeField]
private List<GraphNode> _nodes = new List<GraphNode> ();
Posting this in case you (vexe) subbed and there are no notifications for updates. I posted the code as you asked.
Are you certain that this is what's causing the delay? ... Btw, you know that ScriptableObjects
can't live in prefabs right? - why do you need your class to be a SO? can't it be a normal class?
That was the reason I didn't post the code initially, because I wanted to know if there was a proper way to keep references to game objects in a list (when I assign them in the editor). If I use ScriptableObject, when I create a list of GraphNode, the references to the tileObject will persist (as far as I know).
If you just simply want to keep references to some UnityEngine.Objects in a list, where the values persist, you don't need a SO - use a regular class ins$$anonymous$$d and just make sure it's mocked with System.Serializable
like you have written above. Use a SO when you want to have say, a Settings object for your game, create the SO with SO.CreateInstance, and an asset with AssetDatabase(mySo, path);
- SOs could solve stickier situations, they play well with polymorphic serialization, etc. But you sound like you don't need all that. SOs have their benefits, but they add extra overhead as well...
Your answer
Follow this Question
Related Questions
Unity custom editor, values will not serialize 1 Answer
Private list is serialized inside a prefab 0 Answers
SerializedObject data wipe when applying to prefab 0 Answers
What is runtime analogy for Editor's PrefabUtility.SetPropertyModifications? 0 Answers
Editing prefab fields from custom editor that are not automatically serialized 0 Answers