Unity ゲームでゲームデータをロードして管理する必要がある場合、JSON ファイルを使用するのが一般的な方法です。このブログでは、C# と Unity の JSON 逆シリアル化機能を使用してこれを実現する方法について詳しく説明します。 Unity の JsonUtility を使用して、JSON データを逆シリアル化し、カスタム C# データ構造にマッピングできます。
まず、ゲーム内のキャラクターと武器のデータをロードして管理するためのデータ クラスをいくつか作成しましょう。この例では、Player、Monster、WeaponData の 3 つのデータ型を使用します。
{
"players": [
{
"id": "1",
"name": "player0",
"weaponID": "102",
"maxHp": "50",
"damage": "0",
"defense": "0",
"moveSpeed": "5.0",
"coolDown": "0",
"amount": "0"
},
{
"id": "2",
"name": "player1",
"weaponID": "101",
"maxHp": "40",
"damage": "-10",
"defense": "0",
"moveSpeed": "5",
"coolDown": "20",
"amount": "0"
},
{
"id": "3",
"name": "player2",
"weaponID": "101",
"maxHp": "50",
"damage": "20",
"defense": "1",
"moveSpeed": "5.0",
"coolDown": "-50",
"amount": "1"
},
{
"id": "4",
"name": "player3",
"weaponID": "102",
"maxHp": "50",
"damage": "-50",
"defense": "-1",
"moveSpeed": "6",
"coolDown": "100",
"amount": "0"
}
]
}
JSON データと一致するデータ構造を作成する必要があります。これらのクラスは [Serializable] 属性を使用して、JSON のシリアル化と逆シリアル化を有効にします。
[Serializable]
public class Monster
{
public int id;
public string name;
public int maxHp;
public int damage;
public int defense;
public float moveSpeed;
public int expMul;
}`在这里插入代码片`
JSON ファイルを観察すると、この JSON ファイルの例は複数のプレーヤー情報を含む配列であることがわかりました。
[Serializable]
public class PlayerData
{
public List<Player> players = new List<Player>();
}
プレーヤー データ構造の配列を使用して、この JSON ファイルを保存できます。
使用
TextAsset textAsset = Managers.Resource.Load<TextAsset>($"Data/PlayerData");
PlayerData []players JsonUtility.FromJson<Loader>(textAsset.text);
解析してトラバースして辞書に追加するには、
解析する JSON が大量にある場合は、JSON のロードを担当する汎用メソッド LoadJson を定義します。データを作成し、具体的な型のディクショナリに逆シリアル化します。このメソッドは、ILoader インターフェイスを実装する必要がある Loader タイプを受け入れます。
Loader LoadJson<Loader, Key, Value>(string path) where Loader : ILoader<Key, Value>
{
TextAsset textAsset = Managers.Resource.Load<TextAsset>($"Data/{
path}");
return JsonUtility.FromJson<Loader>(textAsset.text);
}
ILoaderインターフェース
public interface ILoader<Key, Value>
{
Dictionary<Key, Value> MakeDict();
}
JSON データ構造を格納する配列にこのインターフェイスを継承させます。
[Serializable]
public class PlayerData : ILoader<int, Player>
{
public List<Player> players = new List<Player>();
public Dictionary<int, Player> MakeDict()
{
Dictionary<int, Player> dict = new Dictionary<int, Player>();
foreach (Player player in players)
dict.Add(player.id, player);
return dict;
}
}
このようにして、JSON ファイルを配列に逆シリアル化できます。
public Dictionary<int, Data.WeaponData> WeaponData {
get; private set; } = new Dictionary<int, Data.WeaponData>();
public Dictionary<int, Data.Player> PlayerData {
get; private set; } = new Dictionary<int, Data.Player>();
public Dictionary<int, Data.Monster> MonsterData {
get; private set; } = new Dictionary<int, Data.Monster>();
public void Init()
{
PlayerData = LoadJson<Data.PlayerData, int, Data.Player>("PlayerData").MakeDict();
WeaponData = LoadJson<Data.WeaponDataLoader, int, Data.WeaponData>("WeaponData").MakeDict();
MonsterData = LoadJson<Data.MonsterData, int, Data.Monster>("MonsterData").MakeDict();
}
コードは以下のように表示されます
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface ILoader<Key, Value>
{
Dictionary<Key, Value> MakeDict();
}
public class DataManager
{
public Dictionary<int, Data.WeaponData> WeaponData {
get; private set; } = new Dictionary<int, Data.WeaponData>();
public Dictionary<int, Data.Player> PlayerData {
get; private set; } = new Dictionary<int, Data.Player>();
public Dictionary<int, Data.Monster> MonsterData {
get; private set; } = new Dictionary<int, Data.Monster>();
public void Init()
{
PlayerData = LoadJson<Data.PlayerData, int, Data.Player>("PlayerData").MakeDict();
WeaponData = LoadJson<Data.WeaponDataLoader, int, Data.WeaponData>("WeaponData").MakeDict();
MonsterData = LoadJson<Data.MonsterData, int, Data.Monster>("MonsterData").MakeDict();
}
Loader LoadJson<Loader, Key, Value>(string path) where Loader : ILoader<Key, Value>
{
TextAsset textAsset = Managers.Resource.Load<TextAsset>($"Data/{
path}");
return JsonUtility.FromJson<Loader>(textAsset.text);
}
}
DataManager の Init メソッドでは、キャラクターデータ、武器データ、モンスターデータなどのゲームデータを読み込み、初期化します。 LoadJson ジェネリック メソッドを呼び出すことで、さまざまな種類の JSON データを簡単にロードし、辞書オブジェクトに変換できます。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Data
{
#region Character
[Serializable]
public class Player
{
public int id;
public string name;
public int weaponID;
public int maxHp;
public int damage;
public int defense;
public float moveSpeed;
public int coolDown;
public int amount;
}
[Serializable]
public class PlayerData : ILoader<int, Player>
{
public List<Player> players = new List<Player>();
public Dictionary<int, Player> MakeDict()
{
Dictionary<int, Player> dict = new Dictionary<int, Player>();
foreach (Player player in players)
dict.Add(player.id, player);
return dict;
}
}
#endregion
#region Monster
[Serializable]
public class Monster
{
public int id;
public string name;
public int maxHp;
public int damage;
public int defense;
public float moveSpeed;
public int expMul;
}
public class MonsterData : ILoader<int, Monster>
{
public List<Monster> monsters = new List<Monster>();
public Dictionary<int, Monster> MakeDict()
{
Dictionary<int, Monster> dict = new Dictionary<int, Monster>();
foreach (Monster monster in monsters)
dict.Add(monster.id, monster);
return dict;
}
}
#endregion
#region Weapon
[Serializable]
public class WeaponData
{
public int weaponID;
public string weaponName;
public List<WeaponLevelData> weaponLevelData = new List<WeaponLevelData>();
}
[Serializable]
public class WeaponLevelData
{
public int level;
public int damage;
public float movSpeed;
public float force;
public float cooldown;
public float size;
public int penetrate;
public int countPerCreate;
}
[Serializable]
public class WeaponDataLoader : ILoader<int, WeaponData>
{
public List<WeaponData> weapons = new List<WeaponData>();
public Dictionary<int, WeaponData> MakeDict()
{
Dictionary<int, WeaponData> dict = new Dictionary<int, WeaponData>();
foreach (WeaponData weapon in weapons)
dict.Add(weapon.weaponID, weapon);
return dict;
}
}
#endregion
}
Unity の JsonUtility と C# の汎用メソッドを使用することで、ゲーム データの読み込みと管理を簡単に行うことができます。この方法は、さまざまな種類のデータを処理する場合に非常に便利で、コードは再利用可能です。このブログが、Unity での JSON デシリアライゼーションとデータ管理の理解に役立つことを願っています。ご質問がある場合、またはさらに詳しいガイダンスが必要な場合は、コメント欄でお気軽にお問い合わせください。