using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.UI; public class SubtitleBlock { private static readonly Regex TimeStampRegex = new Regex(@"^(?\d{2}:\d{2}:\d{2},\d{3})\s+(?\d{2}:\d{2}:\d{2},\d{3})$"); public int Index { get; private set; } public double Length { get; private set; } public double From { get; private set; } public double To { get; private set; } public string Text { get; private set; } public SubtitleBlock(int index, double from, double to, string text) { this.Index = index; this.From = from; this.To = to; this.Length = to - from; this.Text = text; } public override string ToString() { return "Index: " + Index + " From: " + From + " To: " + To + " Text: " + Text; } static public List ParseSubtitles(string content) { var subtitles = new List(); //var regex = new Regex($@"(?\d*\s*)\n(?\d*:\d*:\d*,\d*)\s*-->\s*(?\d*:\d*:\d*,\d*)\s*\n(?.*)\n(?.*)\n"); var regex = new Regex(@"(?\d+)\r?\n(?\d{2}:\d{2}:\d{2},\d{3}) --> (?\d{2}:\d{2}:\d{2},\d{3})\r?\n(?.*?)(\r?\n\r?\n|$)", RegexOptions.Singleline); var matches = regex.Matches(content); foreach (Match match in matches) { var groups = match.Groups; int ind = int.Parse(groups["index"].Value); TimeSpan fromtime, totime; TimeSpan.TryParse(groups["start"].Value.Replace(',', '.'), out fromtime); TimeSpan.TryParse(groups["end"].Value.Replace(',', '.'), out totime); string contenttext = groups["content"].Value; subtitles.Add(new SubtitleBlock(ind, fromtime.TotalSeconds, totime.TotalSeconds, contenttext)); } return subtitles; } //public static List ParseSubtitles1(string srtFilePath) //{ // List subtitles = new List(); // string[] lines = File.ReadAllLines(srtFilePath); // for (int i = 0; i < lines.Length; i++) // { // var line = lines[i].Trim(); // if (int.TryParse(line, out int index)) // { // // 当前行是新字幕的索引,开始解析新的字幕 // int tmpIndex = index; // i++; // 跳过空行 // var timeStampLine = lines[i]; // var match = TimeStampRegex.Match(timeStampLine); // if (match.Success) // { // Double From = TimeSpan.Parse(match.Groups["start"].Value).TotalSeconds; // Double To = TimeSpan.Parse(match.Groups["end"].Value).TotalSeconds; // } // i++; // 跳过时间戳行 // while (i < lines.Length && !string.IsNullOrWhiteSpace(lines[i])) // { // // 添加字幕文本直到遇到下一个索引 // string Text += lines[i++] + Environment.NewLine; // } // subtitles.Add(new SubtitleBlock(index,From,To,Text)); // } // } // return subtitles; //} } public class Subtitle : MonoBehaviour { public float offset; List subt; public Text subtitletext; string subcontent; public TextAsset subTitleSource; void Start() { if (subTitleSource == null) return; subt = SubtitleBlock.ParseSubtitles(subTitleSource.text); offset = (float)subt[0].From; Play(); } private void Play() { StartCoroutine(DisplaySubtitles()); } public float localtimer; public IEnumerator DisplaySubtitles() { for (int j = 0; j < subt.Count; j++) { subt[j].ToString(); var i = subt[j]; subtitletext.text = ""; if (i.From <= localtimer && i.To >= localtimer) { subtitletext.text = i.Text; yield return new WaitForSeconds((float)i.Length); } else if (i.From > localtimer) { yield return new WaitForSeconds(Mathf.Min((float)i.From - localtimer, 0.1f)); j--; } } yield return null; } }