您当前的位置: 首页 >  unity

程序员正茂

暂无认证

  • 1浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity不用协程加载本地PNG图片

程序员正茂 发布时间:2021-02-22 18:38:33 ,浏览量:1

 Unity加载本地图片一般用WWW或UnityWebRequest ,这两种都是异步模式,但有时需要使用同步模式,以下代码为同步模式,可自动更新Texture2D尺寸。

public static Texture2D LoadPNG(string filePath) {
 
     Texture2D tex = null;
     byte[] fileData;
 
     if (File.Exists(filePath))     {
         fileData = File.ReadAllBytes(filePath);
         tex = new Texture2D(2, 2);
         tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
     }
     return tex;
 }

 

关注
打赏
1660743125
查看更多评论
立即登录/注册

微信扫码登录

0.0374s