Search Unity

Save Parsed Json into Prefab/Scriptable Object

Discussion in 'Formats & External Tools' started by JoshOClock, Feb 27, 2014.

  1. JoshOClock

    JoshOClock

    Joined:
    Dec 8, 2010
    Posts:
    107
    I have a Json file that's taking quite a while (15s) to parse and create a new JSONObject from.

    What are my options to speed this up?

    I'd like to serialize the result in a Prefab or ScriptableObject so I can just do it once... which isn't working. Does anyone have any suggestions?

    Thanks,
    Josh
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you try to create the prefab and scriptable object in the Unity editor, there shouldn't be an issue. If you struggle, show the code, such that we can help.
     
  3. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    You should be able to serialize it to a ScriptableObject. The trick is that you can't just deserialize into a ScriptableObject, usually the properties on the ScriptableObject will cause issues. You will need to copy over the data manually (e.g. deserialize to JSONObject then copy the arrays over to a newly created ScriptableObject).

    If you don't need to do this at runtime, you should do it in a build step. If it's shared across scenes, the ScriptableObject should live in an asset and you can use http://docs.unity3d.com/Documentation/ScriptReference/AssetPostprocessor.html to detect changes to repopulate the ScriptableObject. If the ScriptableObject lives in a scene, you should use http://docs.unity3d.com/Documentation/ScriptReference/Callbacks.PostProcessSceneAttribute.html to update the scene before building (this also works in the editor).

    Final caveat is PostProcessScene runs AFTER the Awake()/OnEnable() calls are executed for the scene objects when running in the Editor. I logged this as a bug and their response was "as designed"... yeah ... right.
     
  4. JoshOClock

    JoshOClock

    Joined:
    Dec 8, 2010
    Posts:
    107
    Thanks,

    I did experiment with the ScriptableObject a bit but in the end used SimpleJSON to compress and load the compressed file. Creating the JSONNode via LoadFromCompressedFile with SimpleJSON went from 15s to 3s. On top of that I put the load on another thread so by the time everything else is loaded my JSON is loaded as well.

    Thanks for the help...