using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class SubtitleSystem : MonoBehaviour { public TextAsset subtitleFile; private StreamReader reader; [SerializeField] public List subList = new List(); public SubtitleManager subManager; public AudioSource aSource; private float playTime; int subtitleIndex = 0; int index; int count; public Animator ani_mate; public GameObject button; void Start() { button.SetActive(false); Debug.Log(subtitleFile.name.ToString()); subtitleFile = Resources.Load(subtitleFile.name.ToString()) as TextAsset; if (subtitleFile == null) { subManager.showText("Error loading Subtitles File"); Debug.Log("Error Loading Subtitles File"); } else { subManager.showText("I'm able to set subtitles"); Debug.Log("I'm able to set subtitles"); } GetText(); subManager.hideText(); } void Update() { if (aSource.isPlaying) { if (subtitleIndex < subList.Count) { Debug.Log(aSource.time * 1); var SourceTime = aSource.time * 1; if (SourceTime >= subList[subtitleIndex].timeStart && SourceTime < subList[subtitleIndex].timeStop) { subManager.showText(subList[subtitleIndex].content); } if (SourceTime >= subList[subtitleIndex].timeStop) { subManager.hideText(); subtitleIndex++; } } else { subtitleIndex = 0; } } else { subManager.hideText(); button.SetActive(true); subtitleIndex = 0; } } float ParseTime(string time) { float tempTime = 0; float finalTime = 0; string[] times = time.Split(":"[0]); for (int i = 0; i < times.Length; i++) { tempTime = float.Parse(times[i]); if (i == 0) { if (tempTime >= 0) { tempTime *= 3600; finalTime += tempTime; tempTime = 0; } } else if (i == 1) { if (tempTime >= 0) { tempTime *= 60; finalTime += tempTime; tempTime = 0; } } else if (i == 2) { if (tempTime >= 0) { finalTime += tempTime; tempTime = 0; } } } return finalTime; } void GetText() { reader = new StreamReader(new MemoryStream(subtitleFile.bytes)); while (!reader.EndOfStream) { var input = reader.ReadLine(); if (!string.IsNullOrEmpty(input)) { if (count == 0) { Debug.Log(index.ToString()); subList.Add(new Subtitle()); count++; } else if (count == 1) { string[] time = input.Split("/"[0]); subList[index].timeStart = ParseTime(time[0]); subList[index].timeStop = ParseTime(time[1]); count++; } else if (count == 2) { subList[index].content = input; Debug.Log(subList[index].content); count++; } } else { index++; count = 0; } } } }