MultiPlayerManager.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. UnityEngine.Object @object = ResMgr.Instance.Load("Player");
  44. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(@object as GameObject);
  45. GameObject player = GameObject.Instantiate(gameObject);
  46. SynchronizationObject SyncPlayer = player.GetComponent<SynchronizationObject>();
  47. player.transform.SetParent(GameManager.Instance.Player.transform.parent);
  48. SyncPlayer.uid = uid;
  49. SyncPlayer.InitData(syncData);
  50. dicPlayer.Add(SyncPlayer.uid, SyncPlayer);
  51. }
  52. OnSynObject(syncData);
  53. }
  54. public void ReceivedActiveSp(JsonData data)
  55. {
  56. string spid = JsonConvert.DeserializeObject<string>("\"" + data["spid"].ToString() + "\"");
  57. Debug.Log("DGJ ====> " + spid);
  58. foreach (var item in dicSpoitValue)
  59. {
  60. Debug.Log("DGJ ====> " + item.Key);
  61. }
  62. dicSpoitValue[spid].spoitObj.SetActive(bool.Parse(data["isOpen"].ToString()));
  63. }
  64. public void ReceivedActiveVideo(JsonData data)
  65. {
  66. SyncVideoData syncData = new SyncVideoData();
  67. syncData.spid = JsonConvert.DeserializeObject<string>("\"" + data["spid"].ToString() + "\"");
  68. syncData.id = JsonConvert.DeserializeObject<string>("\"" + data["videoId"].ToString() + "\"");
  69. syncData.isPlay = bool.Parse(data["isplay"].ToString());
  70. syncData.times = float.Parse(data["jindu"].ToString());
  71. OnSynVideo(syncData);
  72. }
  73. }
  74. public class SyncSpoit
  75. {
  76. public string spoitId;
  77. public GameObject spoitObj;
  78. }
  79. public class SyncVideo
  80. {
  81. public string spoitId;
  82. public string videoId;
  83. public AVProVideoPlayer aVProVideo;
  84. }
  85. public class SyncObject
  86. {
  87. public string id { get; set; }
  88. // public bool isSelect { get; set; }
  89. // public bool objAction { get; set; }
  90. public Vector3 pos { get; set; }
  91. // public Vector3 rot { get; set; }
  92. }
  93. public class SyncVideoData
  94. {
  95. public string spid { get; set; }
  96. public string id { get; set; }
  97. public bool isPlay { get; set; }
  98. public float times { get; set; }
  99. }
  100. public enum SyncObjectType
  101. {
  102. Object,
  103. Video,
  104. Player
  105. }