private static ActiveCodeReqPROTO Read(ActiveCodeReqPROTO activeCodeReqPROTO, ProtoReader protoReader) { int fieldNum; while ((fieldNum = protoReader.ReadFieldHeader()) > 0) { if (fieldNum != 1) { if (fieldNum != 2) { if (activeCodeReqPROTO == null) { ActiveCodeReqPROTO activeCodeReq = new ActiveCodeReqPROTO(); ProtoReader.NoteObject(activeCodeReq, protoReader); activeCodeReqPROTO = activeCodeReq; } protoReader.AppendExtensionData(activeCodeReqPROTO); } else { if (activeCodeReqPROTO == null) { ActiveCodeReqPROTO activeCodeReq = new ActiveCodeReqPROTO(); ProtoReader.NoteObject(activeCodeReq, protoReader); activeCodeReqPROTO = activeCodeReq; } string activeCode = protoReader.ReadString(); if (activeCode != null) { activeCodeReqPROTO.ActiveCode = activeCode; } } } else { if (activeCodeReqPROTO == null) { ActiveCodeReqPROTO activeCodeReq = new ActiveCodeReqPROTO(); ProtoReader.NoteObject(activeCodeReq, protoReader); activeCodeReqPROTO = activeCodeReq; } long accountId = protoReader.ReadInt64(); activeCodeReqPROTO.AccountId = accountId; } } if (activeCodeReqPROTO == null) { ActiveCodeReqPROTO activeCodeReq = new ActiveCodeReqPROTO(); ProtoReader.NoteObject(activeCodeReq, protoReader); activeCodeReqPROTO = activeCodeReq; } return(activeCodeReqPROTO); }
void Awake() { MemoryStream ms = new MemoryStream(); ActiveCodeReqPROTO ace = new ActiveCodeReqPROTO(); ace.AccountId = 10000000000000001; ace.ActiveCode = "lyw"; ProtobufSerializer serializer = new ProtobufSerializer(new TypeModelProtobufSerializer()); serializer.Serialize(ms, ace); MemoryStream memoryStream = new MemoryStream(ms.ToArray()); ActiveCodeReqPROTO activeCodeReq = (ActiveCodeReqPROTO)serializer.Deserialize(memoryStream, typeof(ActiveCodeReqPROTO)); Debug.LogError(string.Format("AccountId = {0}, ActiveCode = {1}", activeCodeReq.AccountId, activeCodeReq.ActiveCode)); }
private static void Write(ActiveCodeReqPROTO activeCodeReqPROTO, ProtoWriter protoWrite) { long accountId = activeCodeReqPROTO.AccountId; if (accountId != 0) { // Variant可变的 ProtoWriter.WriteFieldHeader(1, WireType.Variant, protoWrite); ProtoWriter.WriteInt64(accountId, protoWrite); } string activeCode = activeCodeReqPROTO.ActiveCode; if (!string.IsNullOrEmpty(activeCode)) { ProtoWriter.WriteFieldHeader(2, WireType.String, protoWrite); ProtoWriter.WriteString(activeCode, protoWrite); } ProtoWriter.AppendExtensionData(activeCodeReqPROTO, protoWrite); }
void Start() { MemoryStream ms = new MemoryStream(); ActiveCodeReqPROTO ace = new ActiveCodeReqPROTO(); ace.AccountId = 10000000000000002; ace.ActiveCode = "lyw123456"; MetaDataProtobufSerializer mdps = new MetaDataProtobufSerializer(); mdps.Serialize <ActiveCodeReqPROTO>(ms, ace); //mdps.Serialize(ms, ace); // 这里MemoryStream传入一个buffer, MemoryStream的Capacity大小是buffer的大小。 MemoryStream memoryStream = new MemoryStream(ms.ToArray()); // 这里反序列化传入的Stream的Capacity大小, 必须是buffer的大小。 //ActiveCodeReq activeCodeReq = (ActiveCodeReqPROTO)mdps.Deserialize(memoryStream, typeof(ActiveCodeReqPROTO)); ActiveCodeReqPROTO activeCodeReq = mdps.Deserialize <ActiveCodeReqPROTO>(memoryStream); // 这里打印Error级别的日志, 是为了在手机上可以更好的查看日志。 Debug.LogError(string.Format("AccountId = {0}, ActiveCode = {1}", activeCodeReq.AccountId, activeCodeReq.ActiveCode)); }