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

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

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

如果我有src(例如C:\ medias \ img1.jpg)那么我如何将它作为图像放在RawImage组件上?

我的代码 – >

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 () { } } 

谢谢 :)

首先,我试图让它与图像一起工作。

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

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

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

 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];