I have a:
- PlayableAsset, called DialogClip. It has a public string and an AudioClip.
- TrackAsset, called DialogTrack.
- PlayableBehaviour, called DialogBehaviour
- PlayableBehaviour, called DialogMixer
Because there is a public audioclip in DialogClip, I’m able to drag an audio clip right onto the timeline - which is awesome.
However, the duration always defaults to 5 seconds. I would like the default duration to be the duration of the audio clip. I would have thought using PlayableExtensions.SetDuration(audioClip.length) would do the trick - but it doesn’t.
How do I set the duration of a clip?
using UnityEngine;
using UnityEngine.Playables;
public class DialogClip : PlayableAsset
{
[TextArea(2, 10)]
public string captionText;
public AudioClip audioClip;
public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
{
var playable = ScriptPlayable<DialogBehaviour>.Create(graph);
DialogBehaviour dialogBehaviour = playable.GetBehaviour();
dialogBehaviour.captionText = captionText;
dialogBehaviour.audioClip = audioClip;
playable.SetDuration(audioClip.length); ///////// this seems to do nothing
return playable;
}
}
using UnityEngine;
using UnityEngine.Playables;
public class DialogBehaviour : PlayableBehaviour
{
public string captionText;
public AudioClip audioClip;
}
using UnityEngine.Playables;
public class DialogMixer : PlayableBehaviour
{
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
ActorManager actor = playerData as ActorManager;
string currentText = "";
float currentAlpha = 0f;
if (!actor) { return; }
int inputCount = playable.GetInputCount();
for (int i = 0; i < inputCount; i++)
{
float inputWeight = playable.GetInputWeight(i);
if (inputWeight > 0f)
{
ScriptPlayable<DialogBehaviour> inputPlayable = (ScriptPlayable<DialogBehaviour>)playable.GetInput(i);
DialogBehaviour dialog = inputPlayable.GetBehaviour();
currentText = dialog.captionText;
currentAlpha = inputWeight;
}
}
actor.speechBubble.captionText.text = currentText;
actor.speechBubble.canvasGroup.alpha = currentAlpha;
}
}
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
[TrackBindingType(typeof(ActorManager))]
[TrackClipType(typeof(DialogClip))]
public class DialogTrack : TrackAsset
{
public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
{
ScriptPlayable<DialogMixer> scriptPlayable = ScriptPlayable<DialogMixer>.Create(graph, inputCount);
return scriptPlayable;
}
}