SubtitleMovieManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using wf;
  5. public class SubtitleMovieManager : MonoBehaviour
  6. {
  7. public bool isPlaying { get; private set; }
  8. public static SubtitleMovieManager GetGlobalInstance()
  9. {
  10. GameObject childObject = UTY.GetChildObject(GameObject.Find("__GameMain__"), "SystemUI Root", false);
  11. GameObject gameObject = UTY.GetChildObject(childObject, "SubtitleMovieManager", true);
  12. if (gameObject != null)
  13. {
  14. gameObject = Utility.CreatePrefab(childObject, "SubtitleMovieManager", true);
  15. }
  16. return gameObject.GetComponent<SubtitleMovieManager>();
  17. }
  18. public static void DestroyGlobalInstance()
  19. {
  20. GameObject childObject = UTY.GetChildObject(GameObject.Find("__GameMain__"), "SystemUI Root", false);
  21. GameObject childObject2 = UTY.GetChildObject(childObject, "SubtitleMovieManager", true);
  22. if (childObject2 != null)
  23. {
  24. UnityEngine.Object.DestroyImmediate(childObject2);
  25. }
  26. }
  27. public void Clear()
  28. {
  29. this.displayDatas.Clear();
  30. this.isPlaying = false;
  31. }
  32. public bool AddData(int displayStartTime, int displayTime, string text)
  33. {
  34. if (this.isPlaying)
  35. {
  36. return false;
  37. }
  38. SubtitleMovieManager.DisplayData displayData = new SubtitleMovieManager.DisplayData();
  39. displayData.displayStartTime = displayStartTime;
  40. displayData.displayEndTime = displayStartTime + displayTime;
  41. displayData.text = text;
  42. this.displayDatas.Add(displayData);
  43. return true;
  44. }
  45. public void LoadSubtitleScriptFile(string fileName)
  46. {
  47. if (!GameUty.FileSystem.IsExistentFile(fileName))
  48. {
  49. return;
  50. }
  51. byte[] array = null;
  52. try
  53. {
  54. using (AFileBase afileBase = GameUty.FileOpen(fileName, null))
  55. {
  56. if (!afileBase.IsValid())
  57. {
  58. return;
  59. }
  60. array = new byte[afileBase.GetSize()];
  61. afileBase.Read(ref array, afileBase.GetSize());
  62. }
  63. }
  64. catch (Exception)
  65. {
  66. return;
  67. }
  68. if (array == null)
  69. {
  70. return;
  71. }
  72. string[] array2 = NUty.SjisToUnicode(array).Split(new string[]
  73. {
  74. "\r\n",
  75. "\r",
  76. "\n"
  77. }, StringSplitOptions.RemoveEmptyEntries);
  78. int num = 0;
  79. int num2 = 0;
  80. bool flag = false;
  81. string text = string.Empty;
  82. foreach (string text2 in array2)
  83. {
  84. if (text2.IndexOf("@talk") == 0)
  85. {
  86. int num3 = text2.IndexOf("[") + 1;
  87. string[] array4 = text2.Substring(num3, text2.Length - num3 - 1).Split(new string[]
  88. {
  89. "-"
  90. }, StringSplitOptions.RemoveEmptyEntries);
  91. num = int.Parse(array4[0]);
  92. num2 = int.Parse(array4[1]);
  93. flag = true;
  94. text = string.Empty;
  95. }
  96. else if (text2.IndexOf("@hitret") == 0)
  97. {
  98. this.AddData(num, num2 - num, text);
  99. flag = false;
  100. }
  101. else if (flag)
  102. {
  103. text += text2;
  104. }
  105. }
  106. }
  107. public void Play(int displayTime, string text)
  108. {
  109. this.Clear();
  110. this.AddData(0, displayTime, text);
  111. this.Play();
  112. }
  113. public void Play()
  114. {
  115. this.timeElapsed = 0;
  116. this.isPlaying = true;
  117. }
  118. public void Stop()
  119. {
  120. if (this.subtitleMgr != null)
  121. {
  122. this.subtitleMgr.visible = false;
  123. }
  124. this.isPlaying = false;
  125. }
  126. public void Update()
  127. {
  128. if (!this.isPlaying || this.displayDatas.Count == 0 || this.subtitleMgr == null)
  129. {
  130. this.Stop();
  131. return;
  132. }
  133. this.timeElapsed += (int)(Time.deltaTime * 1000f);
  134. bool visible = false;
  135. List<SubtitleMovieManager.DisplayData> list = new List<SubtitleMovieManager.DisplayData>();
  136. for (int i = 0; i < this.displayDatas.Count; i++)
  137. {
  138. SubtitleMovieManager.DisplayData displayData = this.displayDatas[i];
  139. if (displayData.displayStartTime <= this.timeElapsed && this.timeElapsed <= displayData.displayEndTime)
  140. {
  141. visible = true;
  142. this.subtitleMgr.SetTextFromScriptStyle(displayData.text);
  143. }
  144. else if (displayData.displayEndTime < this.timeElapsed)
  145. {
  146. list.Add(displayData);
  147. }
  148. }
  149. this.subtitleMgr.visible = visible;
  150. for (int j = 0; j < list.Count; j++)
  151. {
  152. for (int k = 0; k < this.displayDatas.Count; k++)
  153. {
  154. if (list[j] == this.displayDatas[k])
  155. {
  156. this.displayDatas.RemoveAt(k);
  157. break;
  158. }
  159. }
  160. }
  161. }
  162. [SerializeField]
  163. private SubtitleDisplayManager subtitleMgr;
  164. private int timeElapsed;
  165. private List<SubtitleMovieManager.DisplayData> displayDatas = new List<SubtitleMovieManager.DisplayData>();
  166. private class DisplayData
  167. {
  168. public int displayStartTime;
  169. public int displayEndTime;
  170. public string text;
  171. }
  172. }