VideoManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Examples of VideoPlayer function
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.Video;
  5. using System.Collections;
  6. public class VideoManager : MonoBehaviour
  7. {
  8. public RenderTexture VideoRenderTexture;
  9. public static VideoManager Instance;
  10. public float Timer { get; internal set; }
  11. public TextMeshProUGUI TimerLabel;
  12. public VideoPlayer VideoPlayer;
  13. void Start()
  14. {
  15. Instance = this;
  16. // Will attach a VideoPlayer to the main camera.
  17. GameObject camera = gameObject;
  18. // VideoPlayer automatically targets the camera backplane when it is added
  19. // to a camera object, no need to change videoPlayer.targetCamera.
  20. VideoPlayer = camera.AddComponent<UnityEngine.Video.VideoPlayer>();
  21. }
  22. void EndReached(UnityEngine.Video.VideoPlayer vp)
  23. {
  24. //vp.playbackSpeed = vp.playbackSpeed / 10.0F;
  25. }
  26. void Update ()
  27. {
  28. Timer -= Time.deltaTime;
  29. TimerLabel.text = ((int)Timer).ToString();
  30. if(MenuManager._instance)
  31. {
  32. if (Timer <= 0 && MenuManager._instance.VideoAd.isActiveAndEnabled)
  33. {
  34. if (PlayerPrefs.GetInt("PlayAdForLifes") == 0)
  35. {
  36. NotificationCenter.Post(NotificationType.CelebrityDisappeared);
  37. }
  38. else
  39. {
  40. LivesManager.Instance.RechargeLife();
  41. }
  42. MenuManager._instance.OnLeavingViewingAd();
  43. PlayerPrefs.SetInt("PlayAdForLifes", 0);
  44. PlayerPrefs.Save();
  45. }
  46. }
  47. }
  48. public void PlayAd()
  49. {
  50. Timer = 8;
  51. Debug.Log("PlayAd");
  52. VideoPlayer.frame = 0;
  53. VideoPlayer.Play();
  54. }
  55. public void StopAd()
  56. {
  57. VideoPlayer.Stop();
  58. }
  59. IEnumerator playVideo(string strURL)
  60. {
  61. //Disable Play on Awake for both Video and Audio
  62. VideoPlayer.playOnAwake = false;
  63. //We want to play from video clip not from url
  64. VideoPlayer.source = VideoSource.Url;
  65. // By default, VideoPlayers added to a camera will use the far plane.
  66. // Let's target the near plane instead.
  67. VideoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane;
  68. // This will cause our Scene to be visible through the video being played.
  69. VideoPlayer.targetCameraAlpha = 0.5F;
  70. VideoPlayer.renderMode = VideoRenderMode.RenderTexture;
  71. VideoPlayer.targetTexture = VideoRenderTexture;
  72. // Restart from beginning when done.
  73. VideoPlayer.isLooping = true;
  74. VideoPlayer.url = strURL;
  75. //Set video To Play then prepare Audio to prevent Buffering
  76. VideoPlayer.Prepare();
  77. //Wait until video is prepared
  78. while (!VideoPlayer.isPrepared)
  79. {
  80. // Debug.Log("Preparing Video");
  81. yield return null;
  82. }
  83. Debug.Log("Done Preparing Video");
  84. //Set Audio Output to AudioSource
  85. //VideoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
  86. //Assign the Audio from Video to AudioSource to be played
  87. //VideoPlayer.EnableAudioTrack(0, true);
  88. //VideoPlayer.SetTargetAudioSource(0, audioSource);
  89. //Assign the Texture from Video to RawImage to be displayed
  90. //image.texture = videoPlayer.texture;
  91. //Play Video
  92. VideoPlayer.Play();
  93. //Play Sound
  94. //audioSource.Play();
  95. Debug.Log("Playing Video");
  96. while (VideoPlayer.frame < (long)VideoPlayer.frameCount - 1) { //as frame goes upto frameCount - 1
  97. yield return null;
  98. }
  99. Debug.Log("Done Playing Video");
  100. }
  101. public void SetURLVideo(string videoUrl)
  102. {
  103. StartCoroutine(playVideo(videoUrl));
  104. }
  105. }