Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do components(e.g transform) save data?

Discussion in 'Scripting' started by Longman1981, Mar 13, 2018.

  1. Longman1981

    Longman1981

    Joined:
    Feb 16, 2018
    Posts:
    16
    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 you move to game mode, this means that completely new program has started, 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.
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    This is very confusing post for me.
    Editor mode and game mode "share the data" this is true. but when you you're in game mode and move an objects position, then stop and go back to edit mode, this will not be saved. this is normal functionality of unity.
     
  3. Longman1981

    Longman1981

    Joined:
    Feb 16, 2018
    Posts:
    16
    I'm talking about Editor mode changes that affect play mode. Not the other way around. e.g you're in Editor mode, move object from (1;1) to (2;2) than press play and you'll see that gameobject will appear at (2;2), so how does transform instance of this game object in play mode know about this new value (2;2)?

    If nothing I wrote does make sense to you, than imagine I want to write exact copy of transform component, people can move object in editor mode and transform component will change position accordingly, than I would need to share this player position to play mode's transform instance somehow, how would I do that?
     
    Last edited: Mar 13, 2018
  4. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    Well Gameobjects always and automatically have some type of Transform Component connected with them, doesn't matter if they are in Edit mode or play mode.
    I believe you are talking about the data that is serialized and deserialized when Editor/Game modes swap. You're not quite accurate. not "every" change in edit mode passes to game mode. Unity simply takes a snapshot recording of the current state of said object before switching game-modes. Unity combs through the fields using reflection and serializes certain fields that meet a criteria (basically UnityObject references, or primitive types stored in public fields or fields with a serializeField attribute). Unity saves that snapshot to the Scene file and uses it during its deserialization step when you switch to game mode. so even though they are new object instances, unity uses reflection to set those same fields it saved to the snapshot.

    when you leave gamemode back to editmode Unity will then use that same snapshot from eariler to rebuild the scene for edit mode.

    Data changed at game-time will not be saved when you return to edit mode.... if its rooted to a scene. Assets on the other hand (like scriptableObjects) can be edited at game time and changes can be saved when returning to edit mode.

    The sharing of data between mode is automatically supported for all your classes that derive from ScriptableObject or Monobehaviour. Custom classes can also be serialized but they need to have a [System.Serializable] attrbute and a UnityObject from the scene as a root for it to write its serialization data (ex. a Monobehaviour must be on a gameobject before its data could be serialized) in a scene its the Gameobjects that are the roots for data that gets serialized, while for ScriptableObjects its the Asset file itself that serves as the root.

    In other words Components you write should have this feature built-in automatically for you by default. You don't have to think about it much. for starters just make the data you want to persist from editor as fields either with the public modifier or with the [SerializeField] attribute.
     
    Last edited: Mar 13, 2018
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    It's written to and from file, using Unity's yaml-based serialization.

    Try opening a scene file (.unity) in a text editor, and you'll see the layout of that data.

    When you press play, Unity takes your current scene, writes it to that format, starts the game, and loads that data. Not sure if it's all in-memory or if it's put in a temporary file.
     
  6. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I still don't understand witch peace of data is missing. When he moves a gameobject from transform.posion (1,1) to position (2,2) in editor mode, of course in game mode the gameobject will be at position (2,2).
    Is this a prefab you're talking about? in game mode you are instantiating it?
     
  7. Longman1981

    Longman1981

    Joined:
    Feb 16, 2018
    Posts:
    16
    This helps a lot, I came to conclusion to use SriptableObject for this purpose, but I'll try out this solution when I come home.