Creating a Data Manager for Unity 3D

http://mannequin-atelier.deviantart.com/art/Elf-archer-concept-designs-475128330

So you think about creating a game, whatever it is, one thing that you can definitely end up bumping into is how you will access all the character-level progression information. You might think about throwing everything in a local storage and that’s it, but at the end, putting information in an organized way can help you make the game balancing easier.

Imagine that we are creating a role playing game and we want to set up the main character levels, we can create a .xls and organize everything just like this:

Level    HP  BaseAttack
1 100 10
2 200 15

Using a xls file can be a simple way to edit character attributes in addition to having a broad view of the attributes for each level of the game, besides making this edition in a separate file enable you to share the same file with other team members such as game design and level design.

Habemus Script

Right now a step-by-step on how to create a Data Manager. There must be several different solutions, this one I am presenting here is just one of them. Before continuing, the Data Manager is nothing more than a script to manage important data on the game, in this example we are using it to manage the attributes of the main character’s level.

  1. Converting a .xls file into JSON

After editing the character level progression, you can convert it into a JSON using any library, I’ve used one called NPOI. In the Unity editor we have also to parser the JSON file, and to do this I used another lib called SimpleJSON.

Once converted your JSON file should look like this:

[
{
"level":1,
"maxHP":100,
"baseAttack":10
},
{
"level":2,
"maxHP":200,
"baseAttack":15
}
]

I called this file as PlayerLevelData, and put it inside a folder called Resources/ LevelData / PlayerLevelData.

2. Creating a Level Data object

Next step is to create a class that will have the character’s attributes:

using System.Collections.Generic;


[System.Serializable]
public class PlayerLevelData
{
[System.Serializable]
public class Attribute
{
public int level;
public int maxHP;
public float baseAttack;
}
}

3. Creating a DataManager

Right now we can actually create the DataManager, which in this case will be a singleton (Everyone will access the same instance). Two important things that we should create here are two attributes for DataManager. One that will have the JSON value retrieved from the resource, and another that will have the parsed object.

public TextAsset playerLevelDataJson; //JSON

public PlayerLevelData.Attribute[] playerLevelData; //Parsed Data

As I mentioned before, I will use the SimpleJSON library to make the parser easier, you can find it here:

The only thing you should do is download the SimpleJSON script and put this into a folder inside the game directory, I usually put it into a folder called Scripts / SimpleJSON. Here’s how the complete DataManager script looks like:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class DataManager : MonoBehaviour
{
private static DataManager _Instance;
public static DataManager Instance { get { return _Instance; } }

public TextAsset PlayerLevelDataJson;
public PlayerLevelData.Attribute[] PlayerLevelData;

void Awake()
{
if (_Instance == null)
{
_Instance = this;

PlayerLevelDataJson = (TextAsset)Resources.Load("LevelData/PlayerLevelData");

ParseData();

DontDestroyOnLoad(gameObject);
}

}

void ParseData()
{
PlayerLevelData = SimpleJson.SimpleJson.DeserializeObject<PlayerLevelData.Attribute[]>(PlayerLevelDataJson.text);

Debug.Log("JSON Level : "+PlayerLevelDataJson.text);

}

public PlayerLevelData.Attribute GetPlayerCurrentLevelData(int levelIndex)
{
return PlayerLevelData[levelIndex];
}

}

Now you can create an empty game object and add this script (I’ve called DataManager), note that the attributes are public, that way they will appear on the editor at runtime, but you do not have to drag anything to them, once you run the game, you will realize that they will be automatically populated by ParseData.

Now how you’re going to use it will depend, the suggestion is to create a script called PlayerData, which will have the PlayerLevelData reference that we created up there. The PlayerLevelData will then be loaded with the level you pass as parameter in the GetCurrentLevelData of DataManager, it is observed here that each time the player level up, the information of the current level will be shown in the editor:

An example of the PlayerData script:

using UnityEngine;
using System.Collections;


public class PlayerData : MonoBehaviour
{

public PlayerLevelData.Attribute LevelData;

private int _LevelIndex = 0;

public int MaxHP;
public float BaseAttack;

void Awake()
{

LevelData = DataManager.Instance.GetPlayerCurrentLevelData(_LevelIndex);

Debug.Log (" Player Level " + LevelData.level);
Debug.Log (" HP : " + LevelData.maxHP);
Debug.Log (" BaseAttack : " + LevelData.baseAttack);

Debug.Log (" Calling Level Up");

LevelUp();

Debug.Log (" Player Level " + LevelData.level);
Debug.Log (" HP : " + LevelData.maxHP);
Debug.Log (" BaseAttack : " + LevelData.baseAttack);

}


void LevelUp()
{
_LevelIndex = 1;

LevelData = DataManager.Instance.GetPlayerCurrentLevelData(_LevelIndex);

}
}

In this example the character starts at the first level and when we call LevelUp function, the level is updated with the new attributes.

See that now we can access any information of the character’s level simply and quickly. We can create various functions such as LevelUp taking the attribute information to the next level, or create more attributes like the amount of XP to get to a certain level.

So that’s it, if some information was not clear, you can download the example in the github below.

See you!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.