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

unity 暂停按钮

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

unity 暂停按钮

1. 创建Canvas--RawImage。

2. 导入视频资源(在Assets目录下新建Video文件夹),直接将视频拖入Video文件夹。

3. 将导入的视频拖入第1步新建的RawImage,RawImage右边的Inspector栏将自动出现Video Player。

将导入的视频拖入Video Clip。

4. 创建按钮Button,text改成  暂停。

5. 创建C#脚本Movie,控制视频播放与暂停。脚本如下:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.Video;

using UnityEngine.UI;

public class Movie : MonoBehaviour

{

public Text text_PlayOrPause;

public Button button_PlayOrPause;

private VideoPlayer videoPlayer;

private RawImage rawImage;

private int flag = 0;

//private AudioSource audioSource;

// Start is called before the first frame update

void Start()

{

videoPlayer = this.GetComponent();

//audioSource = this.GetComponent();

rawImage = this.GetComponent();

button_PlayOrPause.onClick.AddListener(PlayorPause);

}

void Update()

{

//判断视频播放情况,播放则按钮显示暂停,暂停就显示播放,并更新相关文本

//没有视频则返回,不播放

if (flag == 0)

{

if (videoPlayer.texture == null)

{

return;

}

//渲染视频到UGUI

else

{

rawImage.texture = videoPlayer.texture;

flag++;

}

}

}

void PlayorPause()

{

if (videoPlayer.isPlaying == true)

{

videoPlayer.Pause();

//audioSource.Pause();

text_PlayOrPause.text = "播放";

}

else

{

videoPlayer.Play();

//audioSource.Play();

text_PlayOrPause.text = "暂停";

}

}

}

6. 将脚本挂载到Canvas--RawImage并设置相关参数,将第4步创建的Button拖入Button_Play Or Pause,Button下的Text拖入Text_Play Or Pause,如下图。

7. 运行。

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

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

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