如何在UnityUI上播放手机视频在电脑上播放

如何在Unity中播放视频?
提要:在Unity 中 视频支持.mov、.mpg、.mpeg、.mp4、.avi和.asf。而Unity对于视频都是以MovieTexture来使用的。
如果视频中有声音那么在视频文件下面会相应生成audio文件。所以一个完整的视频播放是应该画面和音频的同步播放。
方法一:基于GameObject上,这种方式可以让影片以任何方式播放,比如你要实现一个教室,教室讲台上有个投影幕,电影应当在投影幕上播放。这样影片播放就是实打实的在荧幕这个游戏GameObject上面播放。
实现方法:就好比在游戏世界中创建一个Plane面对象,摄像机直直的照射在这个面,首先你要在播放的地方加个plane。上面挂一个 Play 脚本。
using UnityE
using System.C
public class Play : MonoBehaviour
//电影纹理
public MovieTexture movT //播放视频要用到的MovieTexture属于贴图Texture的子类
public AudioSource audioS
void Start()
//设置当前对象的主纹理为电影纹理
renderer.material.mainTexture = movT
//设置电影纹理播放模式为循环
movTexture.loop =
//如果有声音才播放声音
audioSource = GetComponent&AudioSource&();
public void OnPlay()
{&pre name="code" class="csharp"&
&span style="white-space:pre"& &/span&if(!movTexture.isPlaying)&span style="white-space:pre"& &/span&{&pre name="code" class="csharp"& &span style="white-space:pre"& &/span&
movTexture.Play();
audioSource.Play();
&span style="white-space:pre"& &/span&} } public void OnPause() { movTexture.Pause(); // audioSource.Pause(); } public void OnStop() { movTexture.Stop(); // audioSource.Stop(); }}
我们将视频生成的MovieTexture拖放复制到movTexture中,如果有声音,应当给plane添加AudioSource组件并把把生成的音频文件拖放到AudioClip处。脚本中给出了三个函数分别对应的是视频的播放、暂停、停止。方法二:基于Unity 的OnGUI实现,这样视频就是以UI元素存在于游戏中。
实现方法:这种方法不需要载体。直接可以将脚本挂在Mamera上。
using UnityE
using System.C
public class Play : MonoBehaviour
//电影纹理
public MovieTexture movT
public AudioSource audioS
void Start()
//设置电影纹理播放模式为循环
movTexture.loop =
//如果有声音才播放声音
audioSource = GetComponent&AudioSource&();
void OnGUI()
//绘制电影纹理
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), movTexture, ScaleMode.StretchToFill);
public void OnPlay()
movTexture.Play();
// audioSource.Play();
public void OnPause()
movTexture.Pause();
// audioSource.Pause();
public void OnStop()
movTexture.Stop();
// audioSource.Stop();
//在移动设备上播放视频
public class Test : MonoBehaviour {
void OnGUI()
if (GUI.Button (new Rect (20,10,200,50), "PLAY ControlMode.CancelOnTouch"))
Handheld.PlayFullScreenMovie("test.mp4", Color.black, FullScreenMovieControlMode.CancelOnInput);
if (GUI.Button (new Rect (20,90,200,25), "PLAY ControlMode.Full"))
Handheld.PlayFullScreenMovie("test.mp4", Color.black, FullScreenMovieControlMode.Full);
if (GUI.Button (new Rect (20,170,200,25), "PLAY ControlMode.Hidden"))
Handheld.PlayFullScreenMovie("test.mp4", Color.black, FullScreenMovieControlMode.Hidden);
if (GUI.Button (new Rect (20,250,200,25), "PLAY ControlMode.Minimal"))
Handheld.PlayFullScreenMovie("test.mp4", Color.black, FullScreenMovieControlMode.Minimal);
}直接使用第三方插件AVPro QuickTime来播放视频。
没有更多推荐了,只有游戏编程不会其他的
Unity在UGUI上播放视频的办法
先在Canvas添加RawImage组件。
然后在RawImage挂载如下脚本。
接下来使用TheoraConverter.NET 1.1 将mp4 转成ogv。这样就免去类Unity要安装movetime player,quictime什么一大堆莫名问题。(来源于网上)。
然后将该ogv视频加入资源。将该资源直接拖到如上脚本的movTexture即可。
没有更多推荐了,using UnityE
using System.C
using System.Collections.G
using System.IO;
using UnityEngine.V
using UnityEngine.UI;
public class VideoPlayerControl : MonoBehaviour
//public static VideoPlayerControl I
public RawImage m_
public VideoPlayer m_vP
public Button m_btnP
public Button m_btnP
public Button m_btnVolumnOn;
public Button m_btnVolumnO
//视频控制器
public Slider m_sliderV
//音量控制器
public Slider m_sliderS
//当前视频时间
public Text m_textT
public TextMeshProUGUI m_videoN
//视频总时长
public Text m_textC
//音频组件
public AudioSource m_
//需要添加播放器的物体
public GameObject m_
//是否拿到视频总时长
public bool m_isS
//前进后退的大小
public float m_numBer = 20f;
//时 分的转换
private int hour,
private float m_
private float m_timeC
private float m_timeC
//视频是否播放完成
private bool m_isV
bool isFullScreen = false;
//隐藏视频控制按钮区域
public GameObject m_videoControlA
protected bool m_isShowUI;
[SerializeField]
public TreeView m_T
protected Vector2 m_videoAreaS
protected Vector3 m_videoAreaA
public GameObject m_fullScreenB
public GameObject m_unFullScreenB
void Awake()
//Instance =
GetVideoName();
void Start()
m_image = m_obj.GetComponent&RawImage&();
//一定要动态添加这两个组件,要不然会没声音
m_vPlayer = m_obj.AddComponent&VideoPlayer&();
m_source = m_obj.AddComponent&AudioSource&();
//这3个参数不设置也会没声音 唤醒时就播放关闭
m_vPlayer.playOnAwake = false;
m_source.playOnAwake = false;
m_source.Pause();
m_videoControlArea.SetActive(false);
//Init(urlNetWork);
m_btnPlay.onClick.AddListener(delegate { OnClick(<span style="color: #); });
m_btnPause.onClick.AddListener(delegate { OnClick(<span style="color: #); });
m_btnVolumnOn.onClick.AddListener(delegate { OnClick(<span style="color: #); });
m_btnVolumnOff.onClick.AddListener(delegate { OnClick(<span style="color: #); });
m_sliderSource.value = m_source.
//text.text = string.Format("{0:0}%", source.volume * 100);
m_sliderSource.onValueChanged.AddListener(delegate { ChangeSource(m_sliderSource.value); });
//全屏的默认值
m_videoAreaSize = m_obj.GetComponent&RectTransform&().sizeD
m_videoAreaAnchor = m_obj.GetComponent&RectTransform&().anchoredPosition3D;
void Update()
if (m_vPlayer.isPlaying && m_isShow)
//把图像赋给RawImage
m_image.texture = m_vPlayer.
//帧数/帧速率=总时长
如果是本地直接赋值的视频,我们可以通过VideoClip.length获取总时长
m_sliderVideo.maxValue = (m_vPlayer.frameCount / m_vPlayer.frameRate);
m_time = m_sliderVideo.maxV
hour = (int)m_time / <span style="color: #;
mint = (int)m_time % <span style="color: #;
m_textCount.text = string.Format("{0:D2}:{1:D2}", hour.ToString(), mint.ToString());
m_isShow = !m_isS
if (Mathf.Abs((int)m_vPlayer.time - (int)m_sliderVideo.maxValue) == <span style="color: #)
m_vPlayer.frame = (long)m_vPlayer.frameC
m_vPlayer.Pause();
m_isVideo = false;
else if (m_isVideo && m_vPlayer.isPlaying)
m_time = (float)m_vPlayer.
hour = (int)m_time / <span style="color: #;
mint = (int)m_time % <span style="color: #;
m_textTime.text = string.Format("{0:D2}:{1:D2}", hour.ToString(), mint.ToString());
m_sliderVideo.value = m_
/// &summary&
/// 获取文件夹下的文件列表
/// &/summary&
/// &param name="path"&&/param&
private void GetVideoName()
Configuration cfg = ConfigManager.GetConfig("AppConfig.xml");
cfg.Get("VideoPath", out path);
if (!Directory.Exists(path))
Debug.LogError("教学视频路径不存在!");
DirectoryInfo dir = new DirectoryInfo(path);
m_Tree.Nodes = InitTreeList(dir);
m_Tree.gameObject.transform.Find("ScrollRect").GetComponent&ScrollRect&().horizontal = true;
/// &summary&
/// 初始化视频文件列表
/// &/summary&
private ObservableList&TreeNode&TreeViewItem&& InitTreeList(DirectoryInfo dir)
ObservableList&TreeNode&TreeViewItem&& nodes = new ObservableList&TreeNode&TreeViewItem&&();
FileInfo[] files = dir.GetFiles();
for (int i = <span style="color: #; i & files.L i++)
FileInfo file = files[i];
TreeViewItem item = new CustomTreeViewItem(file.Name, null,file.FullName);
TreeNode&TreeViewItem& node = new TreeNode&TreeViewItem&(item, null);
nodes.Add(node);
DirectoryInfo[] childDirList = dir.GetDirectories();
for (int i = <span style="color: #; i & childDirList.L i++)
DirectoryInfo childDir = childDirList[i];
ObservableList&TreeNode&TreeViewItem&& childDirNodes = InitTreeList(childDir);
TreeViewItem item = new CustomTreeViewItem(childDir.Name, null, null);
TreeNode&TreeViewItem& childNode = new TreeNode&TreeViewItem&(item, childDirNodes);
nodes.Add(childNode);
/// &summary&
/// 播放视频
/// &/summary&
/// &param name="id"&&/param&
public void PlayVideo(string fullName)
Init(fullName);//@"E:\互动教学视频" +@"\"
public void OnSelect(int index, ListViewItem item)
TreeViewComponent componentItem = item as TreeViewC
CustomTreeViewItem customItem = componentItem.Item as CustomTreeViewI
//Debug.Log("Selected: " + index + "; name: " + customItem.Name + "; value: " + customItem.Value + "; data: " + customItem.Data);
if (customItem.Data != null)
Init(customItem.Data);
m_videoName.text = customItem.N
/// &summary&
初始化VideoPlayer
/// &/summary&
/// &param name="url"&&/param&
private void Init(string url)
m_isVideo = true;
m_isShow = true;
m_timeCount = <span style="color: #;
m_timeCurrent = <span style="color: #;
m_sliderVideo.value = <span style="color: #;
//设置为URL模式
m_vPlayer.source = VideoSource.U
//设置播放路径
m_vPlayer.url =
//在视频中嵌入的音频类型
m_vPlayer.audioOutputMode = VideoAudioOutputMode.AudioS
//把声音组件赋值给VideoPlayer
m_vPlayer.SetTargetAudioSource(<span style="color: #, m_source);
m_vPlayer.controlledAudioTrackCount = <span style="color: #;
m_vPlayer.IsAudioTrackEnabled(<span style="color: #);
//当VideoPlayer全部设置好的时候调用
m_vPlayer.prepareCompleted += P
//启动播放器
m_vPlayer.Prepare();
// player.Play();
/// &summary&
改变音量大小
/// &/summary&
/// &param name="value"&&/param&
public void ChangeSource(float value)
m_source.volume =
// text.text = string.Format("{0:0}%", value * 100);
/// &summary&
改变视频进度
/// &/summary&
/// &param name="value"&&/param&
public void ChangeVideo()
if (m_vPlayer.isPrepared)
m_vPlayer.time = (long)m_sliderVideo.
//Debug.Log((long)value);
//Debug.Log("VideoPlayer Time:" + m_vPlayer.time);
m_time = (float)m_vPlayer.
hour = (int)m_time / <span style="color: #;
mint = (int)m_time % <span style="color: #;
m_textTime.text = string.Format("{0:D2}:{1:D2}", hour.ToString(), mint.ToString());
if (m_isVideo == false)
m_isVideo = true;
m_vPlayer.Play();
private void OnClick(int num)
switch (num)
case <span style="color: #:
m_vPlayer.Play();
m_btnPlay.gameObject.SetActive(false);
m_btnPause.gameObject.SetActive(true);
Time.timeScale = <span style="color: #;
case <span style="color: #:
m_vPlayer.Pause();
m_btnPlay.gameObject.SetActive(true);
m_btnPause.gameObject.SetActive(false);
Time.timeScale = <span style="color: #;
case <span style="color: #:
m_source.volume = <span style="color: #;
m_btnVolumnOn.gameObject.SetActive(false);
m_btnVolumnOff.gameObject.SetActive(true);
case <span style="color: #:
m_source.volume = m_sliderSource.
m_btnVolumnOn.gameObject.SetActive(true);
m_btnVolumnOff.gameObject.SetActive(false);
void Prepared(VideoPlayer player)
player.Play();
/// &summary&
/// 当鼠标进入到下方进度条区域时显示进度条
/// &/summary&
public void OnControllAreaPointEnter()
if (!m_isShowUI)
m_videoControlArea.SetActive(true);
m_isShowUI = true;
/// &summary&
/// 当鼠标离开下方进度条区域时隐藏进度条
/// &/summary&
public void OnControllAreaPointExit()
if (m_isShowUI)
m_videoControlArea.SetActive(false);
m_isShowUI = false;
public void OnFullScreenBtnClick()
if (!isFullScreen && m_vPlayer.isPrepared)
var rt = m_image.GetComponent&RectTransform&();
RectTransform fullScreen = GameObject.Find("MainUI").GetComponent&RectTransform&();
//m_image.GetComponent&RectTransform&().sizeDelta = new Vector2(Screen.width, Screen.height);
rt.localScale = new Vector3(fullScreen.rect.width / rt.rect.width, fullScreen.rect.height / rt.rect.height, <span style="color: #);
rt.position = fullScreen.
isFullScreen = true;
m_fullScreenBtn.SetActive(false);
m_unFullScreenBtn.SetActive(true);
m_image.GetComponent&RectTransform&().localScale = Vector3.
m_image.GetComponent&RectTransform&().anchoredPosition3D = m_videoAreaA
isFullScreen = false;
m_fullScreenBtn.SetActive(true);
m_unFullScreenBtn.SetActive(false);
public void OnSelectTab(int id)
if (id == <span style="color: #)
//m_tree.SelectNode(m_tree.DataSource[0].Node);
if (m_Tree.SelectedNode != null)
m_Tree.DeselectNode(m_Tree.SelectedNode);
阅读(...) 评论()Unity 5游戏UI用户界面制作视频教程_腾讯视频
三倍流畅播放
1080P蓝光画质
新剧提前看
1080P蓝光画质
纯净式无框播放器
三倍流畅播放
扫一扫 手机继续看
下载需先安装客户端
{clientText}
客户端特权:
3倍流畅播放
当前播放至 {time}
扫一扫 手机继续看
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要
副标题要不要现在我有一个MP4格式的视频,怎样把它插进unity3d里???_百度知道
现在我有一个MP4格式的视频,怎样把它插进unity3d里???
不要贴网上的纳差UN代码给我,我已经看过尝试过,不成功,我想有具体的,一步步的步骤给我,有图就更好啦,因为我说初学者。
我有更好的答案
public MovieTexture movT
void Start() {
//设置当前对象的主纹理为电影纹理
renderer.material.mainTexture = movT
//设置电影纹理播放模式为循环
movTexture.loop =
void OnGUI()
if(GUILayout.Button(&播放/继续&))
//播放/继续播放视频
if(!movTexture.isPlaying)
movTexture.Play();
if(GUILayout.Button(&暂停播放&))
//暂停播放
movTexture.Pause();
if(GUILayout.Button(&停止播放&))
//停止播放
movTexture.Stop();
}把这代码拖给一个压扁的cube,然后把MP4拖给这个脚本上的movTexture,运行,点击播放就可以了,不过你的电脑上必须安装quicktime播放器,视频才能转化成unity可以使用的,没有quicktime好像只能播放ogg的视频。上面是吧视频当作cube的纹理,下面是利用ui原理播放的,//电影纹理
public MovieTexture movT
void Start()
//设置电影纹理播放模式为循环
movTexture.loop =
void OnGUI()
//绘制电影纹理
GUI.DrawTexture (new Rect (0,0, Screen.width, Screen.height),movTexture,ScaleMode.StretchToFill);
if(GUILayout.Button(&播放/继续&))
//播放/继续播放视频
if(!movTexture.isPlaying)
movTexture.Play();
if(GUILayout.Button(&暂停播放&))
//暂停播放
movTexture.Pause();
if(GUILayout.Button(&停止播放&))
//停止播放
movTexture.Stop();
}把代码随便拖给一个物体,然后把视频拖给movTexture,运行,点播放就可以了,如果还不成功可以准问我,两个方法都需要电脑安装quicktime播放器
quicktime我之前就安装好了,我看网上说把视频拉进project里会自动产生MovieTexture,但是我的都没有,所以一直都不懂怎么做,如果有可能的话,可不可以麻烦你加我扣远程做给我看看,一步一步的。我也是说如果可以的话可以的话,或是你做了截图给我也灰常感激[感恩][感恩]
采纳率:88%
来自团队:
为您推荐:
其他类似问题
unity3d的相关知识
&#xe675;换一换
回答问题,赢新手礼包&#xe6b9;
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。}

我要回帖

更多关于 网页上的视频不能播放 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信