Search Unity

Caching/Precompute in Editor and then...

Discussion in 'Scripting' started by mgrenier, Jun 23, 2011.

  1. mgrenier

    mgrenier

    Joined:
    Apr 26, 2011
    Posts:
    57
    I'm looking to pre-compute heavy calculation in the Editor so that when the scene load, the data is serialized somewhere. As you can see at the beginning of this demo, the frame rate is very low since my navmesh is computing a bunch of stuff.

    I did my homework on Serialization (Unity and .Net) and I wonder what is the best course of action?

    Having a complex object (network of nodes) computed in Editor mode and then...
    a. store in a gameobject (so it is already on the scene when it load)
    b. serialized in binary format (to be later loaded at runtime)

    Any advice would be really appreciated
     
  2. mgrenier

    mgrenier

    Joined:
    Apr 26, 2011
    Posts:
    57
    It seem like solution (a) is not as easy as I though it would. On the other hand solution (b) is really strait forward: implements System.Runtime.Serialization.ISerializable and then dump to file in binary or xml.

    Loading files at runtime is not permitted in the WebPlayer, so what would be the best way to acheive loading? Resources.Load?
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    loading files is permitted but they have to be on a HTTP reachable place.

    But naturally you can also use Resources or you can make them a scriptableobject and have them assigned on a monobehaviours field
     
  4. mgrenier

    mgrenier

    Joined:
    Apr 26, 2011
    Posts:
    57
    ScriptableObject seems to have the same behavior of [Serializable] (only public members, etc...)

    So I went and try Serialize my class with ISerializable and try to load it at runtime. I found a wierd behavior with Deserialization.

    Because UnityEngine.Vector3 dont implement ISerializable, I needed to convert it to a temp-class Vec3.

    Code (csharp):
    1.  
    2. // On serialization
    3. public void GetObjectData(SerializationInfo info, StreamingContext context)
    4. {
    5.     info.AddValue("vector", new Vec3(100,100,100));
    6. }
    7.  
    8. // On deserialization
    9. protected Node(SerializationInfo info, StreamingContext context)
    10. {
    11.     Debug.Log("Deserializing Node...");
    12.     // Deserialize
    13.     Vec3 temp = (Vec3)info.GetValue("vector", typeof(Vec3));
    14.     // Should print (100,100,100)
    15.     Debug.Log(temp);
    16.  
    17.  
    18.     // Convert Vec3 => Vector3
    19.     this.vector = temp.toVector3();
    20.  
    21.     Debug.Log("Node deserialized");
    22. }
    23.  
    In my Vec3(SerializationInfo info, StreamingContext context) (constructor of a serialized object), I put a Debug.Log(...)

    Then I took a look at my console...
    Code (csharp):
    1.  
    2. Deserializing Node...
    3. (0, 0, 0)
    4. Node deserialized
    5. Deserializing Vec3 (100, 100, 100)
    6.  
    Carefully look at the order... "Deserializing Vec3..." should happened right after "Deserializing Node...". I understand that, internally, the deserialization process is asynch and the value returned by GetValue is updated at some point in the future... but since I need to convert my temp-class Vec3 to Vector3, I'm stuck

    Off to bed now... any help would be appreciated
     
  5. mgrenier

    mgrenier

    Joined:
    Apr 26, 2011
    Posts:
    57
  6. mgrenier

    mgrenier

    Joined:
    Apr 26, 2011
    Posts:
    57