游戏运行时我们无法通过EPPlus读取Excel,不过我们可以将它保存成自定义格式,例如CSV JSON ScriptableObject等。
Unity 支持JSON的序列化和反序列化,需要注意的是,参与序列化的类必须在上方声明[Serializable]属性,并且支持对象的相互嵌套。
我们可以使用JsonUtility.ToJson()以及JsonUtility.FromJson<T>()来进行序列化反序列化。
遗憾的是,它不支持字典类型的序列化。
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class JSONTestMyTools
{
[MenuItem("MyJSON/JSONTest", false, 0)]
static void LoadJSONTest()
{
Data_JSONTest data = new Data_JSONTest();
data.name = "data";
data.subDatas.Add(new Data_JSONTest_SubData() { strvalue = "one", intvalue = 1, boolvalue = true });
data.subDatas.Add(new Data_JSONTest_SubData() { strvalue = "two", intvalue = 2, boolvalue = false });
string jsonstr = JsonUtility.ToJson(data);
Debug.Log(jsonstr);
data = null;
data = JsonUtility.FromJson<Data_JSONTest>(jsonstr);
Debug.Log(data.name);
for (int i = 0; i < data.subDatas.Count; i++)
{
Debug.Log("strvalue :" + data.subDatas[i].strvalue
+ " intvalue :" + data.subDatas[i].intvalue
+ " boolvalue :" + data.subDatas[i].boolvalue);
}
}
[Serializable]
public class Data_JSONTest
{
public string name;
public List<Data_JSONTest_SubData> subDatas = new List<Data_JSONTest_SubData>();
}
[Serializable]
public class Data_JSONTest_SubData
{
public string strvalue;
public int intvalue;
public bool boolvalue;
}
}
//--JSON支持字典
Unity 的JSON是不支持字典的,不过可以继承ISerializationCallbackReceiver接口,间接的实现字典的序列化。
using System.Collections.Generic;
using UnityEngine;
public class SerializableDictionary<K, V> : ISerializationCallbackReceiver
{
[SerializeField]
private List<K> m_Keys;
[SerializeField]
List<V> m_Values;
private Dictionary<K, V> m_Dictionary = new Dictionary<K, V>();
public int Count
{
get
{
return m_Dictionary.Count;
}
}
public V this[K key]
{
get
{
if (!m_Dictionary.ContainsKey(key))
{
return default(V);
}
return m_Dictionary[key];
}
set
{
m_Dictionary[key] = value;
}
}
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
{
foreach (var item in m_Dictionary)
{
yield return item;
}
}
public void Add(K key, V value)
{
if (m_Dictionary.ContainsKey(key))
{
Debug.LogError("same key :" + key.ToString());
return;
}
m_Dictionary.Add(key, value);
}
public void OnAfterDeserialize()
{
int count = m_Keys.Count;
m_Dictionary = new Dictionary<K, V>();
for (int i = 0; i < count; i++)
{
m_Dictionary[m_Keys[i]] = m_Values[i];
}
m_Keys = null;
m_Values = null;
}
public void OnBeforeSerialize()
{
m_Keys = new List<K>();
m_Values = new List<V>();
foreach (var item in m_Dictionary)
{
m_Keys.Add(item.Key);
m_Values.Add(item.Value);
}
}
}
还添加了 Add() Count 支持froeach 等。
using UnityEditor;
using UnityEngine;
public class JSONDictionaryMyTools
{
[MenuItem("MyJSON/JSONDictionary", false, 1)]
static void JSONDictionary()
{
SerializableDictionary<int, string> serializableDictionary = new SerializableDictionary<int, string>();
serializableDictionary.Add(100, "value100");
serializableDictionary[200] = "value200";
serializableDictionary[300] = "value300";
//serializableDictionary.Add(100, "new value100");
string jsonstr = JsonUtility.ToJson(serializableDictionary);
Debug.Log(jsonstr);
serializableDictionary = null;
serializableDictionary = JsonUtility.FromJson<SerializableDictionary<int, string>>(jsonstr);
//Debug.Log(serializableDictionary.Count);
//Debug.Log(serializableDictionary[100]);
foreach (var item in serializableDictionary)
{
Debug.Log(item.Key + " " + item.Value);
}
}
}