You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
5.0 KiB
187 lines
5.0 KiB
2 years ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.IO;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
using UnityEngine.Video;
|
||
|
using UnityEngine.SceneManagement;
|
||
|
using System;
|
||
|
using UnityEngine.EventSystems;
|
||
|
|
||
|
public class VideoRawImage : MonoBehaviour
|
||
|
{
|
||
|
//定义参数获取VideoPlayer组件和RawImage组件
|
||
|
public string Url;
|
||
|
public VideoPlayer VideoPlayer;
|
||
|
|
||
|
private RawImage rawImage;
|
||
|
|
||
|
public Toggle PlayerToggle;
|
||
|
//public Button StopButton;
|
||
|
public Button CloseButotn;
|
||
|
public Slider ShowProgressSlider;
|
||
|
public Slider ClickProgressSlider;
|
||
|
//private bool isPlayState;
|
||
|
// Use this for initialization
|
||
|
|
||
|
void Awake()
|
||
|
{
|
||
|
|
||
|
//获取场景中对应的组件
|
||
|
|
||
|
VideoPlayer = this.GetComponent<VideoPlayer>();
|
||
|
|
||
|
rawImage = this.GetComponent<RawImage>();
|
||
|
PlayerToggle.onValueChanged.AddListener(PlayerToggleChange);
|
||
|
//StopButton.onClick.AddListener(StopClick);
|
||
|
CloseButotn.onClick.AddListener(CloseButtonClick);
|
||
|
ClickProgressSlider.onValueChanged.AddListener(ProgressChange);
|
||
|
|
||
|
}
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
private void OnEnable()
|
||
|
{
|
||
|
VideoPlayer.url = Url;
|
||
|
if (VideoPlayer.texture == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
VideoPlayer.isLooping = false;
|
||
|
PlayerToggle.isOn = true;
|
||
|
VideoPlayer.time = 0;
|
||
|
rawImage.texture = VideoPlayer.texture;
|
||
|
ClickProgressSlider.value = 0;
|
||
|
ShowProgressSlider.value = 0;
|
||
|
//VideoPlayer.Pause();
|
||
|
}
|
||
|
private void StopClick()
|
||
|
{
|
||
|
VideoPlayer.Stop();
|
||
|
}
|
||
|
|
||
|
private void OnDestroy()
|
||
|
{
|
||
|
PlayerToggle.onValueChanged.RemoveListener(PlayerToggleChange);
|
||
|
CloseButotn.onClick.RemoveListener(CloseButtonClick);
|
||
|
ClickProgressSlider.onValueChanged.RemoveListener(ProgressChange);
|
||
|
//StopButton.onClick.RemoveListener(StopClick);
|
||
|
}
|
||
|
|
||
|
private void ProgressChange(float value)
|
||
|
{
|
||
|
if (VideoPlayer.texture == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
if (VideoPlayer.time >= VideoPlayer.length)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
if (value > 0.98)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
//PlayerToggle.isOn = false;
|
||
|
ShowProgressSlider.value = value;
|
||
|
VideoPlayer.time = VideoPlayer.length * value;
|
||
|
StopAllCoroutines();
|
||
|
StartCoroutine(StopSliderClick());
|
||
|
//StartCoroutine(CheckValue());
|
||
|
}
|
||
|
IEnumerator StopSliderClick()
|
||
|
{
|
||
|
ClickProgressSlider.interactable = false;
|
||
|
yield return new WaitForSeconds(1f);
|
||
|
ClickProgressSlider.interactable = true;
|
||
|
}
|
||
|
IEnumerator CheckValue()
|
||
|
{
|
||
|
yield return new WaitForSeconds(0.5f);
|
||
|
if (Mathf.Abs(ShowProgressSlider.value - ClickProgressSlider.value) > 0.05)
|
||
|
{
|
||
|
Debug.Log("报错卡住了");
|
||
|
VideoPlayer.url = Url;
|
||
|
rawImage.texture = VideoPlayer.texture;
|
||
|
|
||
|
PlayerToggle.isOn = true;
|
||
|
yield return new WaitForSeconds(0.1f);
|
||
|
|
||
|
VideoPlayer.time = VideoPlayer.length * ClickProgressSlider.value;
|
||
|
ShowProgressSlider.value = ClickProgressSlider.value;
|
||
|
PlayerToggle.isOn = false;
|
||
|
}
|
||
|
}
|
||
|
private void CloseButtonClick()
|
||
|
{
|
||
|
transform.parent.gameObject.SetActive(false);
|
||
|
}
|
||
|
|
||
|
|
||
|
private void PlayerToggleChange(bool value)
|
||
|
{
|
||
|
PlayerToggle.transform.Find("Play").gameObject.SetActive(!value);
|
||
|
PlayerToggle.transform.Find("Stop").gameObject.SetActive(value);
|
||
|
if (value)
|
||
|
{
|
||
|
if (Mathf.Abs(ShowProgressSlider.value - ClickProgressSlider.value) > 0.1)
|
||
|
{
|
||
|
VideoPlayer.url = Url;
|
||
|
rawImage.texture = VideoPlayer.texture;
|
||
|
}
|
||
|
//ClickProgressSlider.value = ShowProgressSlider.value;
|
||
|
//VideoPlayer.url = Url;
|
||
|
VideoPlayer.Play();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
VideoPlayer.Pause();
|
||
|
//ClickProgressSlider.value = ShowProgressSlider.value;
|
||
|
}
|
||
|
ClickProgressSlider.value = ShowProgressSlider.value;
|
||
|
//isPlayState = value;
|
||
|
}
|
||
|
|
||
|
// Update is called once per frame
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
|
||
|
//如果videoPlayer没有对应的视频texture,则返回
|
||
|
|
||
|
if (VideoPlayer.texture == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
//把VideoPlayerd的视频渲染到UGUI的RawImage
|
||
|
|
||
|
rawImage.texture = VideoPlayer.texture;
|
||
|
//Debug.Log(VideoPlayer.time);
|
||
|
//Debug.Log(VideoPlayer.length);
|
||
|
//if (PlayerToggle.isOn)
|
||
|
{
|
||
|
if (VideoPlayer.time < VideoPlayer.length)
|
||
|
{
|
||
|
ShowProgressSlider.value = (float)VideoPlayer.time / (float)VideoPlayer.length;
|
||
|
//ClickProgressSlider.value = ShowProgressSlider.value;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
VideoPlayer.time = 0;
|
||
|
ShowProgressSlider.value = 0;
|
||
|
PlayerToggle.isOn = false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|