- Home /
How do components(e.g transform) save data?
Let's say you place gameobject at the position (1;1). now as you're in editor mode, I guess one instance of transform object is created for this gameobject right? This transform object knows when you move gameobject and set's new position values. This all happens in editor mode, now when you move to game mode, We get new instance of this object, but this new one knows every change that happened in editor mode, so question is how do this two instances share data?
In other words, I would like to copy this behavior and create my own similar component, which will share data between editor and game mode.
Answer by Xarbrough · Mar 13, 2018 at 01:00 PM
Unity components do not share data between edit and play mode. They also don't know about changed made by the user.
Instead, Unity uses custom editors and handles to change component data. When a user selects a Transform, a handle appears in the scene and an inspector that component is shown. If you change the values via user input, the data gets written to the Transform components. Internally the data is handled by the Unity serializer, which we come in contact with via the SerializedObject and SerializedProperty classes. All data is actually marshalled to the C++ representation of objects in the engine. This is where the scene graph lives (a collection of all objects related to the current scene). The scene is stored as a binary or text file in the YAML format. If you select "Force Text Serialization" in the Editor settings, you can open a scene file to see how objects are saved to disk.
When you press play, the entire scene is destroyed an recreated. This is why we can't, for example, easily persist changes from play mode to edit mode afterwards. The exception being project assets, which maintain their values through play mode.
At play mode, the editor still provides the tools from edit mode, so you can still move object with the Transform handle and the values are being written to the component. However, these changes are not written to the YAML scene file to disk, therefore they are lost, when you exit play mode.
Coming back to your question. you said, that the Transform component at runtime knows the values from the Transform component at edit time. This is handled by the Unity serializer automatically. To make your own component you simply inherit from the MonoBehaviour class and add an instance of the component to a GameObject in a scene. Now, any serialized data is carried over to play mode.
Do you mean something else with your question? Generally you need to make sure that your data is serializable and as said, that scene objects are destroyed when entering and leaving play mode. Persisting changes from play mode back to edit mode might be relevant topic, but I'm unsure if you're asking about this.
Your answer
Follow this Question
Related Questions
Preview procedural texture in scene without serializing it? 1 Answer
OnSerialize event 2 Answers
How do you save changes made with custom editor? 3 Answers
SerializedProperty with children class fields 2 Answers
UnityEvent Serialization Problem 1 Answer