赞
踩
本文提供详细教程 记录遇到的难点并帮助同行的朋友们 坚持以最简单的方法传授和把更好的阅读体验带给你们! |
1,在空的场景里创建RawImage,并在此基础上添加VideoPlayer组件
1,标清所起的作用,并把位置设置好
这里我找的素材比较多,5个了
using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; /// <summary> /// Jerry代码编写 /// </summary> public class VideoController : MonoBehaviour { //设置VideoPlayer、RawImage和当前播放视频索引参数 private VideoPlayer videoPlayer; private RawImage rawImage; private int currentClipIndex; //设置相关文本和按钮参数以及视频列表 public Text text_PlayOrPause; public Button button_PlayOrPause; public Button button_Pre; public Button button_Next; public VideoClip[] videoClips; void Start() { //获取VideoPlayer和RawImage组件,以及初始化当前视频索引 videoPlayer = this.GetComponent<VideoPlayer>(); rawImage = this.GetComponent<RawImage>(); currentClipIndex = 0; //设置相关按钮监听事件 button_PlayOrPause.onClick.AddListener(OnPlayOrPauseVideo); button_Pre.onClick.AddListener(OnPreVideo); button_Next.onClick.AddListener(OnNextVideo); } // Update is called once per frame void Update() { //没有视频则返回,不播放 if (videoPlayer.texture == null) { return; } //渲染视频到UGUI上 rawImage.texture = videoPlayer.texture; } /// <summary> /// 播放和暂停当前视频 /// </summary> private void OnPlayOrPauseVideo() { //判断视频播放情况,播放则暂停,暂停就播放,并更新相关文本 if (videoPlayer.isPlaying == true) { videoPlayer.Pause(); text_PlayOrPause.text = "播放"; } else { videoPlayer.Play(); text_PlayOrPause.text = "暂停"; } } /// <summary> /// 切换上一个视频 /// </summary> private void OnPreVideo() { //视频列表减一播放上一个视频,并进行避免越界操作 currentClipIndex -= 1; if (currentClipIndex < 0) { currentClipIndex = videoClips.Length - 1; } videoPlayer.clip = videoClips[currentClipIndex]; text_PlayOrPause.text = "暂停"; } /// <summary> /// 切换下一个视频 /// </summary> private void OnNextVideo() { //视频列表加一播放下一个视频,并进行避免越界操作 currentClipIndex += 1; currentClipIndex = currentClipIndex % videoClips.Length; videoPlayer.clip = videoClips[currentClipIndex]; text_PlayOrPause.text = "暂停"; } }
拥有自己的服务器 让开发工作不再难 |
点击此处领取----阿里产品优惠券大礼包 (新手必得享超值优惠)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。