3d物体播放视频
-
创建一个cube,在cube下创建canvas,然后调到世界,并调整位置和大小,
-
cube上添加audio source组件和video player组件
-
在创建两个image,image下面创建text文本,分别为暂停和播放,然后再image上添加button组件
-
创建脚本,写代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
public class AudioPlayer : MonoBehaviour {
VideoPlayer videoPlayer;
Slider slider;
// Use this for initialization
void Start () {
videoPlayer = GetComponent<VideoPlayer>();
slider = transform.GetChild(0).GetChild(0).GetComponent<Slider>();
slider.maxValue=(float)videoPlayer.clip.length;//视频长度
}
public void Play()
{
videoPlayer.Play();
}
public void Pause()
{
videoPlayer.Pause();
}
public void ChangeFloat(float t)
{
if (SliderPointer.isClick)
{
videoPlayer.time = slider.value;
}
}
// Update is called once per frame
void Update () {
if (!SliderPointer.isClick)
{
slider.value = (float)videoPlayer.time;
}
}
}
- 再创建一个脚本挂在slider上,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class SliderPointer : MonoBehaviour,IPointerDownHandler,IPointerUpHandler {
public static bool isClick = false;
public void OnPointerDown(PointerEventData eventData)
{
isClick = true;
}
public void OnPointerUp(PointerEventData eventData)
{
isClick = false;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
- 写好后,把全景视频拖拽,button事件拖拽还有slider上面的拖拽
- 效果
3d物体播放视频
UI播放视频
- 创建rawimage,添加audio source组件和video player组件,然后把视频拖进去
rawimage和image的区别
image贴图类型只能是精灵图片
rawimage什么贴图都可以
- 要想承接播放一个视频,就要在assets创建一个Render Texture,然后拖拽到rawimage属性面板里的Texture里.
- 把刚创建的Render Texture也要拖拽到video player组件属性面板里的相应位置
运行后就看到视频了,但是会发现视频并没有铺满image,就要去写脚本 - 把脚本挂在canvas上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class RawImageOaly : MonoBehaviour {
public RawImage rawImage;
private VideoPlayer videoPlayer;
// Use this for initialization
void Start () {
videoPlayer = rawImage.GetComponent<VideoPlayer>();
rawImage.texture = videoPlayer.texture;
}
public void Play()
{
videoPlayer.Play();
}
public void Pause()
{
videoPlayer.Pause();
}
// Update is called once per frame
void Update () {
rawImage.texture = videoPlayer.texture;
}
}
5.写好代码后回到unity拖拽后运行,就正常播放了,然后添加两个按钮,控制视频的暂停和播放,和上面的一样就不再细述了