Last active
May 9, 2024 16:16
-
-
Save rob5300/5f0b2fa48a09ab13d222a7cd949d6275 to your computer and use it in GitHub Desktop.
Example of a PlayableBehaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Ink.Runtime; | |
using System; | |
using UnityEngine; | |
using UnityEngine.Playables; | |
using UnityEngine.Timeline; | |
using Dialogue; | |
[Serializable] | |
public class DialogueTriggerBehaviour : PlayableBehaviour | |
{ | |
public TextAsset DialogueInkStoryFile; | |
public NPCData StartNPCData; | |
public bool JumpToEnd = false; | |
private Story story; | |
private PlayableGraph graph; | |
private Playable thisPlayable; | |
private EventHandler dialogueEndHandler; | |
private bool began = false; | |
public override void OnPlayableCreate(Playable playable) | |
{ | |
graph = playable.GetGraph(); | |
thisPlayable = playable; | |
story = new Story(DialogueInkStoryFile.text); | |
began = false; | |
dialogueEndHandler = OnDialogueEnd; | |
} | |
public override void ProcessFrame(Playable playable, FrameData info, object playerData) | |
{ | |
if (story != null && !began) | |
{ | |
if (DialogueUI.instance) | |
{ | |
//Pause without breaking the current animation states. Work around for 2018.2 | |
graph.GetRootPlayable(0).SetSpeed(0); | |
DialogueUI.instance.OnDialogueEnd += dialogueEndHandler; | |
DialogueUI.instance.BeginNewDialogueCutscene(story, StartNPCData); | |
began = true; | |
} | |
else | |
{ | |
if (JumpToEnd) JumpToEndofPlayable(); | |
graph.GetRootPlayable(0).SetSpeed(1); | |
} | |
} | |
} | |
public void OnDialogueEnd(object sender, EventArgs args) | |
{ | |
DialogueUI.instance.OnDialogueEnd -= dialogueEndHandler; | |
//Unpause | |
graph.GetRootPlayable(0).SetSpeed(1); | |
if (JumpToEnd) JumpToEndofPlayable(); | |
} | |
private void JumpToEndofPlayable() | |
{ | |
graph.GetRootPlayable(0).SetTime(graph.GetRootPlayable(0).GetTime() + thisPlayable.GetDuration()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment