public TaskProgress LoadScene(string sceneName, bool isAdditive, LoadScenesHandler callback)
 {
     return(LoadScenes(new List <SceneInfo>()
     {
         new SceneInfo()
         {
             DisplayName = sceneName, Name = sceneName
         }
     }, isAdditive, callback));
 }
        public TaskProgress LoadScenes(List <SceneInfo> Scenes, bool isAdditive, LoadScenesHandler callback)
        {
            Contract.ArgumentNotNull("Scenes", Scenes);
            Contract.ArgumentNotNull("callback", callback);

            TaskProgress progress = new TaskProgress();

            StartCoroutine(LoadScenesAsync(Scenes, isAdditive, progress, callback));

            return(progress);
        }
        private IEnumerator LoadScenesAsync(List <SceneInfo> Scenes, bool isAdditive, TaskProgress progress, LoadScenesHandler callback)
        {
            // Release the process to allow caller to attached to Updated event to monitor progress
            yield return(new WaitForEndOfFrame());

            string error = null;

            try
            {
                for (int index = 0; index < Scenes.Count && !progress.IsCancelled; ++index)
                {
                    float  percentComplete = (100 * index / Scenes.Count);
                    string message         = Scenes[index].DisplayName;
                    progress.Update(percentComplete, message);

                    // Release the process to update the UI
                    yield return(new WaitForSeconds(1.0f));

                    AsyncOperation asyncOp;
                    if (!isAdditive)
                    {
                        if (Application.loadedLevelName.CompareTo(Scenes[index].Name) != 0)
                        {
                            // Add the remaing scenes
                            asyncOp = Application.LoadLevelAsync(Scenes[index].Name);
                        }
                        else
                        {
                            continue;
                        }
                        isAdditive = true;
                    }
                    else
                    {
                        // Load the first scene
                        asyncOp = Application.LoadLevelAdditiveAsync(Scenes[index].Name);
                    }

                    while (!asyncOp.isDone && !progress.IsCancelled)
                    {
                        progress.Update(((asyncOp.progress / Scenes.Count) + (index / Scenes.Count)) * 100);
                        yield return(new WaitForEndOfFrame());
                    }
                }

                // Release to allow Awake() methods to be called on newly loaded game objects
                yield return(new WaitForSeconds(2.0f));
            }
            finally
            {
                progress.Finished();
                if (callback != null)
                {
                    callback(error == null, error);
                }
            }
        }