这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

103 行
2.5 KiB

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Localization;
using UnityEngine.Playables;
public class CutsceneManager : MonoBehaviour
{
[SerializeField] private InputReader _inputReader = default;
[SerializeField] private DialogueManager _dialogueManager = default;
[SerializeField] private PlayableDirectorChannelSO _playCutsceneEvent = default;
[SerializeField] public DialogueLineChannelSO _playDialogueEvent = default;
[SerializeField] public VoidEventChannelSO _pauseTimelineEvent = default;
private PlayableDirector _activePlayableDirector;
private bool _isPaused;
bool IsCutscenePlaying => _activePlayableDirector.playableGraph.GetRootPlayable(0).GetSpeed() != 0d;
private void OnEnable()
{
_inputReader.advanceDialogueEvent += OnAdvance;
}
private void OnDisable()
{
_inputReader.advanceDialogueEvent -= OnAdvance;
}
private void Start()
{
if (_playCutsceneEvent != null)
{
_playCutsceneEvent.OnEventRaised += PlayCutscene;
}
if (_playDialogueEvent != null)
{
_playDialogueEvent.OnEventRaised += PlayDialogueFromClip;
}
if (_pauseTimelineEvent != null)
{
_pauseTimelineEvent.OnEventRaised += PauseTimeline;
}
}
void PlayCutscene(PlayableDirector activePlayableDirector)
{
_inputReader.EnableDialogueInput();
_activePlayableDirector = activePlayableDirector;
_isPaused = false;
_activePlayableDirector.Play();
_activePlayableDirector.stopped += HandleDirectorStopped;
}
void CutsceneEnded()
{
if (_activePlayableDirector != null)
_activePlayableDirector.stopped -= HandleDirectorStopped;
_inputReader.EnableGameplayInput();
_dialogueManager.DialogueEndedAndCloseDialogueUI();
}
private void HandleDirectorStopped(PlayableDirector director) => CutsceneEnded();
void PlayDialogueFromClip(LocalizedString dialogueLine, ActorSO actor)
{
_dialogueManager.DisplayDialogueLine(dialogueLine, actor);
}
/// <summary>
/// This callback is executed when the player presses the button to advance dialogues. If the Timeline is currently paused due to a <c>DialogueControlClip</c>, it will resume its playback.
/// </summary>
private void OnAdvance()
{
if (_isPaused)
ResumeTimeline();
}
/// <summary>
/// Called by <c>DialogueControlClip</c> on the Timeline.
/// </summary>
void PauseTimeline()
{
_isPaused = true;
_activePlayableDirector.playableGraph.GetRootPlayable(0).SetSpeed(0);
}
void ResumeTimeline()
{
_isPaused = false;
_activePlayableDirector.playableGraph.GetRootPlayable(0).SetSpeed(1);
}
}