public VideoHandler(RawImage videoHolder, PromoData promoData, System.Action endCallback, RenderTexture renderTexture = null) { mVideoHolder = videoHolder; mPromoData = promoData; mRenderTexture = renderTexture; mEndCallback = endCallback; mVideoPlayer = mVideoHolder.gameObject.GetComponent <UnityEngine.Video.VideoPlayer>(); if (mVideoPlayer == null) { mVideoPlayer = mVideoHolder.gameObject.AddComponent <UnityEngine.Video.VideoPlayer>(); } mVideoPlayer.loopPointReached += OnLoopPointReached; mAudioSource = mVideoHolder.gameObject.GetComponent <AudioSource>(); if (mAudioSource == null) { mAudioSource = mVideoHolder.gameObject.AddComponent <AudioSource>(); } mVideoPlayer.source = UnityEngine.Video.VideoSource.Url; mVideoPlayer.url = mPromoData.VideoFileDownloaded; mVideoPlayer.audioOutputMode = UnityEngine.Video.VideoAudioOutputMode.AudioSource; mVideoPlayer.EnableAudioTrack(0, true); mVideoPlayer.SetTargetAudioSource(0, mAudioSource); mVideoPlayer.Prepare(); if (mRenderTexture == null) { mRenderTexture = new RenderTexture(int.Parse(mPromoData.width), int.Parse(mPromoData.height), 16, RenderTextureFormat.ARGB32); } mVideoPlayer.targetTexture = mRenderTexture; mVideoHolder.texture = mRenderTexture; }
public override void CreatePlayer() { _mediaPlayer = gameObject.GetComponent <UnityEngine.Video.VideoPlayer>(); if (_mediaPlayer == null) { _mediaPlayer = gameObject.AddComponent <UnityEngine.Video.VideoPlayer>(); } _audioSource = gameObject.GetComponent <AudioSource>(); if (_audioSource == null) { _audioSource = gameObject.AddComponent <AudioSource>(); } _mediaPlayer.audioOutputMode = UnityEngine.Video.VideoAudioOutputMode.AudioSource; _mediaPlayer.SetTargetAudioSource(0, _audioSource); _mediaPlayer.renderMode = UnityEngine.Video.VideoRenderMode.APIOnly; _mediaPlayer.prepareCompleted += OnVideoLoadingComplete; _mediaPlayer.EnableAudioTrack(0, true); }
/// <param name="clip">VideoClip reference</param> public override bool SetValues(PlayerValues values) { _mediaPlayer = gameObject.GetComponent <UnityEngine.Video.VideoPlayer>(); if (_mediaPlayer == null) { // no media component already added to this component, try adding a MediaPlayer component try { _mediaPlayer = gameObject.AddComponent <UnityEngine.Video.VideoPlayer>(); } catch (Exception e) { Debug.LogError("AVProVideo not found in project: " + e.ToString()); return(false); } } _audioSource = gameObject.GetComponent <AudioSource>(); if (_audioSource == null) { _audioSource = gameObject.AddComponent <AudioSource>(); } if (_mediaPlayer == null) { _mediaPlayer = gameObject.AddComponent <UnityEngine.Video.VideoPlayer>(); } _mediaPlayer.audioOutputMode = UnityEngine.Video.VideoAudioOutputMode.AudioSource; _mediaPlayer.SetTargetAudioSource(0, _audioSource); _mediaPlayer.renderMode = UnityEngine.Video.VideoRenderMode.APIOnly; _mediaPlayer.clip = values.videoClip; _mediaPlayer.prepareCompleted += OnVideoLoadingComplete; _mediaPlayer.EnableAudioTrack(0, true); return(true); }
/// <summary> /// 繪製轉場圖片 /// </summary> /// <param name="_step">繪製完後要進入的腳本位置</param> /// <param name="sceneName">Scene名稱</param> private static void DrawSlidePic(int _step) { GameObject parent; switch (KK_FBIOpenUp.nowGameMode) { case KK_FBIOpenUp.GameMode.Studio: parent = GameObject.Find("StudioScene/Canvas Main Menu"); break; case KK_FBIOpenUp.GameMode.MainGame: parent = GameObject.Find("ActionScene/UI/ActionMenuCanvas/ModeAnimation"); break; case KK_FBIOpenUp.GameMode.Maker: parent = GameObject.Find("CustomScene/CustomRoot/FrontUIGroup/CustomUIGroup/CvsCoordinateType/redBagBtn"); break; //FreeH死都不成功,放棄 //case KK_FBIOpenUp.GameMode.FreeH: // parent = GameObject.Find("CommonSpace"); // break; default: parent = GameObject.FindObjectsOfType <GameObject>()[0]; //Not tested break; } GameObject gameObject = new GameObject(); gameObject.transform.SetParent(parent.transform, false); gameObject.SetActive(false); if (null != shiftPicture) { GameObject.Destroy(shiftPicture.Transform.parent.gameObject); shiftPicture.image = null; shiftPicture.video = null; shiftPicture = null; } shiftPicture = new ShiftPicture(); //如果影片不存在,用熊吉代替 bool noVideoFallback = _step == 20 && null == KK_FBIOpenUp.videoPath; if (noVideoFallback) { _step = 2; } switch (_step) { case 1: //小學生真是太棒了 shiftPicture.type = ShiftPicture.Type.picture; shiftPicture.image = UIUtility.CreateImage("", gameObject.transform, Extension.Extension.LoadNewSprite("KK_FBIOpenUp.Resources.saikodaze.jpg", 800, 657)); shiftPicture.image.rectTransform.sizeDelta = new Vector2(Screen.height / 1.5f * 800 / 657, Screen.height / 1.5f); Right2Center(); break; case 2: //熊吉逮捕 shiftPicture.type = ShiftPicture.Type.picture; shiftPicture.image = UIUtility.CreateImage("", gameObject.transform, Extension.Extension.LoadNewSprite("KK_FBIOpenUp.Resources.Kumakichi.jpg", 640, 480)); shiftPicture.image.rectTransform.sizeDelta = new Vector2(Screen.height / 1.5f * 640 / 480, Screen.height / 1.5f); Left2Center(); break; case 10: //幼女退光線 shiftPicture.type = ShiftPicture.Type.picture; shiftPicture.image = UIUtility.CreateImage("", gameObject.transform, Extension.Extension.LoadNewSprite("KK_FBIOpenUp.Resources.beam.png", 700, 700)); shiftPicture.image.rectTransform.sizeDelta = new Vector2(Screen.height / 1.25f, Screen.height / 1.25f); Right2Center(); break; case 20: //FBI Open Up影片 shiftPicture.type = ShiftPicture.Type.video; shiftPicture.video = UIUtility.CreateRawImage("", gameObject.transform); shiftPicture.video.rectTransform.sizeDelta = new Vector2(Screen.height / 1.5f, Screen.height / 1.5f); UnityEngine.Video.VideoPlayer videoPlayer = gameObject.AddComponent <UnityEngine.Video.VideoPlayer>(); AudioSource audioSource = gameObject.AddComponent <AudioSource>(); videoPlayer.playOnAwake = false; audioSource.playOnAwake = false; videoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.APIOnly; //videoPlayer.url= "../UserData/audio/FBI.mp4"; videoPlayer.url = KK_FBIOpenUp.videoPath; //Set Audio Output to AudioSource videoPlayer.audioOutputMode = UnityEngine.Video.VideoAudioOutputMode.AudioSource; //Assign the Audio from Video to AudioSource to be played videoPlayer.EnableAudioTrack(0, true); videoPlayer.SetTargetAudioSource(0, audioSource); Logger.LogDebug($"{videoPlayer.url}"); videoPlayer.isLooping = true; //先把他移到螢幕外啟用,否則未啟用無法Prepare,而直接啟用會出現白色畫面 shiftPicture.Transform.position = new Vector3(-2 * Screen.width, Screen.height / 2); gameObject.SetActive(true); videoPlayer.Prepare(); videoPlayer.prepareCompleted += (source) => { ///TODO 這實際上沒有辦法真的catch到錯誤,待修 if (videoPlayer.texture == null) { Logger.LogError("Video not found"); GameObject.Destroy(shiftPicture.Transform.parent.gameObject); shiftPicture.video = null; shiftPicture = null; _step = 0; return; } shiftPicture.video.texture = videoPlayer.texture; videoTimer = 2; videoPlayer.Play(); audioSource.Play(); //影片太大聲QQ audioSource.volume = KK_FBIOpenUp.videoVolume; Left2Center(); }; break; } //如果影片不存在,用熊吉代替 if (noVideoFallback) { _step = 20; } step = _step; Logger.LogDebug("Draw Slide Pic"); void Right2Center() { //Right To Center shiftPicture.Transform.position = new Vector3(Screen.width + shiftPicture.Width / 2, Screen.height / 2); shiftPicture.targetPosition = new Vector3(Screen.width / 2, Screen.height / 2); gameObject.SetActive(true); } void Left2Center() { //Left To Center shiftPicture.Transform.position = new Vector3(-1 * (Screen.width + shiftPicture.Width / 2), Screen.height / 2); shiftPicture.targetPosition = new Vector3(Screen.width / 2, Screen.height / 2); gameObject.SetActive(true); } }