我试图创建一个媒体播放器在团结,从一个静态文件夹读取所有媒体文件,并播放所有媒体(图像静态持续时间,视频的长度)。首先,我试图让它只处理图像。
我对团结很陌生,对C#不太在行。我能够将所有媒体文件源(图像)都转换到一个数组中,但接下来我需要将它们转换为纹理并放置在RawImage -component上。我被这个角色困住了。
如果我有src (前。C:\medias\img1.jpg)那么我如何才能将它作为图像放在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 () {
}
}谢谢:)
发布于 2017-03-06 19:50:04
首先,我试图让它只处理图像。 我对团结很陌生,对C#不太在行。我能够将所有媒体文件源(图像)都转换到一个数组中,但接下来我需要将它们转换为纹理并放置在RawImage -component上。我被这个角色困住了。
您正在寻找Texture2D.LoadImage函数。它将图像字节转换为Texture2D,然后您可以将该Texture2D分配给RawImage。
你必须问一个新的问题,如何用视频来做这件事。这要复杂得多。
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];https://stackoverflow.com/questions/42623578
复制相似问题