feat: initialize Streamingle Utilities package
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 84ede7a9d3d8a8342a1a4958bbdc19d3
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "Streamingle.Subtitles.Runtime",
|
||||
"rootNamespace": "Streamingle.Subtitles",
|
||||
"references": [
|
||||
"Unity.TextMeshPro",
|
||||
"Unity.Timeline"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6d0f424a65eb1e748b892145abb71fc2
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using UnityEngine.Playables;
|
||||
|
||||
namespace Streamingle.Subtitles
|
||||
{
|
||||
[Serializable]
|
||||
public sealed class SubtitleBehaviour : PlayableBehaviour
|
||||
{
|
||||
public string Text = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f48d9147e7499c4a9c572e2802ca17b
|
||||
@@ -0,0 +1,27 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Playables;
|
||||
using UnityEngine.Timeline;
|
||||
|
||||
namespace Streamingle.Subtitles
|
||||
{
|
||||
public sealed class SubtitleClip : PlayableAsset, ITimelineClipAsset
|
||||
{
|
||||
[SerializeField, TextArea(1, 3)]
|
||||
private string text = string.Empty;
|
||||
|
||||
public string Text
|
||||
{
|
||||
get => text;
|
||||
set => text = value ?? string.Empty;
|
||||
}
|
||||
|
||||
public ClipCaps clipCaps => ClipCaps.None;
|
||||
|
||||
public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
|
||||
{
|
||||
var playable = ScriptPlayable<SubtitleBehaviour>.Create(graph);
|
||||
playable.GetBehaviour().Text = text ?? string.Empty;
|
||||
return playable;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 42cbc317c93c78348ab3c28587916567
|
||||
@@ -0,0 +1,81 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Playables;
|
||||
|
||||
namespace Streamingle.Subtitles
|
||||
{
|
||||
public sealed class SubtitleMixerBehaviour : PlayableBehaviour
|
||||
{
|
||||
private const float MinimumWeight = 0.0001f;
|
||||
|
||||
private TMP_Text trackBinding;
|
||||
private string appliedText;
|
||||
|
||||
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
|
||||
{
|
||||
SetBinding(playerData as TMP_Text);
|
||||
if (trackBinding == null)
|
||||
return;
|
||||
|
||||
var selectedText = string.Empty;
|
||||
var greatestWeight = MinimumWeight;
|
||||
var inputCount = playable.GetInputCount();
|
||||
|
||||
for (var index = 0; index < inputCount; index++)
|
||||
{
|
||||
var inputWeight = playable.GetInputWeight(index) * Mathf.Max(0f, info.effectiveWeight);
|
||||
if (inputWeight <= greatestWeight)
|
||||
continue;
|
||||
|
||||
var inputPlayable = (ScriptPlayable<SubtitleBehaviour>)playable.GetInput(index);
|
||||
selectedText = inputPlayable.GetBehaviour().Text ?? string.Empty;
|
||||
greatestWeight = inputWeight;
|
||||
}
|
||||
|
||||
ApplyText(selectedText);
|
||||
}
|
||||
|
||||
public override void OnGraphStop(Playable playable)
|
||||
{
|
||||
RestoreDefault();
|
||||
}
|
||||
|
||||
public override void OnPlayableDestroy(Playable playable)
|
||||
{
|
||||
RestoreDefault();
|
||||
}
|
||||
|
||||
private void SetBinding(TMP_Text target)
|
||||
{
|
||||
if (target == trackBinding)
|
||||
return;
|
||||
|
||||
RestoreDefault();
|
||||
trackBinding = target;
|
||||
if (trackBinding == null)
|
||||
return;
|
||||
|
||||
appliedText = null;
|
||||
}
|
||||
|
||||
private void ApplyText(string value)
|
||||
{
|
||||
value ??= string.Empty;
|
||||
if (appliedText == value && trackBinding.text == value)
|
||||
return;
|
||||
|
||||
trackBinding.text = value;
|
||||
appliedText = value;
|
||||
}
|
||||
|
||||
private void RestoreDefault()
|
||||
{
|
||||
if (trackBinding == null)
|
||||
return;
|
||||
|
||||
trackBinding.text = string.Empty;
|
||||
trackBinding = null;
|
||||
appliedText = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 66f86280161858943871bd56298c6153
|
||||
@@ -0,0 +1,29 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Playables;
|
||||
using UnityEngine.Timeline;
|
||||
|
||||
namespace Streamingle.Subtitles
|
||||
{
|
||||
[TrackColor(0.16f, 0.72f, 0.86f)]
|
||||
[TrackBindingType(typeof(TMP_Text))]
|
||||
[TrackClipType(typeof(SubtitleClip))]
|
||||
public sealed class SubtitleTrack : TrackAsset
|
||||
{
|
||||
public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
|
||||
{
|
||||
return ScriptPlayable<SubtitleMixerBehaviour>.Create(graph, inputCount);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public override void GatherProperties(PlayableDirector director, IPropertyCollector collector)
|
||||
{
|
||||
var target = director != null ? director.GetGenericBinding(this) as TMP_Text : null;
|
||||
if (target != null)
|
||||
collector.AddFromName<TMP_Text>(target.gameObject, "m_text");
|
||||
|
||||
base.GatherProperties(director, collector);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3577eec94a4b72246bf7e47a771787b2
|
||||
Reference in New Issue
Block a user