MultiPlayerManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using LitJson;
  2. using Newtonsoft.Json;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class MultiPlayerManager : MonoSingleton<MultiPlayerManager>
  7. {
  8. public delegate void JoinRoom(SyncObject sycObj);
  9. public delegate void QuitRoom(SyncObject sycObj);
  10. public delegate void SynObject(SyncObject sycObj);
  11. public delegate void SynVideo(SyncVideoData sycVideo);
  12. public JoinRoom OnJoinRoom;
  13. public QuitRoom OnQuitRoom;
  14. public SynObject OnSynObject;
  15. public SynVideo OnSynVideo;
  16. //public Queue<SyncObject> qSyncObject;
  17. //public Queue<SyncVideoData> qSyncVideo;
  18. public Dictionary<string, SyncSpoit> dicSpoitValue;
  19. //public Dictionary<string, SyncVideo> dicVideoValue;
  20. //public List<SyncVideo> listVideoValue;
  21. public Dictionary<string, SynchronizationObject> dicPlayer;
  22. private void Start()
  23. {
  24. dicSpoitValue = new Dictionary<string, SyncSpoit>();
  25. dicPlayer = new Dictionary<string, SynchronizationObject>();
  26. //string test = "\u6587\u65C5\u5C55\u793A\u6F14\u793A";
  27. //Debug.Log("DGJ ====> "+ JsonConvert.DeserializeObject<string>("\"" + test + "\""));
  28. }
  29. public void ReceivedCamPos(string uid, JsonData data)
  30. {
  31. Debug.Log("DGJ ===> ReceivedCamPos " + uid);
  32. SyncObject syncData = new SyncObject();
  33. syncData.id = uid;
  34. syncData.pos = new Vector3(float.Parse(data["x"].ToString()), float.Parse(data["y"].ToString()), float.Parse(data["z"].ToString()));
  35. foreach (var item in dicPlayer)
  36. {
  37. Debug.Log("DGJ dicPlayer =====> " + item.Key);
  38. }
  39. if (!dicPlayer.ContainsKey(uid))
  40. {
  41. Debug.LogError("DGJ ====> " + uid);
  42. // 创建
  43. GameObject player = GameObject.Instantiate(Resources.Load<GameObject>("Template/Prefab/Player"));
  44. SynchronizationObject SyncPlayer = player.GetComponent<SynchronizationObject>();
  45. player.transform.SetParent(GameManager.Instance.Player.transform.parent);
  46. SyncPlayer.uid = uid;
  47. SyncPlayer.InitData(syncData);
  48. dicPlayer.Add(SyncPlayer.uid, SyncPlayer);
  49. }
  50. OnSynObject(syncData);
  51. }
  52. public void ReceivedActiveSp(JsonData data)
  53. {
  54. string spid = JsonConvert.DeserializeObject<string>("\"" + data["spid"].ToString() + "\"");
  55. Debug.Log("DGJ ====> " + spid);
  56. foreach (var item in dicSpoitValue)
  57. {
  58. Debug.Log("DGJ ====> " + item.Key);
  59. }
  60. dicSpoitValue[spid].spoitObj.SetActive(bool.Parse(data["isOpen"].ToString()));
  61. }
  62. public void ReceivedActiveVideo(JsonData data)
  63. {
  64. SyncVideoData syncData = new SyncVideoData();
  65. syncData.spid = JsonConvert.DeserializeObject<string>("\"" + data["spid"].ToString() + "\"");
  66. syncData.id = JsonConvert.DeserializeObject<string>("\"" + data["videoId"].ToString() + "\"");
  67. syncData.isPlay = bool.Parse(data["isplay"].ToString());
  68. syncData.times = float.Parse(data["jindu"].ToString());
  69. OnSynVideo(syncData);
  70. }
  71. }
  72. public class SyncSpoit
  73. {
  74. public string spoitId;
  75. public GameObject spoitObj;
  76. }
  77. public class SyncVideo
  78. {
  79. public string spoitId;
  80. public string videoId;
  81. public AVProVideoPlayer aVProVideo;
  82. }
  83. public class SyncObject
  84. {
  85. public string id { get; set; }
  86. // public bool isSelect { get; set; }
  87. // public bool objAction { get; set; }
  88. public Vector3 pos { get; set; }
  89. // public Vector3 rot { get; set; }
  90. }
  91. public class SyncVideoData
  92. {
  93. public string spid { get; set; }
  94. public string id { get; set; }
  95. public bool isPlay { get; set; }
  96. public float times { get; set; }
  97. }
  98. public enum SyncObjectType
  99. {
  100. Object,
  101. Video,
  102. Player
  103. }