今天用UGUI做多个图片自动滚动展示的功能,具体如下
1.从特定文件夹中读取所有图片
2.排列展示所有图片,如果超出屏幕范围了,就循环滚动
3.点击其中的图片,暂停滚动,该图片放大展示在屏幕中间。再点击就缩小回去,并继续滚动。
首先,要遍历文件夹,读取所有的图片
// 在特定路径中获取所有图片
Dictionary<string, Texture2D> GetAllImgInPath( string fullPath)
{
Dictionary<string, Texture2D> retDict = new Dictionary<string, Texture2D>();
// 先判断是不是存在该路径
if( Directory.Exists( fullPath) == false)
{
return retDict;
}
// 遍历路径中所有的文件
string [] filePathArr = Directory.GetFiles( fullPath);
string ext = string.Empty;
string []imgExtArr = new string[]{".jpg", ".png", ".bmp"};
foreach( string fullName in filePathArr)
{
// 根据后缀判断是不是图片
ext = Path.GetExtension( fullName);
if( ((IList)imgExtArr).Contains(ext))
{
// 读取图像
byte[] imgBytes = File.ReadAllBytes( fullName);
Texture2D t2d = new Texture2D(1,1);
t2d.LoadImage( imgBytes);
retDict.Add( Path.GetFileName( fullName), t2d);
}
}
return