[Unity] DataManager
ゲームで使用するデータをコードで定義すると、メンテナンス上非常に不利になります.
(ゲームでデータのパッチを変更するたびに、ショップで更新されます.)
したがって,数値などをXmlやJsonファイルとして保存し,ゲーム中のデータをコードローディングで管理することが有利である.
DataManager
(ゲームでデータのパッチを変更するたびに、ショップで更新されます.)
したがって,数値などをXmlやJsonファイルとして保存し,ゲーム中のデータをコードローディングで管理することが有利である.
DataManager using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class Stat
{
public int level;
public int hp;
public int attack;
}
[Serializable]
public class StatData : ILoader<int, Stat>
{
public List<Stat> stats = new List<Stat>();
public Dictionary<int, Stat> MakeDict()
{
Dictionary<int, Stat> dict = new Dictionary<int, Stat>();
foreach (Stat stat in stats)
dict.Add(stat.level, stat);
return dict;
}
}
public interface ILoader<Key, Value>
{
Dictionary<Key, Value> MakeDict();
}
public class DataManager
{
public Dictionary<int, Stat> StatDict { get; private set; } = new Dictionary<int, Stat> ();
public void Init()
{
StatDict = LoadJson<StatData, int, Stat>("StatData").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);
}
}
外部ファイルからデータをインポートする場合は、シーケンス化(Sequency)を作成する必要があります.
MakeDict()関数では、Dictionaryはforeach文を使用して値を追加します.この部分はLinqで代用できますが、LinqはiPhoneでバグが多いので、Linqを使用しないことをお勧めします.
Reference
この問題について([Unity] DataManager), 我々は、より多くの情報をここで見つけました
https://velog.io/@kjhdx/Unity-DataManager
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class Stat
{
public int level;
public int hp;
public int attack;
}
[Serializable]
public class StatData : ILoader<int, Stat>
{
public List<Stat> stats = new List<Stat>();
public Dictionary<int, Stat> MakeDict()
{
Dictionary<int, Stat> dict = new Dictionary<int, Stat>();
foreach (Stat stat in stats)
dict.Add(stat.level, stat);
return dict;
}
}
public interface ILoader<Key, Value>
{
Dictionary<Key, Value> MakeDict();
}
public class DataManager
{
public Dictionary<int, Stat> StatDict { get; private set; } = new Dictionary<int, Stat> ();
public void Init()
{
StatDict = LoadJson<StatData, int, Stat>("StatData").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);
}
}
Reference
この問題について([Unity] DataManager), 我々は、より多くの情報をここで見つけました https://velog.io/@kjhdx/Unity-DataManagerテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol