public async Task<SceneController> CreateScene(SceneData sceneData, int difficultyLevel = 1) { // 新建场景 GameObject prefab = await AssetManager.LoadAssetAsync<GameObject>("Assets/Resources_moved/Prefabs/Scenes/" + sceneData.rawSceneId+".prefab"); Debug.Assert(prefab != null, "找不到场景预制体:" + "Prefabs / Scenes / " + sceneData.rawSceneId); GameObject scene = Instantiate(prefab); // Debug.Log(" 预制体id " + sceneData.rawSceneId); scene.transform.parent = SceneRoot.transform; scene.transform.localPosition = GetOnlyScenePosition(); scene.name = sceneData.sceneId; SceneController sceneController = scene.GetComponent<SceneController>(); sceneController.SceneData = sceneData; { // 添加怪物 add by TangJian 2017/08/03 21:35:51 { foreach (var roleData in sceneData.roleDatas) { roleData.locationData.position = sceneController.GetAreaRandomPosition(roleData.locationData.areaId); roleData.difficultyLevel = difficultyLevel; sceneController.AddRole(roleData); } } // 添加物品 add by TangJian 2017/08/22 19:45:51 { foreach (var itemData in sceneData.itemDatas) { var item = GameObjectManager.Instance.Create(itemData.id); DropItemController dropItemController = item.GetComponent<DropItemController>(); itemData.position = sceneController.GetAreaRandomPosition("MonsterAreas"); sceneController.DropItemEnterWithLocalPosition(dropItemController, itemData.position); } } // 添加放置物体 add by TangJian 2018/01/23 15:52:08 { foreach (var placementData in sceneData.PlacementDatas) { var placementObject = GameObjectManager.Instance.Create(placementData.id); SceneObjectController sceneObjectController = placementObject.GetComponent<SceneObjectController>(); PlacementController treasureBoxController = placementObject.GetComponent<PlacementController>(); treasureBoxController.Placementdata = placementData; placementData.position = sceneController.GetAreaRandomPosition("MonsterAreas"); sceneController.PlacementEnterWithLocalPosition(sceneObjectController, placementData.position); ScenePlacementComponent sceneComponent = treasureBoxController.gameObject.GetComponent<ScenePlacementComponent>(); if (sceneComponent != null) { sceneComponent.GridPos = placementObject.transform.localPosition; } } } // 设置门数据 add by TangJian 2018/12/26 16:00 for (int i = 0; i < sceneData.portalDatas.Count; i++) { PortalData portalData = sceneData.portalDatas[i]; Transform portalTransform = scene.transform.Find("Portals/" + portalData.id); Debug.Assert(portalTransform != null, "找不到门: " + sceneData.sceneId + ":" + portalData.id); PortalController portalController = portalTransform.GetComponent<PortalController>(); portalController.PortalData = portalData; portalController.State = 1; } // 为丢失NavMeshDataBuild { foreach(var item in scene.GetComponents<UnityEngine.AI.NavMeshSurface>()) { if (item.navMeshData == null) { item.BuildNavMesh(); } } } } // 初始化 // sceneController.InitData(); return sceneController; }
public IEnumerator LoadLevelConfig(LevelConfig levelConfig) { ClearLoadedScene(); List<SceneData> sceneDataList = new List<SceneData>(); PathConfig currPath = levelConfig.currPath; Debug.Assert(currPath != null); // 创建场景数据 add by TangJian 2018/12/26 15:31 foreach (var roomData in levelConfig.rooms) { SceneData sceneData = new SceneData(); sceneData.sceneId = roomData.id; sceneData.rawSceneId = roomData.RawId; sceneDataList.Add(sceneData); } // 创建场景连接 add by TangJian 2018/12/26 15:32 foreach (var connectionData in currPath.connections) { string fromSceneId = connectionData.fromScene; string fromPortalId = connectionData.fromPortal; string toSceneId = connectionData.toScene; string toPortalId = connectionData.toPortal; SceneData fromSceneData = sceneDataList.Find((SceneData sceneData_) => { return fromSceneId == sceneData_.sceneId; }); SceneData toSceneData = sceneDataList.Find((SceneData sceneData_) => { return toSceneId == sceneData_.sceneId; }); Debug.Assert(fromSceneData!= null, "找不到场景" + fromSceneId); Debug.Assert(toSceneData!= null, "找不到场景" + toSceneId); // 添加门 add by TangJian 2018/12/26 15:42 { PortalData fromPortalData = new PortalData(); fromPortalData.id = fromPortalId; fromPortalData.sceneId = fromSceneId; fromPortalData.toSceneId = toSceneId; fromPortalData.toPortalId = toPortalId; fromSceneData.portalDatas.Add(fromPortalData); PortalData toPortalData = new PortalData(); toPortalData.id = toPortalId; toPortalData.sceneId = toSceneId; toPortalData.toSceneId = fromSceneId; toPortalData.toPortalId = fromPortalId; toSceneData.portalDatas.Add(toPortalData); } } // 向场景数据中添加怪物数据 add by TangJian 2018/12/27 12:42 foreach (var objectData in levelConfig.objectses) { RoomObjectesConfig newRoomObjects = objectData; SceneData sceneData = sceneDataList.Find((SceneData sceneData_) => { return newRoomObjects.roomId == sceneData_.sceneId; }); Debug.Assert(sceneData!=null, "找不到SceneData: " + newRoomObjects.roomId); for (int j = 0; j < newRoomObjects.objects.Count; j++) { RoomObjectConfig newRoomObject = newRoomObjects.objects[j]; for (int k = 0; k < newRoomObject.count; k++) { RoleData roleData = new RoleData(); roleData.Id = newRoomObject.objectId; roleData.Prefab = newRoomObject.objectId; roleData.TeamId = "2"; roleData.locationData.areaId = newRoomObject.areaId; sceneData.roleDatas.Add(roleData); } } } // 设置场景事件 add by TangJian 2019/1/2 14:49 InitSceneEvent(levelConfig.sceneEvents); int step = 0; int stepCount = sceneDataList.Count; // 创建场景 add by TangJian 2018/12/26 15:48 for (int i = 0; i < sceneDataList.Count; i++) { SceneData sceneData = sceneDataList[i]; var handle = CreateSceneAsync(sceneData, levelConfig.difficultyLevel); while (handle.IsCompleted == false) { yield return (float)(step) / stepCount; } SceneController scene = handle.Result; // SceneController scene = CreateScene(sceneData, levelConfig.difficultyLevel); scenes.Add(sceneData.sceneId, scene); scene.gameObject.SetActive(false); yield return (float)(++step) / stepCount; } }