Skip to main content
added 11 characters in body
Source Link

Short of using a third-party parser or writing your own parser for any file format, Unity 5.3 has a built-in JsonUtility class that can automatically parse a JSON string and apply its values to a class:

JsonUtility.FromJsonOverwrite: Overwrite data in an object by reading from its JSON representation.

For example, say you had a Creature class:

public class Creature : MonoBehaviour {
    public string creatureName;
    public int health;
    public int attack;
}

You could have a JSON file named Archer.json placed in your Resources folder containing:

{
    "creatureName": "Archer",
    "health": 3,
    "attack": 1
}

You could then load and assign the JSON values to a Creature like so:

// Load the JSON file
TextAsset textAsset = (TextAsset)Resources.Load("Archer"); // Don't include the .json extension
string jsonString = textAsset.text;

// Instantiate a creatureCreature, assuming you have a `GenericCreature` prefab in your Resources folder
GameObject prefab = (GameObject)Resources.Load("GenericCreature");
GameObject newObject = (GameObject)Instantiate(prefab);
Creature newCreature = newObject.GetComponent<Creature>();

// Assign the Archer JSON values to the Creature
JsonUtility.FromJsonOverwrite(jsonString, newCreature);

Short of using a third-party parser or writing your own parser for any file format, Unity 5.3 has a built-in JsonUtility class that can automatically parse a JSON string and apply its values to a class:

JsonUtility.FromJsonOverwrite: Overwrite data in an object by reading from its JSON representation.

For example, say you had a Creature class:

public class Creature : MonoBehaviour {
    public string creatureName;
    public int health;
    public int attack;
}

You could have a JSON file named Archer.json placed in your Resources folder:

{
    "creatureName": "Archer",
    "health": 3,
    "attack": 1
}

You could load and assign the JSON values to a Creature like so:

// Load the JSON file
TextAsset textAsset = (TextAsset)Resources.Load("Archer"); // Don't include the .json extension
string jsonString = textAsset.text;

// Instantiate a creature
GameObject prefab = (GameObject)Resources.Load("GenericCreature");
GameObject newObject = (GameObject)Instantiate(prefab);
Creature newCreature = newObject.GetComponent<Creature>();

// Assign the Archer JSON values to the Creature
JsonUtility.FromJsonOverwrite(jsonString, newCreature);

Short of using a third-party parser or writing your own parser for any file format, Unity 5.3 has a built-in JsonUtility class that can automatically parse a JSON string and apply its values to a class:

JsonUtility.FromJsonOverwrite: Overwrite data in an object by reading from its JSON representation.

For example, say you had a Creature class:

public class Creature : MonoBehaviour {
    public string creatureName;
    public int health;
    public int attack;
}

You could have a JSON file named Archer.json placed in your Resources folder containing:

{
    "creatureName": "Archer",
    "health": 3,
    "attack": 1
}

You could then load and assign the JSON values to a Creature like so:

// Load the JSON file
TextAsset textAsset = (TextAsset)Resources.Load("Archer"); // Don't include the .json extension
string jsonString = textAsset.text;

// Instantiate a Creature, assuming you have a `GenericCreature` prefab in your Resources folder
GameObject prefab = (GameObject)Resources.Load("GenericCreature");
GameObject newObject = (GameObject)Instantiate(prefab);
Creature newCreature = newObject.GetComponent<Creature>();

// Assign the Archer JSON values to the Creature
JsonUtility.FromJsonOverwrite(jsonString, newCreature);
Source Link

Short of using a third-party parser or writing your own parser for any file format, Unity 5.3 has a built-in JsonUtility class that can automatically parse a JSON string and apply its values to a class:

JsonUtility.FromJsonOverwrite: Overwrite data in an object by reading from its JSON representation.

For example, say you had a Creature class:

public class Creature : MonoBehaviour {
    public string creatureName;
    public int health;
    public int attack;
}

You could have a JSON file named Archer.json placed in your Resources folder:

{
    "creatureName": "Archer",
    "health": 3,
    "attack": 1
}

You could load and assign the JSON values to a Creature like so:

// Load the JSON file
TextAsset textAsset = (TextAsset)Resources.Load("Archer"); // Don't include the .json extension
string jsonString = textAsset.text;

// Instantiate a creature
GameObject prefab = (GameObject)Resources.Load("GenericCreature");
GameObject newObject = (GameObject)Instantiate(prefab);
Creature newCreature = newObject.GetComponent<Creature>();

// Assign the Archer JSON values to the Creature
JsonUtility.FromJsonOverwrite(jsonString, newCreature);