/fpsgame/Assets/Photon Unity Networking/Plugins/PhotonNetwork/CustomTypes.cs

https://bitbucket.org/edofrank/unity3d-fpsphotoncloud-beta · C# · 148 lines · 116 code · 23 blank · 9 comment · 2 complexity · 7e803b73ffcd08845a23cb4ba3b54f2c MD5 · raw file

  1. // ----------------------------------------------------------------------------
  2. // <copyright file="CustomTypes.cs" company="Exit Games GmbH">
  3. // PhotonNetwork Framework for Unity - Copyright (C) 2011 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. //
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // ----------------------------------------------------------------------------
  10. using System;
  11. using System.IO;
  12. using ExitGames.Client.Photon;
  13. using UnityEngine;
  14. public static class CustomTypes
  15. {
  16. public static void Register()
  17. {
  18. PhotonPeer.RegisterType(typeof(Vector2), (byte)'W', SerializeVector2, DeserializeVector2);
  19. PhotonPeer.RegisterType(typeof(Vector3), (byte)'V', SerializeVector3, DeserializeVector3);
  20. PhotonPeer.RegisterType(typeof(Transform), (byte)'T', SerializeTransform, DeserializeTransform);
  21. PhotonPeer.RegisterType(typeof(Quaternion), (byte)'Q', SerializeQuaternion, DeserializeQuaternion);
  22. PhotonPeer.RegisterType(typeof(PhotonPlayer), (byte)'P', SerializePhotonPlayer, DeserializePhotonPlayer);
  23. PhotonPeer.RegisterType(typeof(PhotonViewID), (byte)'I', SerializePhotonViewID, DeserializePhotonViewID);
  24. }
  25. #region Custom De/Serializer Methods
  26. private static byte[] SerializeTransform(object customobject)
  27. {
  28. Transform t = (Transform)customobject;
  29. Vector3[] parts = new Vector3[2];
  30. parts[0] = t.position;
  31. parts[1] = t.eulerAngles;
  32. return Protocol.Serialize(parts);
  33. }
  34. private static object DeserializeTransform(byte[] serializedcustomobject)
  35. {
  36. object x = Protocol.Deserialize(serializedcustomobject);
  37. return x;
  38. }
  39. private static byte[] SerializeVector3(object customobject)
  40. {
  41. Vector3 vo = (Vector3)customobject;
  42. MemoryStream ms = new MemoryStream(3 * 4);
  43. ms.Write(BitConverter.GetBytes(vo.x), 0, 4);
  44. ms.Write(BitConverter.GetBytes(vo.y), 0, 4);
  45. ms.Write(BitConverter.GetBytes(vo.z), 0, 4);
  46. return ms.ToArray();
  47. }
  48. private static object DeserializeVector3(byte[] bytes)
  49. {
  50. Vector3 vo = new Vector3();
  51. vo.x = BitConverter.ToSingle(bytes, 0);
  52. vo.y = BitConverter.ToSingle(bytes, 4);
  53. vo.z = BitConverter.ToSingle(bytes, 8);
  54. return vo;
  55. }
  56. private static byte[] SerializeVector2(object customobject)
  57. {
  58. Vector2 vo = (Vector2)customobject;
  59. MemoryStream ms = new MemoryStream(2 * 4);
  60. ms.Write(BitConverter.GetBytes(vo.x), 0, 4);
  61. ms.Write(BitConverter.GetBytes(vo.y), 0, 4);
  62. return ms.ToArray();
  63. }
  64. private static object DeserializeVector2(byte[] bytes)
  65. {
  66. Vector2 vo = new Vector2();
  67. vo.x = BitConverter.ToSingle(bytes, 0);
  68. vo.y = BitConverter.ToSingle(bytes, 4);
  69. return vo;
  70. }
  71. public static byte[] SerializeQuaternion(object obj)
  72. {
  73. Quaternion o = (Quaternion)obj;
  74. MemoryStream ms = new MemoryStream(3 * 4);
  75. ms.Write(BitConverter.GetBytes(o.w), 0, 4);
  76. ms.Write(BitConverter.GetBytes(o.x), 0, 4);
  77. ms.Write(BitConverter.GetBytes(o.y), 0, 4);
  78. ms.Write(BitConverter.GetBytes(o.z), 0, 4);
  79. return ms.ToArray();
  80. }
  81. public static object DeserializeQuaternion(byte[] bytes)
  82. {
  83. Quaternion o = new Quaternion();
  84. o.w = BitConverter.ToSingle(bytes, 0);
  85. o.x = BitConverter.ToSingle(bytes, 4);
  86. o.y = BitConverter.ToSingle(bytes, 8);
  87. o.z = BitConverter.ToSingle(bytes, 12);
  88. return o;
  89. }
  90. private static byte[] SerializePhotonPlayer(object customobject)
  91. {
  92. int ID = ((PhotonPlayer)customobject).ID;
  93. return BitConverter.GetBytes(ID);
  94. }
  95. private static object DeserializePhotonPlayer(byte[] bytes)
  96. {
  97. int ID = BitConverter.ToInt32(bytes, 0);
  98. if(PhotonNetwork.networkingPeer.mActors.ContainsKey(ID))
  99. {
  100. return PhotonNetwork.networkingPeer.mActors[ID];
  101. }
  102. else
  103. {
  104. return null;
  105. }
  106. }
  107. private static byte[] SerializePhotonViewID(object customobject)
  108. {
  109. int ID = ((PhotonViewID)customobject).ID;
  110. return BitConverter.GetBytes(ID);
  111. }
  112. private static object DeserializePhotonViewID(byte[] bytes)
  113. {
  114. int ID = BitConverter.ToInt32(bytes, 0);
  115. int internalID = ID % PhotonNetwork.MAX_VIEW_IDS;
  116. int actorID = ID / PhotonNetwork.MAX_VIEW_IDS;
  117. PhotonPlayer owner = null;
  118. if (actorID > 0)
  119. {
  120. owner = PhotonPlayer.Find(actorID);
  121. }
  122. return new PhotonViewID(internalID, owner);
  123. }
  124. #endregion
  125. }