栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

【Unity】代码控制视频的播放(视频播放器)

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Unity】代码控制视频的播放(视频播放器)

结果如上图。。。

如图所示,里面有开始,暂停,重播,关闭按钮,同时有一个播放视频的进度条,以及后面两个显示时间总长度和实时时间的文本,以及中间播放视频的RawImage。

上一次写了如何设置RawImage进行自动播放,这次就直接用代码控制,同时还有进度条的控制。

可以看一下上一次的这篇文章:

CSDNhttps://mp.csdn.net/mp_blog/creation/editor/120648989

在写代码之前,还需要把按钮和组件相对的设置好,以及RawImage的设置调整好

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

public class VideoCrol : MonoBehaviour {

	public static VideoCrol instance;

	private VideoPlayer vPlayer;

	public GameObject videoImage;//播放image
	private Button BtnPlay, BtnPause, BtnReStart;//开始,暂停,重播
	public Slider sliderVideo;//进度条
	private Button BtnX;//关闭
	private Image VideoPanel;//视频背景
	private Text NowTime;//播放时间
	private Text TotalTime;//总时间

	private float tt;//视频总时长
	private float Index_t;//进度条计时时间

	private float hour, min, second;

	private bool IsPlay = true;

	void Awake()
	{
		instance = this;
		vPlayer = videoImage.GetComponent();
		BtnPlay = GameObject.Find("Kaishi").GetComponent

同时还有另外一个脚本用来控制Slider的拖动和拖拽(两个接口,拖动和点击)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class SliderChange : MonoBehaviour,IDragHandler,IPointerClickHandler
{
    /// 
    /// 拖动改变视频进度
    /// 
    /// 
    public void OnDrag(PointerEventData eventData)
    {
        VideoCrol.instance.ChangeVideo(VideoCrol.instance.sliderVideo.value);
    }
    /// 
    /// 点击改变视频进度
    /// 
    /// 
    public void OnPointerClick(PointerEventData eventData)
    {
        VideoCrol.instance.ChangeVideo(VideoCrol.instance.sliderVideo.value);
    }
}

在最后写完代码,组件做完之后,视频和参数等也需要调整一下

链接:https://pan.baidu.com/s/1SnSxuDa7o8nwnkj4EoikFg 
提取码:1ac0 
--来自百度网盘超级会员V5的分享

把整体打包了一个,包含代码和组件,可以看一下(上面的是打包好的网盘链接)

视频播放器更新了喇叭控制音量。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

public class VideoCro : MonoBehaviour {

	public static VideoCro instance;

	private VideoPlayer vPlayer;

	public GameObject videoImage;//播放image
	private Button BtnPlay, BtnPause, BtnReStart;//开始,暂停,重播
	public Slider sliderVideo;//进度条
	//private Button BtnX;//关闭
	//private Image VideoPanel;//视频背景
	private Text NowTime;//播放时间
	private Text TotalTime;//总时间

	private float tt;//视频总时长
	private float Index_t;//进度条计时时间

	private float hour, min, second;

	private bool IsPlay = true;
	
	private Slider Audio_Slider;//声音进度条
	private Text AudioNum;//声音数字显示
	public AudioSource audioSource;//声音播放器
	private GameObject audioGameObject;//声音及数字整体组件
	private Button Btn_Audio;//喇叭按钮
	bool a = true;//控制喇叭是否显示

	void Awake()
	{
		instance = this;
		vPlayer = videoImage.GetComponent();
		BtnPlay = transform.Find("Kaishi").GetComponent

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/897781.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号