Unity从文件夹加载媒体并在RawImage上显示 [英] Unity load medias from folder and display on RawImage

查看:270
本文介绍了Unity从文件夹加载媒体并在RawImage上显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Unity中创建一个媒体播放器,从静态文件夹中读取所有媒体文件并播放所有媒体(图像静态持续时间,视频长度为视频)。首先,我试图让它只与图像一起使用。

I am trying to create a media player in Unity that reads all media files from a static folder and plays trough all medias (images static duration, videos for the length of the video). First I am trying to get it to work with just images.

我对Unity很新,对C#不太好。我能够将所有媒体文件源(图像)都放到一个数组中,但接下来我需要将它们转换为纹理并放在RawImage组件上。我坚持使用这部分。

I'm very new with Unity and not good with C#. I'm able to get all media file sources (images) to an array but next I need to convert them to a texture and place on the RawImage -component. I'm stuck with this part.

如果我有src(例如C:\ medias \ img1.jpg)那么我怎么能把它作为一个RawImage -component上的图像?

If I have the src (ex. C:\medias\img1.jpg) then how could I place this as a image on the RawImage -component?

我的代码 - >

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System;
using System.IO;
using System.Linq;

public class Player : MonoBehaviour {

    // Use this for initialization
    void Start () {
        DirectoryInfo dir = new DirectoryInfo(@"C:\medias");
        string[] extensions = new[] { ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG", ".ogg", ".OGG" };
        FileInfo[] info = dir.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();
        Debug.Log (info[0]);
        // Logs C:\medias\img1.jpg
    }

    // Update is called once per frame
    void Update () {

    }
}

谢谢:)

推荐答案


首先,我试图让它只使用图像。

First I am trying to get it to work with just images.

我是Unity的新手,对C#不太好。我能够将所有
媒体文件源(图像)添加到数组中,但接下来我需要将
转换为纹理并放置在RawImage组件上。这部分我坚持使用

I'm very new with Unity and not good with C#. I'm able to get all media file sources (images) to an array but next I need to convert them to a texture and place on the RawImage -component. I'm stuck with this part.

您正在寻找 Texture2D.LoadImage 功能。它将图像字节转换为Texture2D,然后您可以将Texture2D分配给RawImage。

You are looking for the Texture2D.LoadImage function. It converts image bytes to Texture2D then you can assign that Texture2D to the RawImage.

您必须询问有关如何使用视频执行此操作的新问题。这要复杂得多。

You have to ask new question about how to do this with Videos. That's much more complicated.

public RawImage rawImage;
Texture2D[] textures = null;

//Search for files
DirectoryInfo dir = new DirectoryInfo(@"C:\medias");
string[] extensions = new[] { ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG", ".ogg", ".OGG" };
FileInfo[] info = dir.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();

//Init Array
textures = new Texture2D[info.Length];


for (int i = 0; i < info.Length; i++)
{
    MemoryStream dest = new MemoryStream();

    //Read from each Image File
    using (Stream source = info[i].OpenRead())
    {
        byte[] buffer = new byte[2048];
        int bytesRead;
        while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
        {
            dest.Write(buffer, 0, bytesRead);
        }
    }

    byte[] imageBytes = dest.ToArray();

    //Create new Texture2D
    Texture2D tempTexture = new Texture2D(2, 2);

    //Load the Image Byte to Texture2D
    tempTexture.LoadImage(imageBytes);

    //Put the Texture2D to the Array
    textures[i] = tempTexture;
}

//Load to Rawmage?
rawImage.texture = textures[0];

这篇关于Unity从文件夹加载媒体并在RawImage上显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆