Пример #1
0
    private IEnumerator loadLevelSegment(LevelSegmentProperties segmentProps)
    {
        //check if there is already an segment on this position
        if (m_usedPositions.Contains(segmentProps.Position))
        {
            //if yes then mark it as needed
            m_loadedLevelSegments[m_usedPositions.IndexOf(segmentProps.Position)].IsNeeded = true;
            yield break;
        }

        //if there isnt an existing segment on this position then load the related scene
        string         sceneName = m_levelSegmentMapping[segmentProps.Type].name;
        AsyncOperation async     = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);

        if (!async.isDone)
        {
            yield return(new WaitForEndOfFrame());
        }
        Scene loadedScene = SceneManager.GetSceneByName(sceneName);

        while (!loadedScene.isLoaded)
        {
            yield return(new WaitForEndOfFrame());
        }

        //create a new scene to ensure correct scene unloading
        string newSceneName = sceneName + m_levelSegmentTypeCounter[segmentProps.Type]++;
        Scene  newScene     = SceneManager.CreateScene(newSceneName);

        SceneManager.MergeScenes(loadedScene, newScene);
        m_gameSystem.AddAdditionalMainScene(newScene);

        //get segment and set its properties
        LevelSegment segment = newScene.GetRootGameObjects()[0].GetComponent <LevelSegment>();

        m_usedPositions.Add(segmentProps.Position);
        m_loadedLevelSegments.Add(segment);
        segment.transform.position   = segmentProps.Position;
        segment.transform.rotation   = segmentProps.Rotation;
        segment.IsNeeded             = true;
        segment.OnTriggerEnterAction = (LevelSegment self) =>
        {
            StartCoroutine(loadLevelSegments(self,
                                             segmentProps.DoorMapping.Values.ToArray()));
        };
        segment.gameObject.SetActive(true);
    }