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. Dismiss Notice

Question Why does SerializeUtility.DeserializeWorld take in object[] instead of UnityEngine.Object[]?

Discussion in 'Entity Component System' started by mbaker, Jun 16, 2022.

  1. mbaker

    mbaker

    Joined:
    Jan 9, 2013
    Posts:
    52
    Ran into an issue when working with SerializeUtility. The unityObjects parameter is typed to object[] on both serialize and deserialize. Any idea why this is?

    The internal methods that SerializeUtility.DeserializeWorld's call explicitly cast the parameter to UnityEngine.Object[]. Shouldn't that be the incoming parameter's type?

    This came up as part of deserializing a larger state. Generally I try to avoid having any null values in serialized data. Since we don't currently have any managed references in our ECS state the array is empty.

    It's an easy quirk to work around but seems a bit strange that the public methods would ask for an object[] when really UnityEngine.Object[] is valid. Am I missing something?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,019
    My best guess is future compatibility. Perhaps eventually you can serialize any C# object?
     
  3. mbaker

    mbaker

    Joined:
    Jan 9, 2013
    Posts:
    52
    I don't think that's the case. The object[] is an out parameter on the C# object. The serialize method populates that array and you're then responsible for figuring out how you want to serialize those managed references.

    On deserialize you reconstruct that object[] and pass it in to support the deserialization and reconnect the managed references to entities.

    It would be better if the parameter was typed to UnityEngine.Object[] for now and if they add support for object in the future they can change the parameter to object[] without breaking any existing code since UnityEngine.Object[] can implicitly cast to object[].