Ejemplo n.º 1
0
        static IAsyncOperation LoadAdditively(string sceneName, int buildIndex, Action <SceneLoadOperationArgs> loadCompleted = null)
        {
            if (buildIndex != -1)
            {
                // Our loaded / load requests scenes cache is using string key
                // so we need to make sceneName from index just to make sure our cache will work.
                sceneName = buildIndex.ToString();
            }

            if (TryGetLoadedScene(sceneName, out var loadedScene))
            {
                loadCompleted?.Invoke(new SceneLoadOperationArgs {
                    Scene  = loadedScene,
                    Status = OperationStatus.Success
                });
                return(s_LoadSceneOperations[sceneName]);
            }

            if (!s_LoadSceneRequests.ContainsKey(sceneName))
            {
                var callbacks = new List <Action <SceneLoadOperationArgs> >();
                if (loadCompleted != null)
                {
                    callbacks.Add(loadCompleted);
                }

                var loadAsyncOperation = buildIndex != -1
                    ? SceneManager.LoadSceneAsync(buildIndex, LoadSceneMode.Additive)
                    : SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);

                var asyncWrapper = new AsyncOperationWrapper(sceneName, loadAsyncOperation);

                s_LoadSceneRequests.Add(sceneName, callbacks);
                s_LoadSceneOperations.Add(sceneName, asyncWrapper);
                return(asyncWrapper);
            }

            if (loadCompleted != null)
            {
                var callbacks = s_LoadSceneRequests[sceneName] ?? new List <Action <SceneLoadOperationArgs> >();
                callbacks.Add(loadCompleted);
                s_LoadSceneRequests[sceneName] = callbacks;
            }

            return(s_LoadSceneOperations[sceneName]);
        }
Ejemplo n.º 2
0
        static IAsyncOperation LoadAdditively(string sceneName, int buildIndex, Action <Scene> loadCompleted = null)
        {
            if (buildIndex != -1)
            {
                sceneName = buildIndex.ToString();
            }

            if (TryGetLoadedScene(sceneName, out var loadedScene))
            {
                loadCompleted?.Invoke(loadedScene);
                return(s_LoadSceneOperations[sceneName]);
            }
            if (!s_LoadSceneRequests.ContainsKey(sceneName))
            {
                var callbacks = new List <Action <Scene> >();
                if (loadCompleted != null)
                {
                    callbacks.Add(loadCompleted);
                }

                var loadAsyncOperation = buildIndex != -1
                    ? SceneManager.LoadSceneAsync(buildIndex, LoadSceneMode.Additive)
                    : SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);

                var asyncWrapper = new AsyncOperationWrapper(loadAsyncOperation);

                s_LoadSceneRequests.Add(sceneName, callbacks);
                s_LoadSceneOperations.Add(sceneName, asyncWrapper);
                return(asyncWrapper);
            }

            if (loadCompleted != null)
            {
                var callbacks = s_LoadSceneRequests[sceneName] ?? new List <Action <Scene> >();
                callbacks.Add(loadCompleted);
                s_LoadSceneRequests[sceneName] = callbacks;
            }

            return(s_LoadSceneOperations[sceneName]);
        }