Last active
July 7, 2020 05:38
-
-
Save q8f13/1648f046fd630baab1795c2f629bf655 to your computer and use it in GitHub Desktop.
把Unity Timeline中使用activation track的text组件中的文字解出来。统一输出到一个外部text file
This file contains 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 System; | |
using System.Collections.Generic; | |
using System.IO; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.Playables; | |
using UnityEngine.Timeline; | |
using UnityEngine.UI; | |
/// <summary> | |
/// Extract text in timeline(activation track) | |
/// and export to external text file | |
/// </summary> | |
public class SubtitleExtractor : Editor | |
{ | |
// [SerializeField] | |
// PlayableDirector _pd; | |
[MenuItem("Tools/Extract subtitles")] | |
public static void GenerateSubtitles() | |
{ | |
// TimelineAsset ta = _pd.playableAsset as TimelineAsset; | |
PlayableDirector director = Selection.activeGameObject.GetComponent<PlayableDirector>(); | |
if(director == null) | |
{ | |
EditorUtility.DisplayDialog(null, "curr selection is not a timeline playableDirector", "ok", "cancel"); | |
return; | |
} | |
var obj = new SerializedObject(director); | |
var bindings = obj.FindProperty("m_SceneBindings"); | |
List<Subtitle> s_list = new List<Subtitle>(); | |
for(int i=0;i<bindings.arraySize;i++) | |
{ | |
var binding = bindings.GetArrayElementAtIndex(i); | |
var trackProp = binding.FindPropertyRelative("key"); | |
var sceneObjProp = binding.FindPropertyRelative("value"); | |
var track = trackProp.objectReferenceValue; | |
var sceneObj = sceneObjProp.objectReferenceValue; | |
// Debug.LogFormat("Binding {0} {1}", track != null ? track.name : "Null", sceneObj != null ? sceneObj.name : "Null"); | |
if(sceneObj is GameObject) | |
{ | |
GameObject g = sceneObj as GameObject; | |
Text t = g.GetComponent<Text>(); | |
if(t == null || t.enabled == false) | |
continue; | |
if(track is ActivationTrack) | |
{ | |
ActivationTrack at = track as ActivationTrack; | |
s_list.Add(new Subtitle(at.start, at.end, t.text)); | |
} | |
// Debug.Log((sceneObj as GameObject).name); | |
} | |
} | |
s_list.Sort(); | |
StreamWriter sw = File.CreateText("Assets/subtitles.txt"); | |
for(int i=0;i<s_list.Count;i++) | |
{ | |
// s_all += s_list[i].Print(); | |
// Debug.Log(s_list[i].Print()); | |
sw.WriteLine(s_list[i].Print()); | |
} | |
sw.Close(); | |
AssetDatabase.Refresh(); | |
} | |
} | |
public class Subtitle : IComparer<Subtitle>, IComparable<Subtitle> | |
{ | |
public double Start; | |
public double End; | |
public string Text; | |
public Subtitle(double start, double end, string text) | |
{ | |
this.Text = text; | |
this.Start = start; | |
this.End = end; | |
} | |
public string Print() | |
{ | |
return string.Format("{0:F2} - {1:F2}, {2}", this.Start, this.End, this.Text); | |
} | |
public int Compare(Subtitle x, Subtitle y) | |
{ | |
if(x.Start < y.Start) | |
return -1; | |
if(x.Start > y.Start) | |
return 1; | |
return 0; | |
} | |
public int CompareTo(Subtitle other) | |
{ | |
if(this.Start < other.Start) | |
return -1; | |
if(this.Start > other.Start) | |
return 1; | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment