Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if HAVE_SLATE
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using Slate;
- using Framework.Camera;
- using Framework.Support;
- namespace Framework.Cinematics {
- /// <summary>Helper methods for dealing with cutscenes</summary>
- public static class CutsceneHelper {
- #region class CameraParenter
- /// <summary>Temporarily changes the parent of a game object</summary>
- private class CameraParenter : IDisposable {
- /// <summary>Initializes a new instance that does nothing</summary>
- public CameraParenter() {
- this.onCutsceneStoppedDelegate = new Action<Cutscene>(onCutsceneStopped);
- }
- /// <summary>
- /// Assigns a new parent to the camera and remembers the previous one
- /// </summary>
- /// <param name="newParentGameObject">New parent for the camera</param>
- /// <param name="cameraGameObject">Camera that will be reparented</param>
- public CameraParenter(
- GameObject newParentGameObject, GameObject cameraGameObject
- ) : this() {
- Initialize(newParentGameObject, cameraGameObject);
- }
- /// <summary>
- /// Assigns a new parent to the camera and remembers the previous one
- /// </summary>
- /// <param name="newParentGameObject">New parent for the camera</param>
- /// <param name="cameraGameObject">Camera that will be reparented</param>
- public void Initialize(
- GameObject newParentGameObject, GameObject cameraGameObject
- ) {
- this.cameraGameObject = cameraGameObject;
- Transform cameraTransform = cameraGameObject.transform;
- this.previousPosition = cameraTransform.localPosition;
- this.previousOrientation = cameraTransform.localRotation;
- Transform parentTransform = cameraTransform.parent;
- if(parentTransform == null) {
- this.previousParentGameObject = null;
- } else {
- this.previousParentGameObject = parentTransform.gameObject;
- }
- GameObjectHelper.SetAsChild(newParentGameObject, cameraGameObject);
- cameraTransform.localPosition = Vector3.zero;
- cameraTransform.localRotation = Quaternion.identity;
- }
- /// <summary>Restores the original parent of the camera</summary>
- public void Dispose() {
- WatchCutsceneForRecycling(null);
- if(this.cameraGameObject != null) {
- GameObjectHelper.SetAsChild(this.previousParentGameObject, this.cameraGameObject);
- Transform cameraTransform = this.cameraGameObject.transform;
- cameraTransform.localPosition = this.previousPosition;
- cameraTransform.localRotation = this.previousOrientation;
- this.cameraGameObject = null;
- }
- }
- /// <summary>Sets the parenter up for recycling after a cutscene finishes</summary>
- /// <param name="cutscene">Cutscene that will be watched for completion</param>
- public void WatchCutsceneForRecycling(Cutscene cutscene) {
- if(this.cutsceneForUnsubscribe != null) {
- Cutscene.OnCutsceneStopped -= this.onCutsceneStoppedDelegate;
- this.cutsceneForUnsubscribe = null;
- }
- if(cutscene != null) {
- Cutscene.OnCutsceneStopped += this.onCutsceneStoppedDelegate;
- this.cutsceneForUnsubscribe = cutscene;
- }
- }
- /// <summary>Recycles the parenter when the cutscene stops playing</summary>
- /// <param name="cutscene">Cutscene that has stopped playing</param>
- private void onCutsceneStopped(Cutscene cutscene) {
- if(ReferenceEquals(this.cutsceneForUnsubscribe, cutscene)) {
- Dispose();
- CutsceneHelper.recyclableCameraParenters.Add(this);
- }
- }
- /// <summary>Game object that will be reparented</summary>
- private GameObject cameraGameObject;
- /// <summary>Game object that was the camera's previous parent</summary>
- private GameObject previousParentGameObject;
- /// <summary>Previous position of the camera</summary>
- private Vector3 previousPosition;
- /// <summary>Previous orientation of the camera</summary>
- private Quaternion previousOrientation;
- /// <summary>Cutscene this camera parenter is watching for completion</summary>
- private Cutscene cutsceneForUnsubscribe;
- /// <summary>Delegate for the onCutsceneStopped() method</summary>
- private Action<Cutscene> onCutsceneStoppedDelegate;
- }
- #endregion // class CameraParenter
- /// <summary>Starts playing a cutscene using the specified camera</summary>
- /// <param name="cutscene">Cutscene that will be played</param>
- /// <param name="camera">Camera on which the cutscene will be played</param>
- /// <param name="callback">Callback to invoke when the cutscene ends</param>
- public static void PlayCutsceneOnCamera(
- Cutscene cutscene, UnityEngine.Camera camera, System.Action callback = null
- ) {
- // Stop the DirectorCamera from messing with our camera settings
- DirectorCamera.setMainWhenActive = false;
- DirectorCamera.matchMainCamera = False;
- DirectorCamera.autoHandleActiveState = false;
- DirectorCamera.dontDestroyOnLoad = false;
- // Start the cutscene. This will disable the active camera, but we'll undo
- // that little mishap right away
- cutscene.Play(0.0f, callback);
- // Switch to our own render camera
- CameraHelper.ActivateCamera(camera);
- // Parent our render camera to the director camera root
- CameraParenter parenter = recycleOrCreateCameraParenter();
- parenter.Initialize(DirectorCamera.current.gameObject, camera.gameObject);
- // Set up the parenter to recycle (and unparent) when the cutscene ends,
- // then play the cutscene (in this order in case of a zero-length cutscene)
- parenter.WatchCutsceneForRecycling(cutscene);
- }
- /// <summary>Starts playing a cutscene using the director camera</summary>
- /// <param name="cutscene">Cutscene that will be played</param>
- /// <param name="camera">Camera that will be active when the cutscene starts</param>
- /// <param name="callback">Callback to invoke when the cutscene ends</param>
- public static void PlayCutsceneOnDirectorCamera(
- Cutscene cutscene, UnityEngine.Camera camera, System.Action callback = null
- ) {
- // In this case, we want the director camera to take over all camera settings
- DirectorCamera.setMainWhenActive = true;
- DirectorCamera.matchMainCamera = true;
- DirectorCamera.autoHandleActiveState = true;
- DirectorCamera.dontDestroyOnLoad = false;
- // Switch to our own render camera
- CameraHelper.ActivateCamera(camera);
- // Start the cutscene
- cutscene.Play(0.0f, callback);
- }
- /// <summary>
- /// Returns a recycled instance of a camera parenter or creates a new one
- /// </summary>
- /// <returns>An unused instance of a camera parenter</returns>
- private static CameraParenter recycleOrCreateCameraParenter() {
- if(recyclableCameraParenters == null) {
- recyclableCameraParenters = new List<CameraParenter>();
- return new CameraParenter();
- }
- int parenterCount = recyclableCameraParenters.Count;
- if(parenterCount == 0) {
- return new CameraParenter();
- }
- CameraParenter parenter = recyclableCameraParenters[parenterCount - 1];
- recyclableCameraParenters.RemoveAt(parenterCount - 1);
- return parenter;
- }
- /// <summary>Camera parenters that are ready for reuse</summary>
- private static IList<CameraParenter> recyclableCameraParenters;
- }
- } // namespace Framework.Cinematics
- #endif // HAVE_SLATE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement