美文网首页Unity基础入门分享Unity技术分享
Unity NetworkManager异步加载新场景

Unity NetworkManager异步加载新场景

作者: 柚梨柚梨 | 来源:发表于2018-03-08 05:52 被阅读12次

    我最一开始的想法是使用NetworkManager已经实现的NetworkManager.ServerChangeScene()这个函数,它可以在server端异步加载一个场景,并自动让所有该server连接的client都异步加载这个新的场景。但此时game player prefab是不会出现在新场景里的,因为这个函数会将client的状态都设为为准备not-ready,之后需要重新让client调用NetworkClient.Ready()来准备。这样一个好处是我可以保证在player重生进到新的关卡的时候,每个client都是准备好的。

    不过其实,网络游戏异步加载新场景,依然可以用SceneManager.LoadSceneAsync() 这个函数。将所有的player设为DontDestroyOnLoad即可在保持连接的同时让player一直存在在所有的场景中。

    代码逻辑

    这段代码可以直接加在Loading场景的camera上。需要一个进度条和一个Text。UI的逻辑来自 http://blog.csdn.net/yzx5452830/article/details/77646434

    每次需要切换新场景的时候,可以直接加载Loading这个场景,然后会根据sceneList加载下一个场景。稍作修改,也可以通过指定SceneManagerMyGame.nextSceneName来使得Loading这个场景加载下一个制定的场景。

    
    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;
    using UnityEngine.Networking;
    
    // 全局变量
        public class SceneManagerMyGame
        {
            //public static string nextSceneName;
            public static string[] sceneList =
            {
                "scene1",
                "scene2",
            };
            public static int nextSceneID = 0;
        }
    
        public class LoadSceneController : MonoBehaviour
        {
    
            public Slider loadingSlider;
    
            public Text loadingText;
    
            private float loadingSpeed = 1;
    
            private float targetValue;
    
            private AsyncOperation operation;
    
            void Start()
            {
                SceneManagerMyGame.nextSceneID++;
    
                loadingSlider.value = 0.0f;
    
                if (SceneManager.GetActiveScene().name == "Loading")
                {
                    //启动协程    
                    StartCoroutine(AsyncLoading());
                }
            }
    
    
    
            IEnumerator AsyncLoading()
            {
                operation = SceneManager.LoadSceneAsync(SceneManagerTheDistance.sceneList[SceneManagerTheDistance.nextSceneID-1]);
                
    
                //阻止当加载完成自动切换    
                operation.allowSceneActivation = false;
    
                yield return operation;
            }
           
    
            // Update is called once per frame  
            void Update()
            {
                targetValue = operation.progress;
    
                if (operation.progress >= 0.9f)
                {
                    //operation.progress的值最大为0.9  
                    targetValue = 1.0f;
                }
    
                if (targetValue != loadingSlider.value)
                {
                    //插值运算  
                    loadingSlider.value = Mathf.Lerp(loadingSlider.value, targetValue, Time.deltaTime * loadingSpeed);
                    if (Mathf.Abs(loadingSlider.value - targetValue) < 0.01f)
                    {
                        loadingSlider.value = targetValue;
                    }
                }
    
                loadingText.text = ((int)(loadingSlider.value * 100)).ToString() + "%";
    
                if ((int)(loadingSlider.value * 100) == 100)
                {
    
                    //允许异步加载完毕后自动切换场景  
                    operation.allowSceneActivation = true;
                }
            }
        }
    
    

    Unity 官方文档

    You can also change Scenes while the game is active by calling NetworkManager.ServerChangeScene(). This makes all the currently connected clients change Scene too, and updates networkSceneName so that new clients also load the new Scene.

    Clients that connect to this server will automatically switch to this scene. This is called automatically if onlineScene or offlineScene are set, but it can be called from user code to switch scenes again while the game is in progress. This automatically sets clients to be not-ready. The clients must call NetworkClient.Ready() again to participate in the new scene.

    https://docs.unity3d.com/ScriptReference/Networking.NetworkManager.ServerChangeScene.html

    相关文章

      网友评论

        本文标题:Unity NetworkManager异步加载新场景

        本文链接:https://www.haomeiwen.com/subject/kgvrfftx.html