using DG.Tweening;
using RenderHeads.Media.AVProVideo;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.Video;
using XRTool.Util;
namespace XRTool.WorldUI
{
///
/// 基本的3D图形
/// 与UI Image进行结合转化,自动换算
///
public class XRVideoPlayer : MonoBehaviour
{
private XRSlider xRSlider;
private XRImage3D bG;
private XRImage3D xRImage3D;
private XRImage3D pause;
private RenderTexture videoTexture;
public RenderTexture VideoTexture { get => videoTexture; set => videoTexture = value; }
private RectTransform rectTransform;
private Vector2 videoSize;
public bool isDown = false;
private int hour, mint;
private TextMesh timeText;
private VideoPlayer videoPlayer;
private Transform root;
private MeshFilter body;
private Renderer bodyRender;
public bool isAutoScale = true;
public float thickness = 100;
// public float scale = 0.5f;
public VideoClip video;
private XRButton xRButton;
private BoxCollider box;
private AVProVideoPlayer avProVideoPlayer;
void Start()
{
//if (video)
//{
// SetSimple();
//}
StartCoroutine(videoUpdate());
TimeText.text = "0:00";
// XRImage3D.SetColor(Color.black);
initVideo();
}
private IEnumerator InitVideoPlayer()
{
yield return AVProVideoPlayer;
while (AVProVideoPlayer.GetUrl()!=null)
{
yield return new WaitForFixedUpdate();
}
Vector2 size = Vector2.one;
size.x = AVProVideoPlayer.GetVideoWidth();
size.y = AVProVideoPlayer.GetVideoHeight();
// Box.size = new Vector3(size.x/RectTransform.localScale.x, size.y / RectTransform.localScale.y, 1);
if (!VideoTexture)
{
VideoTexture = new RenderTexture((int)size.x, (int)size.y, 0);
}
else
{
VideoTexture.width = (int)size.x;
VideoTexture.width = (int)size.y;
}
XRImage3D.SetColor(Color.white);
// VideoPlayer.targetTexture = VideoTexture;
//XRImage3D.SetSimple(AVProVideoPlayer.Control.GetTexture());
}
public void SetSimple(VideoClip tex)
{
//this.video = tex;
//if (this.gameObject!=null)
//{
// UnityUtil.ChangeMateVideo(VideoPlayer.gameObject, tex);
//}
}
public void SetColor(Color color) {
if (BodyRender && BodyRender.enabled)
{
try
{
UnityUtil.ChangeMateColor(BodyRender, color);
}
catch (Exception ex)
{
UnityLog.Instance.LogError(BodyRender.material + " have no mainTexture");
}
}
}
public void SetSimple(Texture tex)
{
if (BodyRender && tex && BodyRender.enabled)
{
try
{
UnityUtil.ChangeMateTexture(BodyRender, tex);
}
catch (Exception ex)
{
UnityLog.Instance.LogError(BodyRender.material + " have no mainTexture");
}
}
}
public void SetSimple()
{
if (video)
{
SetSimple(video);
}
}
///
/// 3D物体
///
public MeshFilter Body
{
get
{
if (!body)
{
body = UnityUtil.GetBreadthChild(transform, "Body");
}
return body;
}
}
public XRButton XRButton
{
get
{
if (!xRButton)
{
xRButton = UnityUtil.GetBreadthChild(transform, "XRButton");
}
return xRButton;
}
}
public BoxCollider Box
{
get
{
if (!box)
{
box = this.GetComponent();
}
return box;
}
}
public Transform Root
{
get
{
if (!root)
{
root = UnityUtil.GetBreadthChild(transform, "Root");
}
return root;
}
set => root = value;
}
public RectTransform RectTransform
{
get
{
if (!rectTransform)
{
rectTransform = this.GetComponent();
}
return rectTransform;
}
set => rectTransform = value;
}
public Vector2 VideoSize
{
get
{
if (videoSize == Vector2.zero)
{
videoSize = new Vector2(AVProVideoPlayer.GetVideoWidth(), AVProVideoPlayer.GetVideoHeight());
}
return videoSize;
}
}
public XRImage3D XRImage3D
{
get
{
if (!xRImage3D)
{
xRImage3D = UnityUtil.GetBreadthChild(transform, "XRImage3D");
}
return xRImage3D;
}
}
public XRImage3D BG
{
get
{
if (!bG)
{
bG = UnityUtil.GetBreadthChild(transform, "BG");
}
return bG;
}
}
public XRImage3D Pause
{
get
{
if (!pause)
{
pause = UnityUtil.GetBreadthChild(transform, "Pause");
}
return pause;
}
}
public VideoPlayer VideoPlayer
{
get
{
if (!videoPlayer)
{
videoPlayer = this.GetComponent();
}
return videoPlayer;
}
}
public AVProVideoPlayer AVProVideoPlayer
{
get
{
if (!avProVideoPlayer)
{
avProVideoPlayer = this.GetComponent();
}
return avProVideoPlayer;
}
}
public TextMesh TimeText
{
get
{
if (!timeText)
{
timeText = UnityUtil.GetBreadthChild(transform, "TimeText");
}
return timeText;
}
}
public Renderer BodyRender
{
get
{
if (!bodyRender)
{
bodyRender = UnityUtil.GetBreadthChild(transform, "Body");
}
return bodyRender;
}
}
///
/// 如果不是全等缩放,则设置厚度
///
public void SetThickness()
{
SetThickness(thickness);
}
public void SetRenderMesh(Mesh mesh)
{
if (Body && mesh && Body.sharedMesh != mesh)
{
Body.sharedMesh = mesh;
Body.transform.localPosition = Vector3.zero;
Body.transform.localRotation = Quaternion.identity;
Vector3 center = mesh.bounds.center;
Vector3 pos = -center;
pos.z = -(mesh.bounds.center.z + mesh.bounds.size.z) / 2;
Body.transform.localPosition = pos;
}
}
public void SetRenderMate(Material mate)
{
if (BodyRender && mate)
{
BodyRender.material = mate;
}
}
public void AutoSetSize()
{
if (Root)
{
Vector3 size = RectTransform.rect.size;
if (!isAutoScale)
{
size.z = thickness;
}
else
{
size.z = RectTransform.rect.size.x > RectTransform.rect.size.y ?
RectTransform.rect.size.y : RectTransform.rect.size.x;
}
// Root.localScale = size * scale;
}
}
public void SetThickness(float thickness)
{
this.thickness = thickness;
if (!isAutoScale && Root)
{
Vector3 tmp = Root.localScale;
// tmp.z = thickness * scale;
// Root.localScale = tmp;
}
}
///
/// 厚度变化动效
///
/// 比例,相对于物体厚度的本身比例
/// 变化事件
public void DoThickness(float pressDis, float pressTime)
{
if (Root)
{
Root.DOKill();
float target = thickness * pressDis;
if (isAutoScale)
{
// target = (RectTransform.rect.size.x > RectTransform.rect.size.y ?
// RectTransform.rect.size.y : RectTransform.rect.size.x) * scale;
}
// Root.DOScaleZ(target, pressTime);
}
}
void OnRectTransformDimensionsChange()
{
AutoSetSize();
}
public XRSlider XRSlider
{
get
{
if (!xRSlider)
{
xRSlider = UnityUtil.GetBreadthChild(transform, "XRSlider");
}
return xRSlider;
}
}
IEnumerator videoUpdate()
{
while (true)
{
if (XRSlider && AVProVideoPlayer && isDown == false)
{
if (AVProVideoPlayer.IsPlaying())
{
//XRSlider.value = float.Parse(VideoPlayer.frame.ToString()) / float.Parse(VideoPlayer.frameCount.ToString());
// Debug.Log("videoUpdate " + AVProVideoPlayer.GetNowTimer() + " "+ AVProVideoPlayer.GetMaxTimer());
float nowTimer = AVProVideoPlayer.GetNowTimer();
float maxTimer = AVProVideoPlayer.GetMaxTimer();
XRSlider.value = nowTimer / maxTimer;
TimeText.text = (((int)nowTimer)/60).ToString()+":"+(((int)nowTimer) %60).ToString() + "/" + (((int)maxTimer) / 60).ToString() + ":" + (((int)maxTimer) % 60).ToString();
XRImage3D.transform.localScale = new Vector3(XRImage3D.transform.localScale.x
, 1/(AVProVideoPlayer.GetVideoWidth()/ AVProVideoPlayer.GetVideoHeight()), XRImage3D.transform.localScale.z);
// Debug.Log(VideoSize.ToString());
XRSlider.gameObject.SetActive(true);
}
else
{
XRSlider.gameObject.SetActive(false);
}
}
if (!AVProVideoPlayer.IsPlaying()&&!isShowControl)
{
showControl();
}
yield return new WaitForSeconds(0.035f);
}
}
public string GetNowTime()
{
//double f = VideoPlayer.time;
//hour = (int)f / 60;
//mint = (int)f % 60;
//return string.Format(" {0:D2}:{1:D2}", hour.ToString(), mint.ToString());
return AVProVideoPlayer.GetNowTimer().ToString();
}
public string GetMaxTime()
{
//float f = VideoPlayer.frameCount / VideoPlayer.frameRate;
//hour = (int)f / 60;
//mint = (int)f % 60;
//return string.Format(" {0:D2}:{1:D2}", hour.ToString(), mint.ToString());
return AVProVideoPlayer.GetMaxTimer().ToString();
}
public void sliderChanged(float per)
{
if (XRSlider && AVProVideoPlayer)
{
//float targetFrame = VideoPlayer.frameCount * per;
//VideoPlayer.frame = (long)targetFrame;
AVProVideoPlayer.SetSeek(per);
}
}
public void initVideo()
{
StartCoroutine(InitVideoPlayer());
}
bool isShowControl = true;
public void showControl()
{
if(!isShowControl)
{
GameEffect.setAlpha(Pause.gameObject, 1, 0.5f);
GameEffect.setAlpha(XRSlider.gameObject, 1, 0.5f);
GameEffect.setAlpha(BG.gameObject, 1, 0.5f);
isShowControl = true;
}
}
public void hideControl()
{
if (AVProVideoPlayer.IsPlaying()&& isShowControl)
{
GameEffect.setAlpha(Pause.gameObject, 0, 0.5f, (GameObject obj) => {
obj.SetActive(false);
});
GameEffect.setAlpha(XRSlider.gameObject, 0, 0.5f, (GameObject obj) => {
obj.SetActive(false);
});
GameEffect.setAlpha(BG.gameObject, 0, 0.5f,(GameObject obj)=> {
obj.SetActive(false);
});
isShowControl = false;
}
}
}
}