123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- // Examples of VideoPlayer function
- using TMPro;
- using UnityEngine;
- using UnityEngine.Video;
- using System.Collections;
- public class VideoManager : MonoBehaviour
- {
- public RenderTexture VideoRenderTexture;
- public static VideoManager Instance;
- public float Timer { get; internal set; }
- public TextMeshProUGUI TimerLabel;
- public VideoPlayer VideoPlayer;
- void Start()
- {
- Instance = this;
- // Will attach a VideoPlayer to the main camera.
- GameObject camera = gameObject;
- // VideoPlayer automatically targets the camera backplane when it is added
- // to a camera object, no need to change videoPlayer.targetCamera.
- VideoPlayer = camera.AddComponent<UnityEngine.Video.VideoPlayer>();
-
- }
- void EndReached(UnityEngine.Video.VideoPlayer vp)
- {
- //vp.playbackSpeed = vp.playbackSpeed / 10.0F;
- }
- void Update ()
- {
- Timer -= Time.deltaTime;
- TimerLabel.text = ((int)Timer).ToString();
- if(MenuManager._instance)
- {
- if (Timer <= 0 && MenuManager._instance.VideoAd.isActiveAndEnabled)
- {
- if (PlayerPrefs.GetInt("PlayAdForLifes") == 0)
- {
- NotificationCenter.Post(NotificationType.CelebrityDisappeared);
-
- }
- else
- {
- LivesManager.Instance.RechargeLife();
-
- }
- MenuManager._instance.OnLeavingViewingAd();
- PlayerPrefs.SetInt("PlayAdForLifes", 0);
- PlayerPrefs.Save();
- }
- }
- }
- public void PlayAd()
- {
- Timer = 8;
- Debug.Log("PlayAd");
- VideoPlayer.frame = 0;
- VideoPlayer.Play();
- }
- public void StopAd()
- {
- VideoPlayer.Stop();
- }
- IEnumerator playVideo(string strURL)
- {
- //Disable Play on Awake for both Video and Audio
- VideoPlayer.playOnAwake = false;
-
-
- //We want to play from video clip not from url
- VideoPlayer.source = VideoSource.Url;
- // By default, VideoPlayers added to a camera will use the far plane.
- // Let's target the near plane instead.
- VideoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane;
- // This will cause our Scene to be visible through the video being played.
- VideoPlayer.targetCameraAlpha = 0.5F;
- VideoPlayer.renderMode = VideoRenderMode.RenderTexture;
- VideoPlayer.targetTexture = VideoRenderTexture;
- // Restart from beginning when done.
- VideoPlayer.isLooping = true;
- VideoPlayer.url = strURL;
- //Set video To Play then prepare Audio to prevent Buffering
- VideoPlayer.Prepare();
-
- //Wait until video is prepared
- while (!VideoPlayer.isPrepared)
- {
- // Debug.Log("Preparing Video");
- yield return null;
- }
-
- Debug.Log("Done Preparing Video");
-
- //Set Audio Output to AudioSource
- //VideoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
-
- //Assign the Audio from Video to AudioSource to be played
- //VideoPlayer.EnableAudioTrack(0, true);
- //VideoPlayer.SetTargetAudioSource(0, audioSource);
-
- //Assign the Texture from Video to RawImage to be displayed
- //image.texture = videoPlayer.texture;
-
- //Play Video
- VideoPlayer.Play();
-
- //Play Sound
- //audioSource.Play();
- Debug.Log("Playing Video");
- while (VideoPlayer.frame < (long)VideoPlayer.frameCount - 1) { //as frame goes upto frameCount - 1
- yield return null;
- }
-
-
- Debug.Log("Done Playing Video");
- }
- public void SetURLVideo(string videoUrl)
- {
- StartCoroutine(playVideo(videoUrl));
- }
- }
|