ApplyToBase.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_TVOS
  2. #define UNITY_PLATFORM_SUPPORTS_YPCBCR
  3. #endif
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. namespace RenderHeads.Media.AVProVideo
  8. {
  9. /// <summary>
  10. /// Base class to apply texture from MediaPlayer
  11. /// </summary>
  12. public abstract class ApplyToBase : MonoBehaviour
  13. {
  14. [Header("Media Source")]
  15. [Space(8f)]
  16. [SerializeField] protected MediaPlayer _media = null;
  17. public MediaPlayer Player
  18. {
  19. get { return _media; }
  20. set { ChangeMediaPlayer(value); }
  21. }
  22. [Space(8f)]
  23. [Header("Display")]
  24. [SerializeField] bool _automaticStereoPacking = true;
  25. public bool AutomaticStereoPacking
  26. {
  27. get { return _automaticStereoPacking; }
  28. set { if (_automaticStereoPacking != value) { _automaticStereoPacking = value; _isDirty = true; } }
  29. }
  30. [SerializeField] StereoPacking _overrideStereoPacking = StereoPacking.None;
  31. public StereoPacking OverrideStereoPacking
  32. {
  33. get { return _overrideStereoPacking; }
  34. set { if (_overrideStereoPacking != value) { _overrideStereoPacking = value; _isDirty = true; } }
  35. }
  36. [SerializeField] bool _stereoRedGreenTint = false;
  37. public bool StereoRedGreenTint { get { return _stereoRedGreenTint; } set { if (_stereoRedGreenTint != value) { _stereoRedGreenTint = value; _isDirty = true; } } }
  38. protected bool _isDirty = false;
  39. void Awake()
  40. {
  41. ChangeMediaPlayer(_media, force:true);
  42. }
  43. private void ChangeMediaPlayer(MediaPlayer player, bool force = false)
  44. {
  45. if (_media != player || force)
  46. {
  47. if (_media != null)
  48. {
  49. _media.Events.RemoveListener(OnMediaPlayerEvent);
  50. }
  51. _media = player;
  52. if (_media != null)
  53. {
  54. _media.Events.AddListener(OnMediaPlayerEvent);
  55. }
  56. _isDirty = true;
  57. }
  58. }
  59. // Callback function to handle events
  60. private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  61. {
  62. switch (et)
  63. {
  64. case MediaPlayerEvent.EventType.FirstFrameReady:
  65. case MediaPlayerEvent.EventType.PropertiesChanged:
  66. ForceUpdate();
  67. break;
  68. }
  69. }
  70. public void ForceUpdate()
  71. {
  72. _isDirty = true;
  73. if (this.isActiveAndEnabled)
  74. {
  75. Apply();
  76. }
  77. }
  78. private void Start()
  79. {
  80. SaveProperties();
  81. Apply();
  82. }
  83. protected virtual void OnEnable()
  84. {
  85. SaveProperties();
  86. ForceUpdate();
  87. }
  88. protected virtual void OnDisable()
  89. {
  90. RestoreProperties();
  91. }
  92. private void OnDestroy()
  93. {
  94. ChangeMediaPlayer(null);
  95. }
  96. protected virtual void SaveProperties()
  97. {
  98. }
  99. protected virtual void RestoreProperties()
  100. {
  101. }
  102. public abstract void Apply();
  103. }
  104. }