[Serializable]
public class ImageList //文件夹读取所有照片后存放的名字的类
{
[SerializeField]
public string firstname; //第一层
[SerializeField]
public string secondname;//第二层
[SerializeField]
public string thirdname;//第三层
[SerializeField]
public Texture2D texture2D;//第四层texture2d
//构造方法
public ImageList(string firstName, string secondName = null, string thirdName = null, Texture2D Texture2D = null)
{
firstname = firstName;
secondname = secondName;
thirdname = thirdName;
texture2D = Texture2D;
}
}
[Serializable]
public class FirstContactSecond //第一层与第二层联系得到类
{
[SerializeField]
public string name; //第一层名字
[SerializeField]
public List
public FirstContactSecond(string a, List
{
this.name = a;
this.SecondList = stt;
}
}
[Serializable]
public class SecondToThird //第二层与第三层联系的类
{
[SerializeField]
public string name; //第二层名字
[SerializeField]
public List
//[SerializeField]
//public Sprite[] Sp;
public SecondToThird(string n, List
{
this.name = n;
this.T2d = t2d;
}
}
此文代码为宅小丰原创,转载,复制请注明!!!
public class ReadAllImage : MonoBehaviour
{
private string StartUrl; //照片存放路径
public List
//三层关联起来的List
public List
//存放第一层所有名字的临时集合
public List
void Awake()
{
StartUrl = Directory.GetCurrentDirectory() + @"\Assets\StreamingAssets\家具\家具图片";
}
void Start()
{
RecursiveReadImage(StartUrl);
//SortImageList();
FinallySortImageList();
}
///
/// 把ImageLists中第一层名字加入TempList1中
///
private void AddTempList1()
{
for (int i = 0; i < ImageLists.Count; i++)
{
if (!tempList1.Contains(ImageLists[i].firstname))
{
tempList1.Add(ImageLists[i].firstname);
}
}
}
此文代码为宅小丰原创,转载,复制请注明!!!
///
/// 把ImageList数据整合成一个FirstContactSecond集合
///
private void FinallySortImageList()
{
AddTempList1();
List
for (int i = 0; i < tempList1.Count; i++)
{
List
string n1 = tempList1[i];
for (int j = 0; j < ImageLists.Count; j++)
{
if (tempList1[i] == ImageLists[j].firstname && ImageLists[j].secondname != null)
{
List
if (!tempString.Contains(ImageLists[j].secondname))
{
tempString.Add(ImageLists[j].secondname);
if (ImageLists[j].texture2D != null)
{
t.Add(ImageLists[j].texture2D);
}
SecondToThird stt = new SecondToThird(ImageLists[j].secondname, t);
STT.Add(stt);
}
else
{
foreach (var item in STT)
{
if (item.name == ImageLists[j].secondname)
{
item.T2d.Add(ImageLists[j].texture2D);
}
}
}
}
}
FirstContactSecond fcs = new FirstContactSecond(n1, STT);
ContactLists.Add(fcs);
}
}
此文代码为宅小丰原创,转载,复制请注明!!!
///
/// 递归读取图片文件,并存储好名字
///
///
private void RecursiveReadImage(string url)
{
DirectoryInfo root = new DirectoryInfo(url);
if (root.GetDirectories().Length > 0)
{
for (int i = 0; i < root.GetDirectories().Length; i++)
{
//Debug.Log(root.GetDirectories()[i].FullName);
RecursiveReadImage(root.GetDirectories()[i].FullName);
}
}
else
{
//Debug.Log("开始找图片");
if (root.GetFiles().Length > 0)
{
//Debug.Log("有文件了");
for (int i = 0; i < root.GetFiles().Length; i++)
{
string fileUrl = root.GetFiles()[i].FullName;
//Debug.Log("fileUrl----"+fileUrl);
string fileName = root.GetFiles()[i].Name;
// Debug.Log("fileName----"+fileName);
if (fileName.ToUpper().EndsWith(".JPG") || fileName.ToUpper().EndsWith(".PNG"))
{
string temppath = fileUrl.Substring(fileUrl.IndexOf("片") + 2);
//Debug.Log("temppath---"+ temppath);
string[] DirectoryNames = temppath.Split(new char[1] { '\\' });
Texture2D tx2d = LoadImageByIO(fileUrl);
tx2d.name = fileName;
ImageList imageList = new ImageList(DirectoryNames[0], DirectoryNames[1], DirectoryNames[2], tx2d);
ImageLists.Add(imageList);
}
else
{
Debug.Log("不是jpg或者png格式");
}
}
}
else
{
//Debug.Log("该文件夹没有图片,记录该文件夹的路径");
string DirectoryUrl = root.FullName.Substring(root.FullName.IndexOf("片") + 2);
//Debug.Log("DirectoryUrl----"+DirectoryUrl);
string[] Directory = DirectoryUrl.Split(new char[1] { '\\' });
ImageList imageList;
switch (Directory.Length)
{
case 1:
// Debug.Log("只有一层文件");
imageList = new ImageList(Directory[0]);
break;
case 2:
// Debug.Log("有两层文件");
imageList = new ImageList(Directory[0], Directory[1]);
break;
//case 3:
// Debug.Log("有三层文件");
// imageList = new ImageList(Directory[0], Directory[1], Directory[2]);
// break;
default:
//Debug.Log("出错了");
imageList = new ImageList("error");
break;
}
ImageLists.Add(imageList);
}
}
}
此文代码为宅小丰原创,转载,复制请注明!!!
//读取照片并转成Texture2D
///
///
///
///
///
private Texture2D LoadImageByIO(string path)
{
//创建文件流
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 = 372;
Texture2D texture2D = new Texture2D(width, height);
texture2D.LoadImage(bytes);
//加入到texture数组中
//Texture2Ds.Add(texture2D);
return texture2D;
//Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height),
// new Vector2(0.5f, 0.5f));
}
}