Unity3D:用VideoPlayer在UGUI上播放视频

Unity5.6版本添加了一个新的组件VideoPlayer,这个组件是用来播放视频,这个解决了之前用movietexture播放视频,只能在PC端使用的问题,因为刚出来,可能还有一些问题,今天我要说的是怎样将视频显示在UGUI上,我用了两种方法,一种是在RawImage显示,一种是在Image上显示,这两个有什么区别,一个就是直接放Texture就可以,一个是要把Texture转成Sprite,刚开始的思路是将Texture强转为Texture2D,但是Unity运行的时候会报错,后来想到了用RenderTexture来获取Texture2D,在转成Sprite。好了,下面就是代码。

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

[RequireComponent(typeof(VideoPlayer))]
public class UIVideoImage : MonoBehaviour
{
    private RenderTexture movie;
    private Image image;
    private RawImage rawImage;
    private VideoPlayer player;
    public UIMode UGUI;

    [Serializable]    
    public class VideoPlayerFinishedEvent : UnityEvent { }        
    // Event delegates finished.    
    [FormerlySerializedAs("onVideoPlayerFinished")]    
    [SerializeField]    
    private VideoPlayerFinishedEvent m_onVideoPlayerFinished = new VideoPlayerFinishedEvent();    
    public VideoPlayerFinishedEvent videoPlayerFinished    
    {        
        get { return m_onVideoPlayerFinished; }        
        set { m_onVideoPlayerFinished = value; }    
    }

    public enum UIMode
    {
        None = 0,
        Image = 1,
        RawImage = 2
    }
    // Use this for initialization
    void Start()
    {
        movie = new RenderTexture(Screen.width, Screen.height, 24);
        player = GetComponent<VideoPlayer>();
        player.sendFrameReadyEvents = true;
        if (UGUI == UIMode.Image)
        {
            image = GetComponent<Image>();
            player.renderMode = VideoRenderMode.RenderTexture;
            player.targetTexture = movie;
        }
        else if (UGUI == UIMode.RawImage)
        {
            rawImage = GetComponent<RawImage>();
            player.renderMode = VideoRenderMode.APIOnly;
        }
        videoPlayer.loopPointReached += OnVideoLoopOrPlayFinished;
    }
    // Update is called once per frame
    void Update()
    {
        if (UGUI == UIMode.Image)
        {
            //在Image上播放视频
            if (player.targetTexture == null) return;
            int width = player.targetTexture.width;
            int height = player.targetTexture.height;
            Texture2D t = new Texture2D(width, height, TextureFormat.ARGB32, false);
            RenderTexture.active = player.targetTexture;
            t.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            t.Apply();
            image.sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f)) as Sprite;
        }
        if (UGUI == UIMode.RawImage)
        {
            //在RawImage上播放视频
            if (player.texture == null) return;
            rawImage.texture = player.texture;
        }
    }
    // 播放完成或者循环完成事件
    public void OnVideoLoopOrPlayFinished(VideoPlayer vp)    
    {        
        if (videoPlayerFinished != null)        
        {            
            videoPlayerFinished.Invoke();        
        }    
    }
}

代码就这些,其实也没多复杂。只要将上面的代码拖到UGUI的Image或者RawImage上选择相应的模式就能进行视频播放了。

目前来说这个组件还是很好用的,但是在某些情况下,会有莫名其妙的问题。等遇到了再说吧。

You May Also Like

About the Author: 大腿Plus

9 Comments

发表评论