Flexi Archive System 支持轻松添加新的数据类型和存档策略,允许你存档自定义的复杂类型数据。
目前系统已支持的类型:
float、double、int、long、bool、string、vector2、vector3、vector4
object、list、array、dictionary、custom
如果你需要存档一个自定义类型,你无需关心存档过程中所做的操作,也无需对系统进行修改。你只需负责创建一个 CustomData 以及一个 AbstractDataType 具体类型,按照Litjson0.19所支持的类型要求对复杂的数据进行转换。 具体见LitJson文档
建议你在编写 Wrapper 的同时对 ToString 方法进行重写,方便数据以明文的形式显示在开发环境中。
创建自定义类型 #
代码示例(Plan A):
/// <summary>
/// 1.create construct method
/// </summary>
public class CustomDataType : AbstractDataType<CustomData>
{
public CustomDataType(string dataStr) : base(dataStr){}
}
/// <summary>
/// 2.override "ToString(CustomData)" function if would know detail data
/// </summary>
public class CustomData
{
public string author = "温文";
public int code = 1;
public double luckly = 1.0f;
public override string ToString()
{
return "author: " + author +
" code: " + code +
" luckly: " + luckly;
}
}
代码示例(Plan B):
/// <summary>
/// 3.If it is a complex type that litjson can't serialize,
/// # you can choose to extend ValueWrapper class,
/// # I wrapped the conversion method for you.
/// </summary>
public class CustomDataWrapper : ValueWrapper<CustomDataWrapper, CustomData2>
{
public string author = "温文";
public int code = 1;
public double luckly = 1.0f;
public override void ValueToTheWrapper(CustomData2 value)
{
//convert
author = value.author;
code = value.code;
luckly = (double)value.luckly; //float -> double
}
public override CustomData2 WrapperToValue()
{
//new object
throw new System.NotImplementedException();
}
}
public class CustomData2
{
public string author = "温文";
public int code = 1;
public float luckly = 1.0f;
public override string ToString()
{
return "author: " + author +
" code: " + code +
" luckly: " + luckly;
}
}
存档 #
获取持有CustomData
值的数据对象
DataObject dataObject = archiveManager.GetDataObject("group3", "key2");
CustomDataType customDataType= dataObject.GetData<CustomDataType>();
写入:
customDataType.Write(new CustomData("温文",1,1.0f));
读取:
CustomData customData = customDataType.DiskData;