SubtitlesUGUI.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProVideo
  7. {
  8. /// <summary>
  9. /// Update a standard uGUI Text element with subtitle text as it plays from the MediaPlayer
  10. /// </summary>
  11. [AddComponentMenu("AVPro Video/Subtitles uGUI", 201)]
  12. [HelpURL("http://renderheads.com/products/avpro-video/")]
  13. public class SubtitlesUGUI : MonoBehaviour
  14. {
  15. [SerializeField] MediaPlayer _mediaPlayer = null;
  16. [SerializeField] Text _text = null;
  17. [SerializeField] Image _backgroundImage = null;
  18. [SerializeField] int _backgroundHorizontalPadding = 32;
  19. [SerializeField] int _backgroundVerticalPadding = 16;
  20. [SerializeField, Range(-1, 1024)] int _maxCharacters = 256;
  21. public MediaPlayer Player
  22. {
  23. set { ChangeMediaPlayer(value); }
  24. get { return _mediaPlayer; }
  25. }
  26. public Text Text
  27. {
  28. set { _text = value; }
  29. get { return _text; }
  30. }
  31. void Start()
  32. {
  33. ChangeMediaPlayer(_mediaPlayer);
  34. }
  35. void OnDestroy()
  36. {
  37. ChangeMediaPlayer(null);
  38. }
  39. void Update()
  40. {
  41. // TODO: Currently we need to call this each frame, as when it is called right after SetText()
  42. // the ContentSizeFitter hasn't run yet, so effectively the box is a frame behind.
  43. UpdateBackgroundRect();
  44. }
  45. public void ChangeMediaPlayer(MediaPlayer newPlayer)
  46. {
  47. // When changing the media player, handle event subscriptions
  48. if (_mediaPlayer != null)
  49. {
  50. _mediaPlayer.Events.RemoveListener(OnMediaPlayerEvent);
  51. _mediaPlayer = null;
  52. }
  53. SetText(string.Empty);
  54. if (newPlayer != null)
  55. {
  56. newPlayer.Events.AddListener(OnMediaPlayerEvent);
  57. _mediaPlayer = newPlayer;
  58. }
  59. }
  60. private void SetText(string text)
  61. {
  62. _text.text = text;
  63. UpdateBackgroundRect();
  64. }
  65. private string PrepareText(string text)
  66. {
  67. // Crop text that is too long
  68. if (_maxCharacters >= 0 && text.Length > _maxCharacters)
  69. {
  70. text = text.Substring(0, _maxCharacters);
  71. }
  72. // Change RichText for Unity uGUI Text
  73. text = text.Replace("<font color=", "<color=");
  74. text = text.Replace("</font>", "</color>");
  75. text = text.Replace("<u>", string.Empty);
  76. text = text.Replace("</u>", string.Empty);
  77. return text;
  78. }
  79. private void UpdateBackgroundRect()
  80. {
  81. if (_backgroundImage)
  82. {
  83. if (string.IsNullOrEmpty(_text.text))
  84. {
  85. _backgroundImage.enabled = false;
  86. }
  87. else
  88. {
  89. _backgroundImage.enabled = true;
  90. _backgroundImage.rectTransform.sizeDelta = _text.rectTransform.sizeDelta;
  91. _backgroundImage.rectTransform.anchoredPosition = _text.rectTransform.anchoredPosition;
  92. _backgroundImage.rectTransform.offsetMin -= new Vector2(_backgroundHorizontalPadding, _backgroundVerticalPadding);
  93. _backgroundImage.rectTransform.offsetMax += new Vector2(_backgroundHorizontalPadding, _backgroundVerticalPadding);
  94. }
  95. }
  96. }
  97. // Callback function to handle events
  98. private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  99. {
  100. switch (et)
  101. {
  102. case MediaPlayerEvent.EventType.Closing:
  103. {
  104. SetText(string.Empty);
  105. break;
  106. }
  107. case MediaPlayerEvent.EventType.SubtitleChange:
  108. {
  109. SetText(PrepareText(_mediaPlayer.Subtitles.GetSubtitleText()));
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. }