using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
//[RequireComponent(typeof(VideoPlayer))]
public class VideoShow : BaseShow
{
public static VideoPlayer vpPlaying;
///
/// 视频播放器对象
///
VideoPlayer vp;
///
/// 视频播放器扩展对象
///
VideoPlayerEx vpEx;
void Start()
{
if (info != null)
{
Renderer renderer = GetComponent();
if (info.PlayVideoOnObject)
{
vp = gameObject.AddComponent();
// vp = GetComponent();
vp.targetMaterialRenderer = renderer;
vp.playOnAwake = info.AutoPlay;
vp.isLooping = info.Loop;
vp.source = VideoSource.Url;
vp.url = CommonData.VideoFullPath + info.VideoName;
vp.renderMode = VideoRenderMode.MaterialOverride;
}
// 如果不是自动播放则显示Cover图片
if (!info.AutoPlay && !string.IsNullOrEmpty(info.ImageName))
{
StartCoroutine(ImageLoader.LoadImage(CommonData.ImageFullPath + info.ImageName, renderer.sharedMaterial));
}
if (info.Clickable)
{
// 添加碰撞器以允许单击
if (GetComponent() == null)
{
gameObject.AddComponent();
}
}
}
vpEx = FindObjectOfType();
if (vpEx != null)
{
vpEx.onVideoClosed += delegate {
BtnController.Instance.SetCanControlDelay();
// Invoke("SetControl", 0.2f);
// BaseController.CanControl = true;
// Caching.ClearCache();
};
}
}
public override void OnClick()
{
if (BaseController.CanControl)
{
Invoke("ShowVideoEx", 0.01f);
}
}
void ShowVideoEx()
{
//if (!BaseController.IsJoyStickOnControl && BaseController.CanControl && CanClick)
if (BaseController.CanControl && CanClick)
{
if (info.PlayVideoOnObject)
{
if (vp != null)
{
if (vp.isPlaying)
{
vp.Pause();
}
else
{
if (vpPlaying != null && vpPlaying.isPlaying)
{
vpPlaying.Pause();
}
vp.Play();
vpPlaying = vp;
}
}
}
else
{
if (vpEx != null && !string.IsNullOrEmpty(info.VideoName))
{
BaseController.CanControl = false;
AudioManager.Instance.PauseBackgroundMusic();
if (H5Controller.IsMobileBroswer())
{
H5Controller.LoadVideo(info.VideoName, info.Title);
}
else
{
vpEx.ShowVideoPlayer();
vpEx.PlayVideo(info.VideoName, info.Title);
}
}
}
}
}
}