您的位置:首页 > 运维架构 > 网站架构

Unity3D自学笔记——架构应用(四)JsonToEntity帮助类更新

2016-11-02 02:39 501 查看

JsonToEntity帮助类更新

适应复合对象的解析

前面的JsonToEntity只能针对独立对象的解析,由于User类是复合类,包含了Account及Hero,所以对他进行了改进。

在遍历过程中首先判断JsonData是否为Object,如果为Object就递归,反射创建该Object类型的对象,再遍历子属性进行赋值。

public class JsonToEntity<T> where T : IEntityIntKey , new()
{
PropertyInfo[] m_Propertys;
JsonData m_Data;
PhotonCacheType m_CacheType;

public PropertyInfo[] Propertys
{
get
{
m_Propertys = new T().GetType().GetProperties();
return m_Propertys;
}
}

public JsonToEntity(PhotonCacheType cacheType, JsonData data)
{
this.m_CacheType = cacheType;
this.m_Data = data;
}

public void BuildEntityToCache()
{
for (int i = 0; i < m_Data.Count; i++)
{
T entity = (T)BuildEntity(m_Data[i], typeof(T));
PhotonDataCache.AddOrUpdateDataCache(this.m_CacheType, entity);
}
}

private object BuildEntity(JsonData childData, Type type)
{
var entity = Activator.CreateInstance(type);
PropertyInfo[] propertys = entity.GetType().GetProperties();
foreach (var item in propertys)
{
string name = item.Name;
Type t = item.PropertyType;
//是否为Object
if (childData[name].IsObject)
{
//递归,Build sub entity
var subEntity = BuildEntity(childData[name], t);
item.SetValue(entity, subEntity, null);
}
else
{
item.SetValue(entity, TransformType(childData[name], t), null);
}
}

return entity;
}

private object TransformType(JsonData data, Type type)
{
if (type == typeof(int))
{
int val;
Int32.TryParse(data.ToString(), out val);
return val;
}
else if (type == typeof(float))
{
float val;
float.TryParse(data.ToString(), out val);
return val;
}
else if (type == typeof(double))
{
double val;
double.TryParse(data.ToString(), out val);
return val;
}
else if (type == typeof(DateTime))
{
DateTime val;
DateTime.TryParse(data.ToString(), out val);
return val;
}
else if (type == typeof(bool))
{
bool val;
bool.TryParse(data.ToString(), out val);
return val;
}
else if (type == typeof(string))
{
return data.ToString();
}
return null;
}
}


今天对前台结构进行了调整

加入了Loading界面



将PhotonManger作为全局对象,不受切换场景而销毁



已完成角色创建





睡觉
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: