首先搭建UI
组件绑定情况:
视频放在StreamingAssets文件夹下:
最后脚本绑定:
FirstFrameImage在视频暂停是显示播放时隐藏,可以做成透明遮罩
Length是视频长度,把分钟转化成秒
最后上脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;public class VideoController : MonoBehaviour
{//视频名称public string videoName;//videoImage组件public GameObject VideoImage;//进度条组件public GameObject SliderController;//AudioSource组件public AudioSource audioSource;//播放按钮public GameObject PlayBtn;//暂停按钮public GameObject PauseBtn;//public GameObject CenterPlayBtn;//视频纹理public RenderTexture renderTexture;//视频第一帧public GameObject FirstFrameImage;//视频时长public float Length;//private bool isDrag = false;// Use this for initializationprivate void Awake(){InitVideo(renderTexture);}void Start(){#region 注册事件PlayBtn.GetComponent().onClick.AddListener(PlayVideo);//CenterPlayBtn.GetComponent().onClick.AddListener(PlayVideo);PauseBtn.GetComponent().onClick.AddListener(PauseVideo);AddTriggersListener(SliderController, EventTriggerType.BeginDrag, OnBeginDrag);AddTriggersListener(SliderController, EventTriggerType.EndDrag, OnEndDrag);#endregion}// Update is called once per framevoid Update(){ChangeSliderValue();ChangeVideoPlayTime();//Debug.Log("Isdrag" + isDrag);if (!gameObject.GetComponentInChildren().isPlaying){PauseVideo();}}/// /// 初始化视频控制器/// public void InitVideoController(){PlayBtn.SetActive(true);//CenterPlayBtn.SetActive(true);PauseBtn.SetActive(false);PauseVideo();}/// /// 初始化videoplayer/// public void InitVideo(RenderTexture renderTexture){DestroyImmediate(gameObject.transform.Find("VideoImage").gameObject.GetComponent());gameObject.transform.Find("VideoImage").gameObject.AddComponent();VideoPlayer videoPlayer = gameObject.GetComponentInChildren();videoPlayer.source = VideoSource.Url;videoPlayer.renderMode = VideoRenderMode.RenderTexture;videoPlayer.playOnAwake = false;videoPlayer.targetTexture = renderTexture;//适配平台if (Application.platform == RuntimePlatform.WebGLPlayer){videoPlayer.url = Application.dataPath + "/StreamingAssets/Video/" + videoName + ".mp4";//实测可播放mp4格式视频}else if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor){videoPlayer.url = Application.dataPath + "/StreamingAssets/Video/" + videoName + ".mp4";}//videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;videoPlayer.Prepare();//设置slider 长度SliderController.GetComponent().maxValue = Length;if (FirstFrameImage != null){FirstFrameImage.SetActive(true);}PauseVideo();}/// /// 播放/// private void PlayVideo(){gameObject.GetComponentInChildren().Play();audioSource.mute = false;PlayBtn.SetActive(false);//CenterPlayBtn.SetActive(false);PauseBtn.SetActive(true);if (FirstFrameImage != null){FirstFrameImage.SetActive(false);}}/// /// 暂停/// public void PauseVideo(){gameObject.GetComponentInChildren().Pause();audioSource.mute = true;PlayBtn.SetActive(true);//CenterPlayBtn.SetActive(true);PauseBtn.SetActive(false);FirstFrameImage.SetActive(true);}/// /// 修改视频播放进度/// private void ChangeVideoPlayTime(){if (isDrag == true){gameObject.transform.Find("VideoImage").gameObject.GetComponent().time = SliderController.GetComponent().value;}}/// /// 修改进度条/// private void ChangeSliderValue(){if (!isDrag){SliderController.GetComponent().value = (float)gameObject.transform.Find("VideoImage").gameObject.GetComponent().time;}}/// /// 开始拖拽/// /// private void OnBeginDrag(BaseEventData enenvt){isDrag = true;}/// /// 结束拖拽/// /// private void OnEndDrag(BaseEventData enenvt){isDrag = false;gameObject.transform.Find("VideoImage").gameObject.GetComponent().time = SliderController.GetComponent().value;}/// /// 为obj添加Eventrigger监听事件/// /// 添加监听的对象/// 添加的监听类型/// 触发的函数private void AddTriggersListener(GameObject obj, EventTriggerType eventType, UnityAction action){//首先判断对象是否已经有EventTrigger组件,若没有那么需要添加EventTrigger trigger = obj.GetComponent();if (trigger == null){trigger = obj.AddComponent();}//实例化delegatesif (trigger.triggers.Count == 0){trigger.triggers = new List();// }//定义所要绑定的事件类型 EventTrigger.Entry entry = new EventTrigger.Entry();//设置事件类型 entry.eventID = eventType;//定义回调函数 UnityAction callback = new UnityAction(action);//设置回调函数 entry.callback.AddListener(callback);//添加事件触发记录到GameObject的事件触发组件 trigger.triggers.Add(entry);}
}
需要测试工程的可参考这篇文章:
Unity发布WebGL播放视频【免插件,可快进后退,复制脚本即可使用】_3D菜鸟张三的博客-CSDN博客