/// <summary> /// xml转 protobuf /// </summary> /// <param name="name"></param> private static void XmlToProtoBuf(string name) { if (string.IsNullOrEmpty(name)) { return; } try { Type type = null; foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) { Type tempType = asm.GetType(name); if (tempType != null) { type = tempType; break; } } if (type != null) { string xmlPath = XmlPath + name + ".xml"; string protobufPath = ProtobufPath + name + ".bytes"; object obj = BinarySerializeOpt.XmlDeserialize(xmlPath, type); BinarySerializeOpt.ProtoSerialize(protobufPath, obj); Debug.Log(name + "xml转Protobuf成功,xml转Protobuf成功路径为:" + protobufPath); } } catch { Debug.LogError(name + "xml转二进制失败!"); } }
void Start() { string path = Application.streamingAssetsPath + "/phone.bytes"; myPhone.name = "Apple"; myPhone.price = 7200; BinarySerializeOpt.ProtoSerialize(path, myPhone); Phone loadedPhone = BinarySerializeOpt.ProtoDeSerialize <Phone>(path); Debug.Log("loadedPhone.name " + loadedPhone.name); Debug.Log("loadedPhone.price " + loadedPhone.price); }
public void ResponseData(byte[] data) { LoginData loginData = BinarySerializeOpt.ProtoDeSerialize <LoginData>(data); string savedName = PlayerPrefs.GetString(DataConst.PlayerName); string savedPassword = PlayerPrefs.GetString(DataConst.PlayerPassword); LoginResult result = new LoginResult(); if (!string.IsNullOrEmpty(savedName) && loginData.name == savedName) { //用户存在 if (loginData.password == savedPassword) { //登陆成功 result.result = true; result.reason = "ojbk"; } else { //密码错误 result.result = false; result.reason = "密码错误"; } } else { //用户不存在 result.result = false; result.reason = "该用户暂未注册"; } byte[] buffer = BinarySerializeOpt.ProtoSerialize(result); AppFacade.instance.GetNetworkManager().OnReceiveData(ProtobufID.S_LoginResult, buffer); //Debug.Log("name " + loadedPhone.name); //Debug.Log("password " + loadedPhone.password); }