Search Unity

Saving and writing 'actions' outside of Unity (JSON, XML, ScriptableObject)?

Discussion in 'Scripting' started by NickVst, Apr 20, 2020.

  1. NickVst

    NickVst

    Joined:
    Feb 17, 2017
    Posts:
    25
    Instead of having to create a prefab with a script, SpriteRenderer and BoxCollider2D for each of my items, I can store all of my items in a JSON file and load that file on startup.

    This means I can edit attributes of my game without recompiling the entire game, and has the additional boon of pretty much adding modding support without a second thought. Just save your objects like so:

    Code (JSON):
    1.  
    2. {
    3.     "items": [
    4.         {
    5.            "name": "sword",
    6.            "type": weapon"
    7.         },
    8.         {
    9.            "name": "potion",
    10.            "type": "consumable"
    11.         }
    12.     ]
    13. }
    14.  
    and loading them is easy as pie with JsonUtility , or if I feel adventurous, XML. However, this only contains incredibly flat data. What can I do to save an action, a reference to a sprite, data from a collider2D so I can recreate it later, or a projectile pattern?

    Ideally, I'd save the type of action this item performs as well, and then when loading the JSON I'd recreate or attach the behaviour depending on what's in the text document, but how would I go about doing that?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Check out Unity's Addressable Assets System: https://docs.unity3d.com/Packages/com.unity.addressables@1.7/manual/index.html

    Basically it will go like this.
    You can make pretty much anything addressable: ScriptableObjects, Textures, Prefabs, etc.
     
    NickVst likes this.