/// <summary> /// 创建场景对象 /// </summary> /// <param name="objInfo"></param> public void CreateObject(int objectIndex, int serverID, ProtoCreateObject sync) { bool isPreSet = objectIndex >= 0; int index = isPreSet ? objectIndex : -objectIndex; if (index < cratePrefabs.Length) { GameObject prefab = cratePrefabs[index]; if (prefab == null) { Debug.LogError("生成AI:无效对象索引" + objectIndex); return; } GameObject go = GameObject.Instantiate(prefab); #if UNITY_EDITOR go.name = "" + serverID; #endif if (!go.activeSelf) { go.SetActive(true); } //获取/初始化配置数据 BaseAI ai = go.GetComponent <BaseAI>(); if (ai != null) { if (Connection.GetInstance().isHost == true) { AppearObjectData objCfg = AICreater.GetObjectCfg(sync.hashCode); if (objCfg != null) { ai.InitAIData(objCfg); } } sceneModel.RegisterAI(ai); } //设置同步组件 SceneGameObject sgo = go.GetComponent <SceneGameObject>(); if (sgo != null) { ProtoPlayerInfo selfInfo = playerModel.GetPlayerInfo(); if (selfInfo != null) { sgo.SetSyncStatus(objectIndex, serverID, true, selfInfo.pos, sync.GetPos(), sync.GetRot()); sceneModel.AddSceneObject(sgo); } } } else { Debug.LogError("生成AI:无效对象索引" + objectIndex); } }
/// <summary> /// 刷新同步数据 /// </summary> public void UpdateSyncData(SyncObject[] updateList) { for (int i = 0; i < updateList.Length; i++) { SyncObject updateObj = updateList[i]; SyncObject srcObj; if (updataList.TryGetValue(updateObj.serverID, out srcObj)) { srcObj.posX = updateObj.posX; srcObj.posY = updateObj.posY; srcObj.posZ = updateObj.posZ; srcObj.rotX = updateObj.rotX; srcObj.rotY = updateObj.rotY; srcObj.rotZ = updateObj.rotZ; srcObj.rotW = updateObj.rotW; } else { if (SyncCreater.instance != null && Connection.GetInstance().isHost == false) { if (updateObj.objectIndex >= 0) { SyncCreater.instance.ActiveObject(updateObj.objectIndex, updateObj.serverID); } else { ProtoCreateObject create = new ProtoCreateObject(); create.objectIndex = updateObj.objectIndex; create.hashCode = -1; create.serverID = updateObj.serverID; create.SetPos(updateObj.GetPos()); create.SetRot(updateObj.GetRot()); SyncCreater.instance.CreateObject(updateObj.objectIndex, updateObj.serverID, create); } } } updateObj.Recycle(); } }
//检查创建对象 void CheckCreate() { if (appearTag != null) { for (int i = 0; i < appearTag.Length; i++) { if (appearTag[i] == false) { AppearSetData apData = levelData.appearSets[i]; if (gameTime >= apData.time) { appearTag[i] = true; if (apData.objectCfgs != null && apData.objectCfgs.Length > 0) { Debug.Log("第" + i + "波"); ProtoCreateObject[] list = new ProtoCreateObject[apData.objectCfgs.Length]; for (int z = 0; z < list.Length; z++) { var objData = apData.objectCfgs[z]; list[z] = new ProtoCreateObject(); list[z].objectIndex = objData.objectIndex; int id = GetCreateID(); list[z].hashCode = id; AddObjectCfg(id, objData); list[z].SetPos(GetPosByRadian(transform.position, objData.XAngle, objData.distance, objData.YAngle)); } var t = new EventObjectArgs(); t.t = list; SceneController.instance.SendNetMsg(ProtoIDCfg.CREATE_OBJECTS, t); } } } } } }