public void SetObject(ref object parent, UnityEngine.Object unity) { if (parent == null) { parent = IL.Help.CreateInstaince(type, null); } if (unity_fieldInfo == null) { unity_fieldInfo = IL.Help.GetField(type, "unity"); if (unity_fieldInfo == null) { L.LogErrorFormat("type:{0} not find unity_fieldInfo", type.Name); return; } } #if UNITY_EDITOR unity = UnityObjectSerialize.To(unity, IL.Help.GetRealType(unity_fieldInfo.FieldType)); #endif try { unity_fieldInfo.SetValue(parent, unity); } catch (System.Exception ex) { L.LogException(ex); } }
void ITypeSerialize.MergeFrom(ref object value, Nested.AnyBase ab) { byte flag = ab.valueType; if (flag == 0) { value = null; return; } if (value == null) { value = IL.Help.Create(type); } if (flag == 2) { return; } int cnt = fieldInfos.Count; FieldInfo field; ITypeSerialize its; for (int i = 0; i < cnt; ++i) { field = fieldInfos[i]; if (ab.Get(field.Name, out var fieldValue)) { object cv = field.GetValue(value); { its = BinarySerializable.GetByFieldInfo(field); if (fieldValue.typeFlags == its.typeFlag) { its.MergeFrom(ref cv, fieldValue); if (its.typeFlag == TypeFlags.unityObjectType) { cv = UnityObjectSerialize.To((UnityEngine.Object)cv, field.FieldType); } field.SetValue(value, cv); } } } } }
public void MergeFrom(ref object value, IStream stream) { byte flag = stream.ReadByte(); if (flag == 0) { value = null; return; } if (value == null) { value = IL.Help.Create(type); } if (flag == 2) { return; } do { byte typeFlag = stream.ReadByte(); var fieldName = stream.ReadString(); var length = RLStream.ReadLength(stream); int endPos = stream.WritePos; stream.WritePos = stream.ReadPos + length; try { var fieldInfo = FindFieldInfo(fieldName); ITypeSerialize bs; if (fieldInfo == null) { //L.LogFormat("type:{0} field:{1} typeFlag:{2} 字段不存在!", IL.Help.GetInstanceType(value).FullName, fieldName, typeFlag); stream.ReadPos += length; } else if ((bs = BinarySerializable.GetByFieldInfo(fieldInfo)).typeFlag != typeFlag) { // 类型与之前的有变化了 //L.LogFormat("type:{0} field:{1} typeFlag {2}->{3} 有变化!", IL.Help.GetInstanceType(value).FullName, fieldName, typeFlag, bs.typeFlag); stream.ReadPos += length; } else { object cv = fieldInfo.GetValue(value); bs.MergeFrom(ref cv, stream); if (typeFlag == TypeFlags.unityObjectType) { cv = UnityObjectSerialize.To((UnityEngine.Object)cv, fieldInfo.FieldType); } fieldInfo.SetValue(value, cv); } } catch (System.Exception ex) { wxb.L.LogException(ex); } finally { #if UNITY_EDITOR if (stream.ReadSize != 0) { wxb.L.LogErrorFormat("type:{0} fieldName:{1} length:{2}", type.Name, fieldName, length); } #endif stream.WritePos = endPos; } }while (stream.ReadSize != 0); }