当前位置:   article > 正文

unity批量加载本地文件(图片)_unity 加载本地图片

unity 加载本地图片
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);
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/112582?site
推荐阅读
相关标签
  

闽ICP备14008679号