从我之前的一个问题开始构建。我正在尝试保存蓝图,这只是游戏对象/实体的一堆设置。我现在将组件(及其设置)存储为List< IEntityComponent> (IEntityComponent是任何组件的接口)包装在名为ComponentTable的类中。我只想序列化列表,并且所有私有内容都没有序列化,只是为了更快的查找(以内存的价格)。这序列化正确,甚至反序列化没有任何错误,但我注意到componentTable没有正确反序列化。
它创建了ComponentTable的实例,但从未实际向其添加值。因此,它不是包含CameraComponent,VelocityComponent和InputComponent的Component表,而只是一个空的ComponentTable。
{
"$types" : {
"ECS.Framework.Collections.Blueprint, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "1",
"ECS.Features.Core.CameraComponent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "2",
"ECS.Features.Core.VelocityComponent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "3",
"InputComponent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "4"
},
"$type" : "1",
"Components" : [
{
"$type" : "2",
"Tag" : "MainCamera",
"Test" : "0, 0, 0",
"BackgroundColour" : "0, 0, 1, 1",
"ViewportRect" : "10, 10 : 10, 10",
"Orthographic" : false,
"FieldOfView" : 60,
"OrthoSize" : 5,
"Depth" : 0,
"OcclusionCulling" : true,
"HDR" : false,
"Enabled" : true
},
{
"$type" : "3",
"Enabled" : true,
"CurrentVelocity" : "0, 0, 0"
},
{
"$type" : "4",
"TEST" : 0,
"Enabled" : true
}
],
"Children" : [
],
"Parent" : ""
}
这就是它的保存方式,所以看起来它正确保存。我只控制向量,矩形和颜色的序列化/序列化,因为任何单位值类型都会导致错误。
我认为它正确地序列化,但由于某种原因它没有反序列化到componentTable中。有没有人知道fastJSON是否存在这种继承问题(使一个类继承自List< customClass>?
理想情况下,我会将其继承为字典<键入,IEntityComponent>,但fastJSON不会序列化类型,只需将其保存为' System.Mono'然后在序列化时导致错误。
编辑:以下是蓝图和组件表类
public sealed class Blueprint
{
public ComponentTable Components { get; private set; }
public List<string> Children { get; set; }
public string Parent { get; set; }
public Blueprint()
{
Components = new ComponentTable();
Children = new List<string>();
Parent = "";
}
public Blueprint(Blueprint _blueprint)
{
Children = new List<string>(_blueprint.Children);
Parent = _blueprint.Parent;
}
}
public class ComponentTable : List<IEntityComponent>
{
private Dictionary<Type, IEntityComponent> Components { get; set; }
#region Constructors
public ComponentTable()
{
Components = new Dictionary<Type, IEntityComponent>();
}
#endregion
#region Base Function Overrides
public void Add(Type _type)
{
if (Components.ContainsKey(_type))
return;
InternalAdd(_type, (IEntityComponent)Activator.CreateInstance(_type));
}
public new void Add(IEntityComponent _component)
{
InternalAdd(_component.GetType(), _component);
}
public void Add<T>() where T : IEntityComponent
{
Add(typeof(T));
}
private void InternalAdd(Type _type, IEntityComponent _component)
{
if (Components.ContainsKey(_type))
throw new InvalidOperationException("Component already contained");
Components.Add(_type, _component);
base.Add(_component);
}
public bool Remove(Type _type)
{
if (Components.ContainsKey(_type))
return InternalRemove(_type, Components[_type]);
return false;
}
public new bool Remove(IEntityComponent _component)
{
return InternalRemove(_component.GetType(), _component);
}
public bool Remove<T>() where T : IEntityComponent
{
return Remove(typeof(T));
}
private bool InternalRemove(Type _type, IEntityComponent _component)
{
if (!Components.ContainsKey(_type))
return false;
Components.Remove(_type);
return base.Remove(_component);
}
public IEntityComponent Get(Type _type)
{
if (Contains(_type))
return Components[_type];
return null;
}
public T Get<T>() where T : IEntityComponent
{
return (T)Get(typeof(T));
}
public bool TryGetValue(Type _type, out IEntityComponent _component)
{
return Components.TryGetValue(_type, out _component);
}
public bool TryGetValue<T>(out IEntityComponent _component) where T : IEntityComponent
{
return TryGetValue(typeof(T), out _component);
}
public bool Contains(Type _type)
{
return Components.ContainsKey(_type);
}
public new bool Contains(IEntityComponent _component)
{
return Contains(_component.GetType());
}
public bool Contains<T>() where T : IEntityComponent
{
return Contains(typeof(T));
}
#endregion
}
答案 0 :(得分:0)
我在Microsoft .Net上对此进行了一些测试,发现了以下问题:
fastJSON不会反序列化Components
属性,除非它有公共设置器:
public sealed class Blueprint
{
public ComponentTable Components { get; set; }
似乎没有任何类型的配置选项可以解决此问题。从Reflection.cs
开始,如果setter不公开,您可以看到创建setter委托的方法返回null
:
internal static GenericSetter CreateSetMethod(Type type, PropertyInfo propertyInfo)
{
MethodInfo setMethod = propertyInfo.GetSetMethod();
if (setMethod == null)
return null;
fastJSON确实似乎无法反序列化List<T>
的子类 - 或任何其他非数组集合类 - 不是通用的。在deserializer内,有以下检查:
if (pi.IsGenericType && pi.IsValueType == false && v is List<object>)
oset = CreateGenericList((List<object>)v, pi.pt, pi.bt, globaltypes);
如您所见,它检查目标类型是否为通用,而不是目标类型或其基类型之一是通用的。
您可以通过使ComponentTable
成为通用名称来解决此问题:
public class ComponentTable<TEntityComponent> : List<TEntityComponent> where TEntityComponent : IEntityComponent
{
private Dictionary<Type, TEntityComponent> Components { get; set; }
#region Constructors
public ComponentTable()
{
Components = new Dictionary<Type, TEntityComponent>();
}
#endregion
#region Base Function Overrides
public void Add(Type _type)
{
if (Components.ContainsKey(_type))
return;
InternalAdd(_type, (TEntityComponent)Activator.CreateInstance(_type));
}
public new void Add(TEntityComponent _component)
{
InternalAdd(_component.GetType(), _component);
}
public void Add<T>() where T : IEntityComponent
{
Add(typeof(T));
}
private void InternalAdd(Type _type, TEntityComponent _component)
{
if (Components.ContainsKey(_type))
throw new InvalidOperationException("Component already contained");
Components.Add(_type, _component);
base.Add(_component);
}
public bool Remove(Type _type)
{
if (Components.ContainsKey(_type))
return InternalRemove(_type, Components[_type]);
return false;
}
public new bool Remove(TEntityComponent _component)
{
return InternalRemove(_component.GetType(), _component);
}
public bool Remove<T>() where T : IEntityComponent
{
return Remove(typeof(T));
}
private bool InternalRemove(Type _type, TEntityComponent _component)
{
if (!Components.ContainsKey(_type))
return false;
Components.Remove(_type);
return base.Remove(_component);
}
public IEntityComponent Get(Type _type)
{
if (Contains(_type))
return Components[_type];
return null;
}
public T Get<T>() where T : IEntityComponent
{
return (T)Get(typeof(T));
}
public bool TryGetValue(Type _type, out TEntityComponent _component)
{
return Components.TryGetValue(_type, out _component);
}
public bool TryGetValue<T>(out TEntityComponent _component) where T : IEntityComponent
{
return TryGetValue(typeof(T), out _component);
}
public bool Contains(Type _type)
{
return Components.ContainsKey(_type);
}
public new bool Contains(TEntityComponent _component)
{
return Contains(_component.GetType());
}
public bool Contains<T>() where T : IEntityComponent
{
return Contains(typeof(T));
}
#endregion
}
然后将Blueprint
更改为:
public sealed class Blueprint
{
public ComponentTable<IEntityComponent> Components { get; set; }
列表内容将被反序列化。然而...
您的ComponentTable
继承自List<T>
,需要覆盖Add()
。但Add()
不是虚拟的,因此您使用的是public new Add()
。这里的问题是,如果有人将您的课程强加给List<T>
并调用Add()
,那么您的方法就不会被调用。特别是,fastJSON在反序列化期间不会调用它!因此,在反序列化过程中,您的类型字典永远不会被初始化,从而无法实现目的。
你正在做的是重新发明KeyedByTypeCollection<TItem>
。而不是那样做,你可以使用它。使用此课程,您的ComponentTable
变得非常简单:
public class ComponentTable<TEntityComponent> : KeyedByTypeCollection<TEntityComponent> where TEntityComponent : IEntityComponent
{
public void Add(Type _type)
{
if (Contains(_type))
return;
Add((TEntityComponent)Activator.CreateInstance(_type));
}
public void Add<T>() where T : IEntityComponent, new()
{
Add(typeof(T));
}
}
现在可以序列化和反序列化集合,而Microsoft .Net上没有数据损坏。然而...
我不确定KeyedByTypeCollection<TItem>
是否存在于统一中。如果没有,您可能需要移植它。请参阅参考源KeyedByTypeCollection.cs
和基类源keyedcollection.cs
。可以在此处找到一次尝试:Alternative to KeyedByTypeCollection in Mono .Net。
作为替代方案,您可以考虑使用Json.NET。 Json.NET支持通过TypeNameHandling
setting序列化多态类型。但是,它是否可以统一使用?
Google suggests有一些统一的端口。这里还有各种问题tagged with both Json.NET and unity3d,例如JSON .Net Unity Deserialize dictionary of anonymous objects。因此,您可以进一步调查该选项。