123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
-
- 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
- {
- /// <summary>
- /// 基本的3D图形
- /// 与UI Image进行结合转化,自动换算
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// 3D物体
- /// </summary>
- public MeshFilter Body
- {
- get
- {
- if (!body)
- {
- body = UnityUtil.GetBreadthChild<MeshFilter>(transform, "Body");
- }
- return body;
- }
- }
- public XRButton XRButton
- {
- get
- {
- if (!xRButton)
- {
- xRButton = UnityUtil.GetBreadthChild<XRButton>(transform, "XRButton");
- }
- return xRButton;
- }
- }
- public BoxCollider Box
- {
- get
- {
- if (!box)
- {
- box = this.GetComponent<BoxCollider>();
- }
- return box;
- }
- }
- public Transform Root
- {
- get
- {
- if (!root)
- {
- root = UnityUtil.GetBreadthChild<Transform>(transform, "Root");
- }
- return root;
- }
- set => root = value;
- }
- public RectTransform RectTransform
- {
- get
- {
- if (!rectTransform)
- {
- rectTransform = this.GetComponent<RectTransform>();
- }
- 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<XRImage3D>(transform, "XRImage3D");
- }
- return xRImage3D;
- }
- }
- public XRImage3D BG
- {
- get
- {
- if (!bG)
- {
- bG = UnityUtil.GetBreadthChild<XRImage3D>(transform, "BG");
- }
- return bG;
- }
- }
- public XRImage3D Pause
- {
- get
- {
- if (!pause)
- {
- pause = UnityUtil.GetBreadthChild<XRImage3D>(transform, "Pause");
- }
- return pause;
- }
- }
- public VideoPlayer VideoPlayer
- {
- get
- {
- if (!videoPlayer)
- {
- videoPlayer = this.GetComponent<VideoPlayer>();
- }
- return videoPlayer;
- }
- }
- public AVProVideoPlayer AVProVideoPlayer
- {
- get
- {
- if (!avProVideoPlayer)
- {
- avProVideoPlayer = this.GetComponent<AVProVideoPlayer>();
- }
- return avProVideoPlayer;
- }
- }
- public TextMesh TimeText
- {
- get
- {
- if (!timeText)
- {
- timeText = UnityUtil.GetBreadthChild<TextMesh>(transform, "TimeText");
- }
- return timeText;
- }
- }
- public Renderer BodyRender
- {
- get
- {
- if (!bodyRender)
- {
- bodyRender = UnityUtil.GetBreadthChild<Renderer>(transform, "Body");
- }
- return bodyRender;
- }
- }
- /// <summary>
- /// 如果不是全等缩放,则设置厚度
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 厚度变化动效
- /// </summary>
- /// <param name="pressDis">比例,相对于物体厚度的本身比例</param>
- /// <param name="pressTime">变化事件</param>
- 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<XRSlider>(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;
- }
- }
- }
- }
|