Unity3D 常用贴图加载方式

Unity3D加载贴图几种常用的方式

第一种 通过WWW异步加载

通过WWW 加载完成后 ,用www.texture获得贴图,然后 通过Sprite.Create()创建Sprite,赋给相应的Image显示。

  private IEnumerator LoadImageWithWWW(string imageName,Action<int,Sprite> callBack)
   {
      //在editor中或者iOS中,需要添加"file://" ; Android 不需要
      #if UNITY_IOS || UNITY_EDITOR
      var url = "file://"+Application.streamingAssetsPath + "/" + imageName;
      #elif UNITY_ANDROID
      var url = "file://"+Application.streamingAssetsPath + "/" + imageName;
       #endif
      
      WWW www = new WWW(url);
      
      yield return www;

      while (!www.isDone)
      {
         yield return 0;
      }
   
      if (string.IsNullOrEmpty(www.error))
      {
         var texture = www.texture;
         if (texture!=null)
         {
            Sprite sprite = Sprite.Create(texture,new Rect(0,0,texture.width, texture.height),new Vector2(0.5f, 0.5f));
            callBack(0, sprite);
         }
         else
         {
            callBack(-1, null);
         }
      }
      else
      {
         callBack(-1, null);
         Debug.LogError(www.error);
      }
      www.Dispose();
   }
第二种 通过UnityWebRequest 异步加载

Unity提供了专门针对加载Texture的UnityWebRequest封装,UnityWebRequestTexture,可以直接使用它来加载贴图。然后通过 DownloadHandlerTexture.GetContent()来获得贴图。

private IEnumerator LoadImageWithWebRequest(string imageName,Action<int,Sprite> callBack)
   {
      //在editor中或者iOS中,需要添加"file://" ; Android 不需要
      #if UNITY_IOS || UNITY_EDITOR
      var url = "file://"+Application.streamingAssetsPath + "/" + imageName;
      #elif UNITY_ANDROID
      var url = "file://"+Application.streamingAssetsPath + "/" + imageName;
       #endif
      
      UnityWebRequest unityWebRequest = UnityWebRequestTexture.GetTexture(url);
      
      yield return unityWebRequest.SendWebRequest();

      if (string.IsNullOrEmpty(unityWebRequest.error))
      {
         var texture = DownloadHandlerTexture.GetContent(unityWebRequest);
         if (texture!=null)
         {
            Sprite sprite = Sprite.Create(texture,new Rect(0,0,texture.width, texture.height),new Vector2(0.5f, 0.5f));
            callBack(0, sprite);
         }
         else
         {
            callBack(-1, null);
         }
      }
      else
      {
         callBack(-1, null);
         Debug.LogError(unityWebRequest.error);
      }
      unityWebRequest.Dispose();
   }
第三种 同步加载

获得文件的byte数组,然后通过Texture2D 的LoadImage函数,讲内容填充到 texture中。texture的宽和高会自动按bytes的内容来相应变化。

  private void LoadTexture(string path,Action<int,Texture> callback)
   {
      Texture2D texture2D = new Texture2D(2,2);
      if (File.Exists(path))
      {
         var bytes = File.ReadAllBytes(Application.streamingAssetsPath+"/1.png");
         texture2D.LoadImage(bytes);
         texture2D.anisoLevel = 8;
         callback(0, texture2D);
      }
      else
      {
         callback(-1, null);
      }
   }
使用代码
 public Image ImagePrefab; //需要再Inspector上赋值

   void Start ()
   {
      string[] imageNames = {"1.png","2.png","3.png","4.png","5.png"};

      foreach (var imageName in imageNames)
      {
         StartCoroutine(LoadImageWithWebRequest(imageName,OnLoadComplete));
      }
   }
   
   /// <summary>
   /// 加载完成的回调
   /// </summary>
   /// <param name="result"></param>
   /// <param name="sprite"></param>
   private void OnLoadComplete(int result,Sprite sprite)
   {
      if (result == 0)
      {
         Image img = GameObject.Instantiate(ImagePrefab);
         img.transform.SetParent(this.transform);
         img.sprite = sprite;
      }
   }

注意Unity的常用路径访问方式:

  • 当使用www或者unitywebrequest 方式异步加载时,在编辑器和ios平台,如果使用的Application.streamingAssetsPath,需要再前面加上“file://”,而ios平台则是不需要的。
  • iOS平台Application.streamingAssetsPath可以使用File.ReadAllBytes 同步加载,但是android 平台不可以。只能使用异步方式,通过www或者unitywebrequest加载。
  • 所有平台的 Application.PersistentPath 可以使用File.ReadAllBytes同步加载。

更多Unity教程请关注微信公众号UnityAsk。
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值