SubtitlesUGUI.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. using UnityEngine.UI;
  5. namespace RenderHeads.Media.AVProVideo
  6. {
  7. [AddComponentMenu("AVPro Video/Subtitles uGUI", 201)]
  8. [HelpURL("http://renderheads.com/product/avpro-video/")]
  9. public class SubtitlesUGUI : MonoBehaviour
  10. {
  11. private void Start()
  12. {
  13. this.ChangeMediaPlayer(this._mediaPlayer);
  14. }
  15. private void OnDestroy()
  16. {
  17. this.ChangeMediaPlayer(null);
  18. }
  19. public void ChangeMediaPlayer(MediaPlayer newPlayer)
  20. {
  21. if (this._mediaPlayer != null)
  22. {
  23. this._mediaPlayer.Events.RemoveListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnMediaPlayerEvent));
  24. this._mediaPlayer = null;
  25. }
  26. this._mediaPlayer = newPlayer;
  27. if (this._mediaPlayer != null)
  28. {
  29. this._mediaPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnMediaPlayerEvent));
  30. }
  31. }
  32. private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  33. {
  34. if (et == MediaPlayerEvent.EventType.SubtitleChange)
  35. {
  36. string text = this._mediaPlayer.Subtitles.GetSubtitleText();
  37. text = text.Replace("<font color=", "<color=");
  38. text = text.Replace("</font>", "</color>");
  39. text = text.Replace("<u>", string.Empty);
  40. text = text.Replace("</u>", string.Empty);
  41. this._text.text = text;
  42. }
  43. }
  44. [SerializeField]
  45. private MediaPlayer _mediaPlayer;
  46. [SerializeField]
  47. private Text _text;
  48. }
  49. }