Search Unity

How to properly use JSON files to instantiate prototypes?

Discussion in 'Scripting' started by esrever2357, Jan 21, 2019.

  1. esrever2357

    esrever2357

    Joined:
    Dec 12, 2018
    Posts:
    23
    I have a class that specifies some global traits for a creature prototype: age range, size, speed, etc.
    I have several of those prototypes, stored in a json file.
    I want to instantiate all these prototypes once and gameobjects would then reference them, as in the flyweight pattern.
    So far I've been using Resources.Load, called in the initialization of a static variable, but then today I read that I shouldn't use it. I could also use the AssetDatabase to link to the json file, but then again that only works in Editor mode.

    So, what's the best way to utilize the json data to instantiate such prototypes? Ideally programmatically, rather than fiddling with GUI. Is there a better way of doing things?

    Thanks.
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You can use ScriptableObjects for that. That would simplify the editing process.
    Pre-made prefab + data from scriptable object should be sufficient.

    Alternatively, it's possible to create scriptable objects / prefabs in editor, to avoid doing that in runtime.
     
    Last edited: Jan 21, 2019
    MNNoxMortem likes this.
  3. esrever2357

    esrever2357

    Joined:
    Dec 12, 2018
    Posts:
    23
    Thanks, did that, looks like it's working alright