首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从文件夹中加载媒体并在RawImage上显示

从文件夹中加载媒体并在RawImage上显示
EN

Stack Overflow用户
提问于 2017-03-06 10:46:51
回答 1查看 2.5K关注 0票数 2

我试图创建一个媒体播放器在团结,从一个静态文件夹读取所有媒体文件,并播放所有媒体(图像静态持续时间,视频的长度)。首先,我试图让它只处理图像。

我对团结很陌生,对C#不太在行。我能够将所有媒体文件源(图像)都转换到一个数组中,但接下来我需要将它们转换为纹理并放置在RawImage -component上。我被这个角色困住了。

如果我有src (前。C:\medias\img1.jpg)那么我如何才能将它作为图像放在RawImage -component上呢?

我的代码->

代码语言:javascript
复制
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 () {

    }
}

谢谢:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-06 19:50:04

首先,我试图让它只处理图像。 我对团结很陌生,对C#不太在行。我能够将所有媒体文件源(图像)都转换到一个数组中,但接下来我需要将它们转换为纹理并放置在RawImage -component上。我被这个角色困住了。

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

你必须问一个新的问题,如何用视频来做这件事。这要复杂得多。

代码语言:javascript
复制
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];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42623578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档