赞
踩
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
namespace LAIALAIA
{
[Serializable]
public class TextureContents
{
public string FloderName;
public List<Sprite> Sprites;
}
/// <summary>
/// 图片资源的加载和使用,待完善,针对需要加载外部图集的项目
/// </summary>
public class TextureLoad : MonoBehaviour
{
/// <summary>
/// 把sprite集合里的图片全部放在新生成的ImagePrefab上,并指定父物体为Coutent
/// </summary>
/// <param Sprite集合="PhotoSpriteList0"></param>
/// <param 放图片的预制体="ImagePrefab"></param>
/// <param 父物体="Coutent"></param>
public static void UseSprite(List<Sprite> PhotoSpriteList0,GameObject ImagePrefab, GameObject Coutent)
{
foreach (var item in PhotoSpriteList0)
{
GameObject a = Instantiate(ImagePrefab, Coutent.transform);
a.GetComponent<Image>().sprite = item;
}
}
/// <summary>
/// 获取当前文件夹的一级子文件夹,和一级子文件夹内的所有图片文件(指定格式)
/// </summary>
/// <param name="folderPath">文件夹路径</param>
/// <param name="FloderVideoList">子文件夹下的文件集合</param>
/// <returns></returns>
public static DirectoryInfo[] GetChildDirectorieAndFiles(string folderPath, List<TextureContents> FloderVideoList)
{
Debug.Log("路径:" + folderPath);
DirectoryInfo dir = new DirectoryInfo(folderPath);
foreach (var item in dir.GetDirectories())
{
Debug.Log(item.Name);
TextureContents A = new TextureContents();
//A.Sprites = videoAddress(folderPath + "/" + item.Name + "/");
A.Sprites = LoadPicture(folderPath + "/" + item.Name + "/");
A.FloderName = item.Name;
FloderVideoList.Add(A);
}
return dir.GetDirectories();
}
/// <summary>
/// //获取文件夹下所有jpg/png图片路径,并把图片转换为sprite,存进指定的集合
/// </summary>
/// <param 文件夹路径="path"></param>
/// <param 用于存放图片的集合="PhotoList"></param>
public static List<Sprite> LoadPicture(string path)
{
List<Sprite> PhotoList = new List<Sprite>();
//folderName = path;
DirectoryInfo info = new DirectoryInfo(path);
FileInfo[] files = info.GetFiles();
if (files.Length>0)
{
foreach (var item in files)
{
print(item.Name);
string suffixName = item.Name.Substring(item.Name.Length - 3, 3); //获取文件后缀名,用以判断是否为图片
suffixName = suffixName.ToLower();
if (suffixName == "jpg" || suffixName == "png")
{
//photoNames.Add(item.Name);
LoadImageByIO(path + @"\" + item.Name, PhotoList);
}
}
}
else
{
print("文件夹为空:"+ path);
}
return PhotoList;
}
/// <summary>
/// 使用文件流加载文件
/// </summary>
/// <param name="path"></param>
/// <param name="PhotoList"></param>
private static void LoadImageByIO(string path, List<Sprite> PhotoList)
{
//创建文件流
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
//创建文件长度的缓冲区
byte[] bytes = new byte[fileStream.Length];
//读取文件
fileStream.Read(bytes, 0, (int)fileStream.Length);
//释放文件读取流
fileStream.Close();
fileStream.Dispose();
fileStream = null;
//创建Texture
int width = 300;
int height = 200;
Texture2D texture2D = new Texture2D(width, height);
texture2D.LoadImage(bytes);
Sprite tempSprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(10, 10));
PhotoList.Add(tempSprite);
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。