112 lines
4.1 KiB
C#
112 lines
4.1 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using System;
|
||
using System.Text;
|
||
|
||
/// <summary>
|
||
/// 生成物体同步,是单例
|
||
/// </summary>
|
||
public class FunctionSync_CreateObejct : MonoBehaviour
|
||
{
|
||
public static FunctionSync_CreateObejct Instance;
|
||
private st_Motions st = new st_Motions { m_iOperaType = 10008 };
|
||
public static Dictionary<string, GameObject> createDic = new Dictionary<string, GameObject>();
|
||
private bool hasInit;
|
||
void Start()
|
||
{
|
||
Init();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化
|
||
/// </summary>
|
||
/// <param name="callback"></param>
|
||
public void Init()
|
||
{
|
||
if (!GameManage.Instance.is单机模式)
|
||
{
|
||
if (!hasInit)
|
||
{
|
||
Instance = this;
|
||
st.area = LoadManage.Instance.currentRoomArea;
|
||
//发送数据
|
||
List<byte> tmpbytes = new List<byte>();
|
||
//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;
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 生成物体
|
||
/// </summary>
|
||
/// <param name="path">Resource</param>
|
||
/// <param name="pos">position</param>
|
||
/// <param name="roate">eulerAngles</param>
|
||
/// <param name="scale">localScale</param>
|
||
public void CreateObejct(string path,Vector3 pos,Vector3 roate,Vector3 scale)
|
||
{
|
||
if (!createDic.ContainsKey(path))
|
||
{
|
||
createDic.Add(path, Resources.Load<GameObject>(path));
|
||
}
|
||
GameObject obj = Instantiate<GameObject>(createDic[path]);
|
||
obj.transform.position = pos;
|
||
obj.transform.eulerAngles = roate;
|
||
obj.transform.localScale = scale;
|
||
SendSync(path, pos, roate, scale);
|
||
}
|
||
/// <summary>
|
||
/// 回调
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
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<GameObject>(path));
|
||
}
|
||
GameObject obj = Instantiate<GameObject>(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);
|
||
}
|
||
}
|