using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.Text; /// /// 生成物体同步,是单例 /// public class FunctionSync_CreateObejct : MonoBehaviour { public static FunctionSync_CreateObejct Instance; private st_Motions st = new st_Motions { m_iOperaType = 10008 }; public static Dictionary createDic = new Dictionary(); private bool hasInit; void Start() { Init(); } /// /// 初始化 /// /// public void Init() { if (!GameManage.Instance.is单机模式) { if (!hasInit) { Instance = this; st.area = LoadManage.Instance.currentRoomArea; //发送数据 List tmpbytes = new List(); //syncId tmpbytes.AddRange(BitConverter.GetBytes(LoadManage.Instance.SyncId)); //postion,roate,scale tmpbytes.AddRange(new byte[36]); //string长度 tmpbytes.AddRange(new byte[4]); st.m_sOperaData = tmpbytes.ToArray(); hasInit = true; } } } /// /// 生成物体 /// /// Resource /// position /// eulerAngles /// localScale public void CreateObejct(string path,Vector3 pos,Vector3 roate,Vector3 scale) { if (!createDic.ContainsKey(path)) { createDic.Add(path, Resources.Load(path)); } GameObject obj = Instantiate(createDic[path]); obj.transform.position = pos; obj.transform.eulerAngles = roate; obj.transform.localScale = scale; SendSync(path, pos, roate, scale); } /// /// 回调 /// /// public void CallBack(byte[] data) { Vector3 pos = new Vector3(BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8), BitConverter.ToSingle(data, 12)); Vector3 roate = new Vector3(BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)); Vector3 scale = new Vector3(BitConverter.ToSingle(data, 28), BitConverter.ToSingle(data, 32), BitConverter.ToSingle(data, 36)); int legth = BitConverter.ToInt32(data, 40); string path = Encoding.UTF8.GetString(data, 44,legth); if (!createDic.ContainsKey(path)) { createDic.Add(path, Resources.Load(path)); } GameObject obj = Instantiate(createDic[path]); obj.transform.position = pos; obj.transform.eulerAngles = roate; obj.transform.localScale = scale; } private void SendSync(string path, Vector3 pos, Vector3 roate, Vector3 scale) { byte[] strs=Encoding.UTF8.GetBytes(path); byte[] data = new byte[strs.Length + 44]; //sysid Array.Copy(st.m_sOperaData, 0, data, 0, 4); //pos Array.Copy(BitConverter.GetBytes(pos.x), 0, data, 4, 4); Array.Copy(BitConverter.GetBytes(pos.y), 0, data, 8, 4); Array.Copy(BitConverter.GetBytes(pos.z), 0, data, 12, 4); //roate Array.Copy(BitConverter.GetBytes(roate.x), 0, data, 16, 4); Array.Copy(BitConverter.GetBytes(roate.y), 0, data, 20, 4); Array.Copy(BitConverter.GetBytes(roate.z), 0, data, 24, 4); //scale Array.Copy(BitConverter.GetBytes(scale.x), 0, data, 28, 4); Array.Copy(BitConverter.GetBytes(scale.y), 0, data, 32, 4); Array.Copy(BitConverter.GetBytes(scale.z), 0, data, 36, 4); //string长度 Array.Copy(BitConverter.GetBytes(strs.Length), 0, data, 40, 4); //string Array.Copy(strs, 0, data, 44, strs.Length); st.m_sOperaData = data; LoadManage.Instance.RSclient.Send(st); } }