UMPTextureUpdator.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace UMP
  4. {
  5. public class UMPTextureUpdator : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private RawImage _image = null;
  9. [SerializeField]
  10. private UniversalMediaPlayer _player = null;
  11. private Texture2D _texture;
  12. private long _framesCounter;
  13. void Start()
  14. {
  15. _player.AddImageReadyEvent(OnImageReady);
  16. _player.AddStoppedEvent(OnStop);
  17. }
  18. void Update()
  19. {
  20. if (_texture != null && _framesCounter != _player.FramesCounter)
  21. {
  22. _texture.LoadRawTextureData(_player.FramePixels);
  23. _texture.Apply();
  24. _framesCounter = _player.FramesCounter;
  25. }
  26. }
  27. void OnDestroy()
  28. {
  29. _player.RemoveStoppedEvent(OnStop);
  30. }
  31. void OnImageReady(Texture image)
  32. {
  33. if (_texture != null)
  34. Destroy(_texture);
  35. //Video size != Video buffer size (FramePixels has video buffer size), so we will use
  36. //previously created playback texture size that based on video buffer size
  37. _texture = MediaPlayerHelper.GenVideoTexture(image.width, image.height);
  38. _texture.Apply();
  39. _image.texture = _texture;
  40. }
  41. void OnStop()
  42. {
  43. if (_texture != null)
  44. Destroy(_texture);
  45. _texture = null;
  46. }
  47. }
  48. }