123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using BeinLab.Util;
- using Newtonsoft.Json;
- using SC.XR.Unity;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class SynchronizeManager : SingletonMono<SynchronizeManager>
- {
- private List<ModelSynchronize> listModel;
- public int modelNume;
- public Queue<string> queueSendData;
- private void Start()
- {
- //GameObject modelc =GameObject.Instantiate( Resources.Load<GameObject>("ModelC"));
- //GameObject model = GameObject.Instantiate(Resources.Load<GameObject>("diqiu"));
- //modelc.GetComponent<ModelC>().AddModel(model.transform);
-
- queueSendData = new Queue<string>();
- TimerMgr.Instance.CreateTimer(()=> { SynModel(); }, 0.2f, -1);
- modelNume = 1;
- }
- public void RecMessage( MessageData data )
- {
- switch (data.msgType)
- {
- case "CreModel":
- GameObject modelc = GameObject.Instantiate(Resources.Load<GameObject>("ModelC"));
- GameObject model = GameObject.Instantiate(Resources.Load<GameObject>(data.CreateModel.name));
- modelc.GetComponent<ModelC>().AddModel(model.transform);
- ModelSynchronize modelSync = modelc.GetComponent<ModelSynchronize>();
- modelSync.Init(data.CreateModel);
-
- modelNume++;
- break;
- case "DesModel":
- for (int i = 0; i < listModel.Count; i++)
- {
- if(listModel[i].id == data.DesModelId)
- {
- listModel[i].gameObject.SetActive(false);
- listModel.RemoveAt(i);
- break;
- }
- }
- break;
- case "SynchModel":
- for (int i = 0; i < data.listModelData.Count; i++)
- {
- Debug.Log(data.listModelData[i].id);
- MsgHandler.SendMsg(data.listModelData[i].id, data.listModelData[i]);
- }
- //Debug.Log("SynchModel");
- //if (data.listModelData != null&& listModel!=null)
- //for (int i = 0; i < listModel.Count; i++)
- //{
- // for (int j = 0; j < data.listModelData.Count; j++)
- // {
-
- // if(listModel[i].id == data.listModelData[j].id)
- // {
- // Debug.Log(data.listModelData[j].id);
- // listModel[i].SetData(data.listModelData[j]);
- // }
- // }
- //}
- break;
- default:
- break;
- }
- }
- public void SynModel()
- {
- List<ModelData> listData = new List<ModelData>();
- for (int i = 0; i < listModel.Count; i++)
- {
- if (listModel[i].isSyn&&listModel[i].isOwnDo||listModel[i].isPlayer)
- {
- listData.Add(listModel[i].GetData());
- //if (!listModel[i].isPlayer)
- // listModel[i].isSyn = false;
- }
- }
- MessageData MsgData = new MessageData("SynchModel", listData, null, null);
- string Message = JsonConvert.SerializeObject(MsgData);
- Debug.Log(Message);
- AddMsgData(true, Message);
- }
- public void CreateModel(ModelData data)
- {
- MessageData MsgData = new MessageData("CreModel", null, data, null);
- string Message = JsonConvert.SerializeObject(MsgData);
- AddMsgData(false,Message);
- }
- public void DesModel(string modelId)
- {
- for (int i = 0; i < listModel.Count; i++)
- {
- if (listModel[i].id == modelId)
- {
- listModel[i].gameObject.SetActive(false);
- listModel.RemoveAt(i);
- MessageData MsgData = new MessageData("DesModel", null, null, modelId);
- string Message = JsonConvert.SerializeObject(MsgData);
- AddMsgData(false, Message);
- break;
- }
- }
- }
- private void AddMsgData(bool isSyn, string Message)
- {
- // Debug.Log("AddMsgData " + Message);
- NetWorkLANManager.Instance.AddMsgData(isSyn, Message);
- // queueSendData.Enqueue(Message);
- }
- public void AddModelSynchronize(ModelSynchronize modelSync)
- {
- if (listModel == null)
- listModel = new List<ModelSynchronize>();
- listModel.Add(modelSync);
- for (int i = 0; i < listModel.Count; i++)
- {
- Debug.Log(listModel[i].id);
- }
- }
- public void SendData()
- {
- if (queueSendData.Count < 1)
- return;
- string msg = queueSendData.Dequeue();
- NetWorkLANManager.Instance.SendData(msg);
- }
-
-
- }
|