コード例 #1
0
    private void ClientSceneReady(NetworkConnection connection, SceneReadyMessage message)
    {
        Scene?scene = SceneManagerExtensions.GetSceneByPathOrName(message.sceneNameOrPath);

        if (scene == null)
        {
            Debug.LogWarning($"Scene {message.sceneNameOrPath} not loaded on server despite client readying it");
            return;
        }

        PlayerForConnection(connection).MoveToScene(scene.Value);
    }
コード例 #2
0
    private void ClientChangeScene(string sceneNameOrPath, SceneOperation sceneOperation)
    {
        if (clientSceneLoadOperation != null)
        {
            Debug.LogWarning($"Scene load operation already in progress!");
        }

        switch (sceneOperation)
        {
        case SceneOperation.Normal:
            clientSceneLoadOperation = SceneManager.LoadSceneAsync(sceneNameOrPath);
            break;

        case SceneOperation.LoadAdditive:
            if (SceneManagerExtensions.GetSceneByPathOrName(sceneNameOrPath) == null)
            {
                Debug.Log($"Starting to load scene {sceneNameOrPath}");
                clientSceneLoadOperation = SceneManager.LoadSceneAsync(sceneNameOrPath, LoadSceneMode.Additive);
            }
            else
            {
                Debug.Log($"Scene {sceneNameOrPath} already loaded, skipping");
            }

            break;

        case SceneOperation.UnloadAdditive:
            if (SceneManagerExtensions.GetSceneByPathOrName(sceneNameOrPath) != null)
            {
                Debug.Log($"Starting to unload scene {sceneNameOrPath}");
                clientSceneLoadOperation            = SceneManager.UnloadSceneAsync(sceneNameOrPath, UnloadSceneOptions.UnloadAllEmbeddedSceneObjects);
                clientSceneLoadOperation.completed += op => clientSceneLoadOperation = null;
            }
            else
            {
                Debug.Log($"Scene {sceneNameOrPath} already unloaded, skipping");
            }

            // The rest of the logic is really only applicable to loading.
            return;
        }

        if (clientSceneLoadOperation != null)
        {
            clientSceneLoadOperation.allowSceneActivation = false;
        }

        StartCoroutine(NotifyServerWhenSceneReady(sceneNameOrPath));
    }
コード例 #3
0
    private IEnumerator WaitForSceneToLoadThenMovePlayer(string sceneNameOrPath, GameObject player)
    {
        yield return(loadAllScenesCoroutine);

        // This scene should be loaded by the above.
        Scene?scene = SceneManagerExtensions.GetSceneByPathOrName(sceneNameOrPath);

        if (SceneManager.SetActiveScene(scene.Value))
        {
            SceneManager.MoveGameObjectToScene(player, scene.Value);
            Debug.Log($"Moved host player to {sceneNameOrPath}");
        }
        else
        {
            Debug.LogWarning($"Failed to change active scene to {sceneNameOrPath}");
        }
    }
コード例 #4
0
    private IEnumerator WaitForSceneActivationThenSetActive(string sceneNameOrPath)
    {
        if (clientSceneLoadOperation != null)
        {
            clientSceneLoadOperation.allowSceneActivation = true;
            yield return(clientSceneLoadOperation);

            clientSceneLoadOperation = null;
        }

        Scene scene = SceneManagerExtensions.GetSceneByPathOrName(sceneNameOrPath).Value;

        if (!SceneManager.SetActiveScene(scene))
        {
            Debug.LogWarning($"Failed to activate scene {sceneNameOrPath}");
        }

        SceneManager.MoveGameObjectToScene(NetworkClient.connection.identity.gameObject, scene);
    }