unity 从外部文件夹批量读取图片

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class PictureChange : MonoBehaviour
{
  
    private string[] dirs = new string[] { };// //获取文件夹下所有的图片路径  
    public static List<Texture2D> allTex2d = new List<Texture2D>(); // 储存获取到的图片  

    public GameObject tupian;//实例化图片
    public static int pictureNumber = 0;//文件夹中图片的个数
    public static int ballName = 0;//水球的名字
    public static bool isDrage = false;//是否开始拖拽ui
    private GameObject jiantou;

    private void OnEnable()
    {
        jiantou = GameObject.Find("向上箭头");

        if (allTex2d.Count == 0)
        {
            load();
        }

    }

    void Start()
    {
        pictureTrue();
    }


    /// <summary>
    /// 获取图片个数,并存储
    /// </summary>
    private void load()
    {
        List<string> filePaths = new List<string>();
        string imgtype = "*.BMP|*.JPG|*.GIF|*.PNG";
        string[] ImageType = imgtype.Split('|');
        int number = 0;//个数存储
 

        //int count = 0;
        //if (int.TryParse(IniReader.ReadOption("Movie", "Count", string.Empty), out count))
        //{
        //    for (int i = 1; i <= count; i++)
        //    {
        //        string str = "Movie" + i.ToString();
        //        string moviePath = IniReader.ReadOption("Movie", str, string.Empty);

        //    }
        //}


        for (int i = 0; i < ImageType.Length; i++)
        {
            //获取文件夹下所有的图片路径  
            dirs = Directory.GetFiles(@"C:\Users\Administrator\Desktop\图片文件", ImageType[i]);

            for (int j = 0; j < dirs.Length; j++)
            {
                filePaths.Add(dirs[j]);
            }
            number += dirs.Length;//不同类型的图片累计增加
        }


        for (int i = 0; i < filePaths.Count; i++)
        {
            Texture2D tx = new Texture2D(100, 100);
            tx.LoadImage(getImageByte(filePaths[i]));
            allTex2d.Add(tx);
        }


        pictureNumber = number;//传递图片个数
        //Debug.Log("图片的个数:" + pictureNumber);

    }

    /// <summary>  
    /// 根据图片路径返回图片的字节流byte[]  
    /// </summary>  
    /// <param name="imagePath">图片路径</param>  
    /// <returns>返回的字节流</returns> 
    private static byte[] getImageByte(string imagePath)
    {
        FileStream files = new FileStream(imagePath, FileMode.Open);
        byte[] imgByte = new byte[files.Length];
        files.Read(imgByte, 0, imgByte.Length);
        files.Close();
        return imgByte;

    }

    /// <summary>
    /// 把图片实例化出来
    /// </summary>
    private void pictureTrue()
    {

        if (allTex2d.Count != 0)
        {
            for (int i = 0; i < allTex2d.Count; i++)
            {
                //实例化图片
                GameObject lizi = Instantiate(tupian, transform);
                lizi.name = i.ToString();
                lizi.transform.localScale = new Vector3(0, 0, 0);
                //找到图片,更换图片
                Sprite sprit = Sprite.Create(allTex2d[i], new Rect(0, 0, allTex2d[i].width, allTex2d[i].height), Vector2.zero);
                lizi.GetComponent<Image>().sprite = sprit;

            }

        }

    }

    /// <summary>
    /// 图片出现
    /// </summary>
    public void pictureShow()
    {

        for (int i = 0; i < allTex2d.Count; i++)
        {
            transform.GetChild(i).localScale = new Vector3(0, 0, 0);
            transform.GetChild(i).localPosition = new Vector3(0, 0, 0);
        }
        

        //transform.GetChild(ballName).localScale = new Vector3(1, 1, 1);
        GameObject go = transform.GetChild(ballName).gameObject;
        iTween.ScaleTo(go, iTween.Hash("scale", new Vector3(1, 1, 1)));

    }

    /// <summary>
    /// 返回按钮
    /// </summary>
    public void fanhui()
    {
        for (int i = 0; i < allTex2d.Count; i++)
        {
            transform.GetChild(i).localScale = new Vector3(0, 0, 0);
        }
        isDrage = false;
        jiantou.GetComponent<Animator>().Play("New State");
    }

}

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值