unity 脚本控制多个视频播放,暂停与切换。

1打开unity,新建一个场景,创建 GameObject-----UI-----RawImage.并且创建三个按钮分别为暂停与播放按钮,上一个,下一个,两个切换按钮。调整好合适的大小和位置。

2.下载好两个以上需要切换的视频,注意修改格式为MP4格式。接下来就导入视频资源。打开unity -----直接将视频拖入Assets界面。

3.编写脚本对视频的播放进行控制。

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

public class Video_Controller : MonoBehaviour {
    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;//定义了一个数组
	// Use this for initialization
	void Start () {
        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;
        }
            rawImage.texture = videoplayer.texture;
        
        
		
	}
    private void OnplayorpauseVideo() {
        if (videoplayer.enabled == true)
       {
            if (videoplayer.isPlaying) { 
                videoplayer.Pause();
            text_playorpause.text = "播放";
            Debug.Log("2322");//用于调试脚本不能正常运行

           }
          else if (!videoplayer.isPlaying)
          {
            videoplayer.Play();
            Debug.Log("111");
            text_playorpause.text = "暂停";
          }
        }
    }
    private void OnpreVideo() {
        currentClipIndex -= 1;
        if (currentClipIndex < 0) {
            currentClipIndex = videoClips.Length - 1;
        }
        videoplayer.clip = videoClips[currentClipIndex];
        text_playorpause.text = "暂停";
        }
    private void OnnextVideo()
    {
        currentClipIndex += 1;
        currentClipIndex = currentClipIndex % videoClips.Length;
        videoplayer.clip = videoClips[currentClipIndex];
        text_playorpause.text = "暂停";
    }
    }
    

4.把脚本挂在RawImage上。并给脚本赋予相应的按钮。

 

 

 最后点击运行就可以实现视频的播放暂停,和切换。

5.思路方法总结:

    多个视频播放,要判断视频的播放状态,转化按钮的功能。要实现视频播放和暂停videoplayer.play();和video.pause();的方法;就需要运用到videoplayer.enabled(bool),videoplayer.isplaying或!videoplayer.is playing等的布尔类型来进行判断。

创建一个数组来存储视频。public VideoClip[] video Clips.自定义了暂停与播放,上一个,下一个三个方法。

  • 14
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Unity中,你可以使用Video Player组件来控制视频播放。如果你想切换两个视频播放,可以在场景中创建两个Video Player对象,然后在需要切换的时候,暂停当前正在播放视频,然后将另一个Video Player对象的视频路径设置为下一个要播放视频文件路径,并开始播放。 以下是一个基本的示例代码: ```csharp using UnityEngine; using UnityEngine.Video; public class VideoController : MonoBehaviour { public VideoPlayer videoPlayer1; public VideoPlayer videoPlayer2; private bool isPlaying1 = true; void Start() { // 开始播放第一个视频 videoPlayer1.Play(); videoPlayer2.Pause(); } void Update() { // 检测是否需要切换视频 if (Input.GetKeyDown(KeyCode.Space)) { // 暂停当前正在播放视频 if (isPlaying1) { videoPlayer1.Pause(); } else { videoPlayer2.Pause(); } // 切换到另一个视频 if (isPlaying1) { videoPlayer2.url = "path/to/next/video"; videoPlayer2.Play(); isPlaying1 = false; } else { videoPlayer1.url = "path/to/next/video"; videoPlayer1.Play(); isPlaying1 = true; } } } } ``` 在这个示例代码中,我们创建了两个Video Player对象(videoPlayer1和videoPlayer2),并在Start()方法中开始播放第一个视频。然后,在Update()方法中检测是否需要切换视频,如果需要,我们就暂停当前正在播放视频,然后将另一个Video Player对象的视频路径设置为下一个要播放视频文件路径,并开始播放

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值