学习记录:Unity图片和文字的外部导入

一:文字的外部导入

1代码书写

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 文字内容的外部加载
/// </summary>
public class TextManager : MonoBehaviour
{
    public string titlePath; //标题文件的名称
    public string filePath;  //内容文件的名称

    [SerializeField] Text titleTxtUGUI;                 //内容文字标题 
    [SerializeField] Text contentTxt;                   //内容展示

    private void Start()
    {
        SetContentTxt();
    }
    private void SetContentTxt()
    {
        //标题或文件名称为空时 不执行
        if (titlePath.Equals("") && filePath.Equals(""))
        {
            Debug.Log("标题内容的string字段为空");
            return;
        }
        //标题的文字替换
        titleTxtUGUI.text = File.ReadAllText(Application.streamingAssetsPath + $"/ContentTitle/{filePath}.txt", Encoding.UTF8);
        //内容的文字替换
        contentTxt.text = File.ReadAllText(Application.streamingAssetsPath + $"/ContentTxt/{filePath}.txt", Encoding.UTF8);

    }
}

2 Hierarchy面板

 在Streamimg文件夹中,新建两个文件夹,然后再文件夹中写txt文档,注意txt文档的名字跟代码中输入的名字是一样的,不然就会找不到这段文字.

二:图片的外部导入

1代码展示

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 图片外部加载
/// </summary>
public class ImageManage : MonoBehaviour
{
    List<Sprite> imageList = new List<Sprite>();   //加载的图片的列表
    [SerializeField] List<GameObject> images = new List<GameObject>();  //图片列表
    [SerializeField] List<Text> texts = new List<Text>();           //图片名称列表


    void Start()
    {
        LoadAllPic();
        for (int i = 0; i < imageList.Count; i++)
        {
            //将图片赋值到预制体上
            images[i].GetComponent<Image>().sprite = imageList[i];
        }

    }
    string[] dirs;
    //外部加载图片
    void LoadAllPic()
    {
        string imgtype = "*.JPG|*.PNG";
        string[] ImageType = imgtype.Split('|');
        List<string> filePathList = new List<string>();
        string[] dirs1 = Directory.GetFiles(Application.streamingAssetsPath + "/image/", ImageType[0]);
        List<string> dirs1List = new List<string>(dirs1);
        string[] dirs2 = Directory.GetFiles(Application.streamingAssetsPath + "/image/", ImageType[1]);
        //当两种格式都存在时,将第二个数组中的内容加入到第一个数组中去
        if (ImageType[0] != null && ImageType[1] != null)
        {
            for (int m = 0; m < dirs2.Length; m++)
            {
                string dir2 = dirs2[m];
                dirs1List.Add(dir2);
            }
            dirs = dirs1List.ToArray();
        }
        //当只有jpg格式时,将数组内容赋值
        else if (ImageType[0] != null && ImageType[1] == null)
            dirs = dirs1;
        //当只有png格式时,将数组内容赋值
        else if (ImageType[0] == null && ImageType[1] != null)
            dirs = dirs2;

        for (int j = 0; j < dirs.Length; j++)
        {
            Texture2D tx = new Texture2D(100, 100);
            tx.LoadImage(getImageByte(dirs[j]));
            Sprite sprite = Sprite.Create(tx, new Rect(0, 0, tx.width, tx.height), new Vector2(-500, -500));

            //获取sprite的名称
            sprite.name = Path.GetFileNameWithoutExtension(dirs[j]);
            //将名称用“-”分割
            string[] names = sprite.name.Split('-');
            //获取数字后面的名字并赋值
            texts[j].text = names[1];
            //将获取的图片加到图片列表中
            imageList.Add(sprite);
        }

    }

    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;
}
}

添加了图片按名称排序,不加排序的话,图片的读取顺序为1,10,11,2,3...添加排序之后,可以设置正序和倒叙读取图片。

2 Hierarchy面板

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值