软件编程
位置:首页>> 软件编程>> C#编程>> Unity 从Resources中动态加载Sprite图片的操作

Unity 从Resources中动态加载Sprite图片的操作

作者:爱尚游Bin  发布时间:2023-08-26 11:37:18 

标签:Unity,Resources,Sprite

我就废话不多说了,大家还是直接看代码吧~


public  Sprite LoadSourceSprite(string relativePath)
   {
       //Debug.Log("relativePath=" + relativePath);
       //把资源加载到内存中
       Object Preb = Resources.Load(relativePath, typeof(Sprite));
       Sprite tmpsprite = null;
       try
       {
           tmpsprite = Instantiate(Preb) as Sprite;
       }
       catch ( System.Exception ex )
       {
       }
       //用加载得到的资源对象,实例化游戏对象,实现游戏物体的动态加载
       return tmpsprite;
       //return Resources.Load(relativePath, typeof(Sprite)) as Sprite;
   }

补充:Unity运行时动态加载本地图片

一、Unity运行时加载本地文件夹下所有图片的方法

用于在使用图片前加载完成


//引入命名空间
using System;
using System.IO;
   /// <summary>
   /// 加载图片的Byte[]数组
   /// </summary>
   /// <param name="filesName">地址</param>
   public List<byte[]> LoadImage(string filesName)
   {
       List<byte[]> list = new List<byte[]>();
       string tempPath ="E:\"+filesName;  // 图片所在文件夹地址
       List<string> filePaths = new List<string>();
       string imgtype = "*.BMP|*.JPG|*.PNG";
       string[] ImageType = imgtype.Split('|');
       for (int i = 0; i < ImageType.Length; i++)
       {
           //文件夹下所有的图片路径  
           string[] dirs = Directory.GetFiles(tempPath, ImageType[i]);
           for (int j = 0; j < dirs.Length; j++)
           {
               filePaths.Add(dirs[j]);
           }
       }
       for (int i = 0; i < filePaths.Count; i++)
       {
           byte[] bs = getImageByte(filePaths[i]);
           list.Add(bs);
       }
       return list;
   }
   #endregion
   /// <summary>  
   /// 根据图片路径返回图片的字节流byte[]  
   /// </summary>  
   /// <param name="imagePath">图片路径</param>  
   /// <returns>返回的字节流</returns>  
   private byte[] getImageByte(string imagePath)
   {
       FileStream files = new FileStream(imagePath, FileMode.Open,FileAccess.Read);
       files.Seek(0,SeekOrigin.Begin);
       byte[] imgByte = new byte[files.Length];
       files.BeginRead(imgByte,0,(int)files.Length,CallBack,files);
       return imgByte;
   }
   /// <summary>
   /// 异步加载
   /// </summary>
   /// <param name="ar"></param>
   void CallBack(IAsyncResult ar)
   {
       FileStream fs = ar.AsyncState as FileStream;
       fs.Close();
       fs.Dispose();
   }

用的时候:


List<byte[]> data=new  List<byte[]>();  //临时接收图片数据流
List<Texture2D> turList=new List<Texture2D>();  //保存图片
data=加载类.LoadImage("测试图片");
foreach (byte[] item in data)
{
  Texture2D tex = new Texture2D(100, 100, TextureFormat.RGBA32, false);  
  tex.LoadImage(item); //建议哪里调用哪里转  还可转精灵
  turList.Add(tex);  
}

二、临时加载一张图片


public static class ImageLoad {
        public static Texture2D LoadImageByte(string path){
           FileStream files=new FileStream (PathSet.dataPath+path,FileMode.Open,FileAccess.Read);
           files.Seek(0,SeekOrigin.Begin);
           byte[] imgByte=new byte[files.Length];
           //少量临时加载会 红问号
           //files.BeginRead(imgByte,0,(int)files.Length,CallBack,files);
           files.Read(imgByte,0,imgByte.Length);
           files.Close();
           Texture2D tx=new Texture2D (512,512);
           tx.LoadImage(imgByte);
           return tx;
       }
      static void CallBack(IAsyncResult ar){
          FileStream fileStream=ar.AsyncState as FileStream;
           fileStream.Close();
           fileStream.Dispose();
      }
   }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

来源:https://blog.csdn.net/wks310/article/details/86061768

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com