SynchronizeManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using BeinLab.Util;
  2. using Newtonsoft.Json;
  3. using SC.XR.Unity;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. public class SynchronizeManager : SingletonMono<SynchronizeManager>
  8. {
  9. private List<ModelSynchronize> listModel;
  10. public int modelNume;
  11. public Queue<string> queueSendData;
  12. private void Start()
  13. {
  14. //GameObject modelc =GameObject.Instantiate( Resources.Load<GameObject>("ModelC"));
  15. //GameObject model = GameObject.Instantiate(Resources.Load<GameObject>("diqiu"));
  16. //modelc.GetComponent<ModelC>().AddModel(model.transform);
  17. queueSendData = new Queue<string>();
  18. TimerMgr.Instance.CreateTimer(()=> { SynModel(); }, 0.2f, -1);
  19. modelNume = 1;
  20. }
  21. public void RecMessage( MessageData data )
  22. {
  23. switch (data.msgType)
  24. {
  25. case "CreModel":
  26. GameObject modelc = GameObject.Instantiate(Resources.Load<GameObject>("ModelC"));
  27. GameObject model = GameObject.Instantiate(Resources.Load<GameObject>(data.CreateModel.name));
  28. modelc.GetComponent<ModelC>().AddModel(model.transform);
  29. ModelSynchronize modelSync = modelc.GetComponent<ModelSynchronize>();
  30. modelSync.Init(data.CreateModel);
  31. modelNume++;
  32. break;
  33. case "DesModel":
  34. for (int i = 0; i < listModel.Count; i++)
  35. {
  36. if(listModel[i].id == data.DesModelId)
  37. {
  38. listModel[i].gameObject.SetActive(false);
  39. listModel.RemoveAt(i);
  40. break;
  41. }
  42. }
  43. break;
  44. case "SynchModel":
  45. for (int i = 0; i < data.listModelData.Count; i++)
  46. {
  47. Debug.Log(data.listModelData[i].id);
  48. MsgHandler.SendMsg(data.listModelData[i].id, data.listModelData[i]);
  49. }
  50. //Debug.Log("SynchModel");
  51. //if (data.listModelData != null&& listModel!=null)
  52. //for (int i = 0; i < listModel.Count; i++)
  53. //{
  54. // for (int j = 0; j < data.listModelData.Count; j++)
  55. // {
  56. // if(listModel[i].id == data.listModelData[j].id)
  57. // {
  58. // Debug.Log(data.listModelData[j].id);
  59. // listModel[i].SetData(data.listModelData[j]);
  60. // }
  61. // }
  62. //}
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. public void SynModel()
  69. {
  70. List<ModelData> listData = new List<ModelData>();
  71. for (int i = 0; i < listModel.Count; i++)
  72. {
  73. if (listModel[i].isSyn&&listModel[i].isOwnDo||listModel[i].isPlayer)
  74. {
  75. listData.Add(listModel[i].GetData());
  76. //if (!listModel[i].isPlayer)
  77. // listModel[i].isSyn = false;
  78. }
  79. }
  80. MessageData MsgData = new MessageData("SynchModel", listData, null, null);
  81. string Message = JsonConvert.SerializeObject(MsgData);
  82. Debug.Log(Message);
  83. AddMsgData(true, Message);
  84. }
  85. public void CreateModel(ModelData data)
  86. {
  87. MessageData MsgData = new MessageData("CreModel", null, data, null);
  88. string Message = JsonConvert.SerializeObject(MsgData);
  89. AddMsgData(false,Message);
  90. }
  91. public void DesModel(string modelId)
  92. {
  93. for (int i = 0; i < listModel.Count; i++)
  94. {
  95. if (listModel[i].id == modelId)
  96. {
  97. listModel[i].gameObject.SetActive(false);
  98. listModel.RemoveAt(i);
  99. MessageData MsgData = new MessageData("DesModel", null, null, modelId);
  100. string Message = JsonConvert.SerializeObject(MsgData);
  101. AddMsgData(false, Message);
  102. break;
  103. }
  104. }
  105. }
  106. private void AddMsgData(bool isSyn, string Message)
  107. {
  108. // Debug.Log("AddMsgData " + Message);
  109. NetWorkLANManager.Instance.AddMsgData(isSyn, Message);
  110. // queueSendData.Enqueue(Message);
  111. }
  112. public void AddModelSynchronize(ModelSynchronize modelSync)
  113. {
  114. if (listModel == null)
  115. listModel = new List<ModelSynchronize>();
  116. listModel.Add(modelSync);
  117. for (int i = 0; i < listModel.Count; i++)
  118. {
  119. Debug.Log(listModel[i].id);
  120. }
  121. }
  122. public void SendData()
  123. {
  124. if (queueSendData.Count < 1)
  125. return;
  126. string msg = queueSendData.Dequeue();
  127. NetWorkLANManager.Instance.SendData(msg);
  128. }
  129. }