SHARE
TWEET

Untitled

a guest Nov 19th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Playables;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public class CutscenePlayer : MonoBehaviour
  8. {
  9.     [SerializeField]
  10.     private PlayableDirector _cutsceneToPlay;
  11.  
  12.     IEnumerator LoadSceneAfterDelay()
  13.     {
  14.         yield return new WaitForSeconds(5.0f);
  15.         SceneManager.LoadSceneAsync(1, LoadSceneMode.Additive);
  16.     }
  17.  
  18.     private void SceneUnloaded(Scene arg0)
  19.     {
  20.         StartCoroutine(LoadSceneAfterDelay());
  21.     }
  22.  
  23.     IEnumerator RunAtEndOfFrame(System.Action action)
  24.     {
  25.         yield return new WaitForEndOfFrame();
  26.         action();
  27.     }
  28.  
  29.     private void SceneLoaded(Scene scene, LoadSceneMode mode)
  30.     {
  31.         if (!scene.name.Equals("Cutscene"))
  32.             return;
  33.  
  34.         StartCoroutine(RunAtEndOfFrame(
  35.         () =>
  36.         {
  37.             _cutsceneToPlay?.Play();
  38.             System.Action<PlayableDirector> extraAction = null;
  39.  
  40.             extraAction = (_) =>
  41.             {
  42.                 _cutsceneToPlay.stopped -= extraAction;
  43.                 SceneManager.UnloadSceneAsync(1);
  44.             };
  45.  
  46.             _cutsceneToPlay.stopped += extraAction;
  47.         }));
  48.     }
  49.  
  50.     void Start()
  51.     {
  52.         StartCoroutine(LoadSceneAfterDelay());
  53.     }
  54.  
  55.     private void OnEnable()
  56.     {
  57.         SceneManager.sceneLoaded += SceneLoaded;
  58.         SceneManager.sceneUnloaded += SceneUnloaded;
  59.     }
  60.  
  61.     private void OnDisable()
  62.     {
  63.         SceneManager.sceneLoaded -= SceneLoaded;
  64.         SceneManager.sceneUnloaded -= SceneUnloaded;
  65.     }
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top