Skip to content

Instantly share code, notes, and snippets.

@tsubaki tsubaki/Messages.cs
Created Aug 26, 2018

Embed
What would you like to do?
Timelineで字幕(ScriptableObjectに字幕情報を格納する版)
using UnityEngine;
[CreateAssetMenu]
public class Messages : ScriptableObject
{
public string[] texts;
}
using System;
using UnityEngine.Playables;
[Serializable]
public class Subtitle2Behaviour : PlayableBehaviour{}
using System;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
[Serializable]
public class Subtitle2Clip : PlayableAsset, ITimelineClipAsset
{
public ClipCaps clipCaps {
get { return ClipCaps.None; }
}
public override Playable CreatePlayable (PlayableGraph graph, GameObject owner)
{
var playable = ScriptPlayable<Subtitle2Behaviour>.Create (graph);
return playable;
}
}
using UnityEngine.Playables;
public class Subtitle2MixerBehaviour : PlayableBehaviour
{
const string defaultText = "";
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
var m_TrackBinding = playerData as TimelineLabel;
if (m_TrackBinding == null)
return;
int inputCount = playable.GetInputCount ();
string message = defaultText;
int start = 0;
for (int i = start; i < inputCount; i++)
{
float inputWeight = playable.GetInputWeight(i);
if( inputWeight > 0)
{
var inputPlayable = (ScriptPlayable<Subtitle2Behaviour>)playable.GetInput(i);
message = m_TrackBinding.message.texts[i];
break;
}else{
continue;
}
}
m_TrackBinding.label.text = message;
}
}
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using UnityEngine.UI;
[TrackColor(0.8207547f, 0.367791f, 0.5062218f)]
[TrackClipType(typeof(Subtitle2Clip))]
[TrackBindingType(typeof(TimelineLabel))]
public class Subtitle2Track : TrackAsset
{
public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
{
return ScriptPlayable<Subtitle2MixerBehaviour>.Create (graph, inputCount);
}
public override void GatherProperties (PlayableDirector director, IPropertyCollector driver)
{
#if UNITY_EDITOR
var trackBinding = ((TimelineLabel)director.GetGenericBinding(this)).label.gameObject;
if (trackBinding == null)
return;
driver.AddFromName<Text>(trackBinding.gameObject, "m_Text");
#endif
base.GatherProperties (director, driver);
}
}
using UnityEngine;
using UnityEngine.UI;
public class TimelineLabel : MonoBehaviour
{
public Text label;
public Messages message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.