Unity编程笔记----视频播放

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

[RequireComponent(typeof(VideoPlayer))]
public class AvPlayOnUGUI : MonoBehaviour
{

    private RenderTexture movie;
    private Image image;
    private RawImage rawimgage;
    private VideoPlayer player;
    public UIMode UI;
    public enum UIMode
    {
        None,
        Image,
        RawImage
    }

    // Use this for initialization
    void Start()
    {
        movie = new RenderTexture(512, 512, 24);
        player = GetComponent<VideoPlayer>();
        if (UI == UIMode.Image)
        {
            image = GetComponent<Image>();
            player.renderMode = VideoRenderMode.RenderTexture;
            player.targetTexture = movie;
        }
        else if (UI == UIMode.RawImage)
        {
            rawimgage = GetComponent<RawImage>();
            player.renderMode = VideoRenderMode.RenderTexture;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (UI == UIMode.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 (UI == UIMode.RawImage)
        {
            if (player.texture == null)
            {
                return;
            }
            rawimgage.texture = player.texture;
        }
    }
}

阅读更多
上一篇Unity编程笔记----事件模式
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭