Unity——播放视频
来源:互联网 发布:网络支付平台问题案例 编辑:程序博客网 时间:2024/05/16 12:01
注:此方法播放视频仅支持ogv格式。
由于此播放视频的方法只支持ogv格式视频,而我们常见的视频格式大多是mp4、flv、avi等格式。所以我们需要对视频进行格式转换。
而格式转换所使用的工具为:Theora Converter .NET
将需要转换的视频拖入箭头指向区域,完成转换输出配置后,点击start即开始转换格式。
将转换好的视频文件放入资源库里,创建视频管理类
using UnityEngine;using System.Collections;using System;using Assets.Scripts.UI;public class MovieManage: SingletonUI<MovieManage>{ public GameObject FigerGestures; private string rootPath = "File_Movie/";//存放音乐的地址 private MovieTexture movieTexture; private UITexture uiTexture; private AudioSource as1; private int State = 1;//0为视频暂停状态;1为视频播放状态; private Action<string> action; private bool checkFlag = false; private string currentPlayMovieName; private int width = 0; private int height = 0; private bool SPFlag = true; private bool autouShut; private Transform uicamera = null; public void setAction(Action<string> action) { this.action = action; }// Use this for initializationvoid Start () { uicamera = GameObject.Find("UI Root").transform.FindChild("Camera"); }// Update is called once per framevoid Update () { if (movieTexture != null && checkFlag) { if (SPFlag) { if (!movieTexture.isPlaying) { if(action != null) action(this.currentPlayMovieName); checkFlag = false; movieTexture.Stop(); if(autouShut) DestroyMovie();// enabled = false;//任务执行完毕:使得该类不再继续执行update方法 } } } } /// <summary> /// 播放视频 /// </summary> /// <param name="movie"></param> public void PlayMovie(string movieName,bool autouShut_= true) { width = 1920; height = 1080; MovieManage sp = GameObject.Find("Movie").GetComponent<MovieManage>(); if (sp == null) sp = GameObject.Find("Movie").AddComponent<MovieManage>(); if (!sp.enabled) { sp.enabled = true; //任务开始执行执行:使得该类开始执行update方法 } this.autouShut = autouShut_; if (uiTexture == null) { Create(movieName); } if (movieTexture != null) { movieTexture.Play(); if (movieTexture.audioClip != null) { uiTexture.gameObject.AddComponent<AudioSource>(); var as_array = uiTexture.gameObject.GetComponents(typeof(AudioSource)); as1 = (AudioSource)as_array[0]; as1.clip = (AudioClip)movieTexture.audioClip; as1.Play(); } } checkFlag = true; SPFlag = true; } /// <summary> /// 停止播放 /// </summary> public void StopMovie() { if (movieTexture.audioClip != null) { as1.Stop(); } SPFlag = false; movieTexture.Stop(); } /// <summary> /// 继续播放 /// </summary> public void ContinueMovie() { State = 1; movieTexture.Play(); SPFlag = true; if (movieTexture.audioClip != null) { as1.Play(); } } /// <summary> /// 暂停视频 /// </summary> public void PauseMovie() { State = 0; movieTexture.Pause(); SPFlag = false; if (movieTexture.audioClip != null) { as1.Pause(); } } /// <summary> /// 销毁视频及其相应组件 /// </summary> /// <param name="movieName"></param> public void DestroyMovie() { if (uiTexture != null) { State = 1; Transform uicamera = GameObject.Find("UI Root").transform.FindChild("Camera"); uicamera.GetComponent<UIData>().VideoControl.SetActive(false); uicamera.GetComponent<UIData>().BlackBoard.SetActive(false); movieTexture.Stop(); Destroy(transform.FindChild("tt").gameObject); checkFlag = false; action = null; } if (enabled) enabled = false; } /// <summary> /// 设置音量 /// </summary> /// <param name="volume"></param> public void SetVolume(float volume) { if (movieTexture != null) { if (movieTexture.audioClip != null) { as1.volume = volume; } } } private void Create(string movieName) { movieTexture = (MovieTexture)Resources.Load(rootPath + movieName, typeof(MovieTexture)); if (movieTexture != null) { this.currentPlayMovieName = movieName; uiTexture = NGUITools.AddChild<UITexture>(this.gameObject); uiTexture.name = "tt"; Transform uicamera = GameObject.Find("UI Root").transform.FindChild("Camera"); uiTexture.transform.localPosition = new Vector3(0, 0, 0); uiTexture.transform.localPosition = new Vector3(0, 0, 0); uiTexture.width = width; uiTexture.height = height; //动态添加boxcollider uiTexture.gameObject.AddComponent<BoxCollider>(); //在这里资源加载,movie是视频的文件名不需要后缀名 uiTexture.mainTexture = movieTexture; } } }
在Unity中使用NGUI插件创建名为Movie的UIPanel组件。
将视频管理类脚本挂在组件上。
创建Main方法:
using UnityEngine;using System.Collections;public class main1 : MonoBehaviour { private MovieManage mm;// Use this for initializationvoid Start () { mm = GameObject.Find("Movie").GetComponent<MovieManage>(); mm.PlayMovie("120");}// Update is called once per framevoid Update () {}}
0 0
- Unity——播放视频
- Unity+高通Vuforia SDK——AR播放视频
- Unity VR——全景视频播放方案
- Unity VR——全景视频播放方案
- Unity+高通Vuforia SDK——AR播放视频
- unity—NGUI播放视频及声音注意事项
- unity 播放视频
- unity播放视频代码
- Unity播放视频
- Unity中视频播放
- Unity 视频播放杂谈
- unity视频播放,
- unity视频播放
- Unity视频播放插件
- Unity播放视频插件
- 视频播放(三)——视频播放
- Unity移动端播放视频
- Unity播放视频方法汇总
- NYOJ 擅长排列的小明
- java中正则表达式的一些应用和总结
- GB28181各大平台对接之各种填坑
- 一、solr服务搭建
- Error:CreateProcess error=216, 该版本的 %1 与您运行的 Windows 版本不兼容。请查看计算机的系统信息,了解是否需要 x86 (32 位)或 x64 (64 位)
- Unity——播放视频
- 渣渣
- U3D导出obj模型
- 阿里巴巴全球速卖通(AliExpress)参数签名算法
- 迁移学习和微调深度卷积神经网络
- bootstrap table系列:
- The last update operation tried to add the file 'xxx.java',but the file already exists universioned
- GB28181平台对接之填坑
- 使用Dagger 2 依赖注入