| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- 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(@"^(?<start>\d{2}:\d{2}:\d{2},\d{3})\s+(?<end>\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<SubtitleBlock> ParseSubtitles(string content)
- {
- var subtitles = new List<SubtitleBlock>();
- //var regex = new Regex($@"(?<index>\d*\s*)\n(?<start>\d*:\d*:\d*,\d*)\s*-->\s*(?<end>\d*:\d*:\d*,\d*)\s*\n(?<content>.*)\n(?<content2>.*)\n");
- var regex = new Regex(@"(?<index>\d+)\r?\n(?<start>\d{2}:\d{2}:\d{2},\d{3}) --> (?<end>\d{2}:\d{2}:\d{2},\d{3})\r?\n(?<content>.*?)(\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<SubtitleBlock> ParseSubtitles1(string srtFilePath)
- //{
- // List<SubtitleBlock> subtitles = new List<SubtitleBlock>();
- // 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<SubtitleBlock> 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;
- }
- }
|