摘要:利用Http加载网络图片。

解决思路:

1、直接用unity 自带的www加载,在高版本www已经过时了。

2、本文直接使用万能的文件流加载。

(1)使用System.Net.HttpWebRequest 请求网络流。

(2)利用System.Drawing这个dll把网络流装载到内存。可以获取远程网络图片的基本信息,图片宽高,格式等。如果已经知道远程图片数据,可以先把网络流拷贝到内存,直接对流进行操作。

(3)Texture2D.LoadImage显示图片

废话不多说,直接上代码

获取网络图片二进制数据:

 1 public struct RemoteImageMessage
2 {
3 public int Width;
4
5 public int Height;
6
7 public byte[] DataSource;
8 }
9
10 public static class ImageDownloadHelper
11 {
12 /// <summary>
13 /// 获取远程图片
14 /// </summary>
15 /// <param name="imgPath"></param>
16 /// <param name="complete"></param>
17 public static void GetImage(string imgPath, Action<RemoteImageMessage> complete)
18 {
19 System.Threading.Tasks.Task.Run(() =>
20 {
21 RemoteImageMessage remoteImageMessage = new RemoteImageMessage();
22 try
23 {
24 System.Net.HttpWebRequest webRequest = System.Net.WebRequest.CreateHttp(imgPath);
25 using (var response = webRequest.GetResponse())
26 {
27 if (response.ContentLength != 0)
28 {
29 System.IO.Stream stream = response.GetResponseStream();
30 System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
31 remoteImageMessage.Width = image.Width;
32 remoteImageMessage.Height = image.Height;
33 var bytes = ImageToBytes(image);
34 remoteImageMessage.DataSource = bytes;
35 stream.Close();
36 stream.Dispose();
37 }
38 }
39 }
40 catch (Exception ex)
41 {
42 Debug.LogError("加载图片出错!" + ex);
43 }
44 finally
45 {
46 complete?.Invoke(remoteImageMessage);
47 }
48 });
49 }
50
51 /// <summary>
52 /// Image 转bytes
53 /// </summary>
54 /// <param name="image"></param>
55 /// <returns></returns>
56 private static byte[] ImageToBytes(System.Drawing.Image image)
57 {
58 System.Drawing.Imaging.ImageFormat format = image.RawFormat;
59 using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
60 {
61 if (format.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
62 {
63 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
64 }
65 else if (format.Equals(System.Drawing.Imaging.ImageFormat.Png))
66 {
67 image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
68 }
69 else if (format.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
70 {
71 image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
72 }
73 else if (format.Equals(System.Drawing.Imaging.ImageFormat.Gif))
74 {
75 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
76 }
77 else if (format.Equals(System.Drawing.Imaging.ImageFormat.Icon))
78 {
79 image.Save(ms, System.Drawing.Imaging.ImageFormat.Icon);
80 }
81 byte[] buffer = new byte[ms.Length];
82 //Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
83 ms.Seek(0, System.IO.SeekOrigin.Begin);
84 ms.Read(buffer, 0, buffer.Length);
85 return buffer;
86 }
87 }
88
89 }

在unity中显示

 1 public class ImageLoaderHeler : MonoBehaviour
2 {
3 private RawImage rawImage = null;
4
5 private string[] imageUrl = new string[]
6 { "http://t8.baidu.com/it/u=2247852322,986532796&fm=79&app=86&f=JPEG?w=1280&h=853",
7 "http://t9.baidu.com/it/u=3363001160,1163944807&fm=79&app=86&f=JPEG?w=1280&h=830",
8 "http://t9.baidu.com/it/u=583874135,70653437&fm=79&app=86&f=JPEG?w=3607&h=2408",
9 "http://t9.baidu.com/it/u=2268908537,2815455140&fm=79&app=86&f=JPEG?w=1280&h=719",
10 "http://t7.baidu.com/it/u=1179872664,290201490&fm=79&app=86&f=JPEG?w=1280&h=854",
11 "http://t9.baidu.com/it/u=3923875871,1613462878&fm=79&app=86&f=JPEG?w=1280&h=854",
12 "http://t9.baidu.com/it/u=3949188917,63856583&fm=79&app=86&f=JPEG?w=1280&h=875",
13 "http://t7.baidu.com/it/u=1355385882,1155324943&fm=79&app=86&f=JPEG?w=1280&h=854",
14 "http://t8.baidu.com/it/u=2857883419,1187496708&fm=79&app=86&f=JPEG?w=1280&h=763",
15 "http://t7.baidu.com/it/u=830740827,3648735644&fm=79&app=86&f=JPEG?w=1280&h=853",
16 "http://t8.baidu.com/it/u=198337120,441348595&fm=79&app=86&f=JPEG?w=1280&h=732",
17 "http://t9.baidu.com/it/u=1577456063,1344044640&fm=79&app=86&f=JPEG?w=1280&h=853",
18 "http://t8.baidu.com/it/u=2678662753,1297312162&fm=79&app=86&f=JPEG?w=1181&h=787",
19 "http://t8.baidu.com/it/u=2148738019,2920001333&fm=79&app=86&f=JPEG?w=1181&h=788",
20 "http://t9.baidu.com/it/u=1373840141,993565751&fm=79&app=86&f=JPEG?w=1181&h=788"
21 };
22
23 // Start is called before the first frame update
24 void Start()
25 {
26 rawImage = GetComponent<RawImage>();
27 //InvokeRepeating("Test", 3, 0.5f);
28 }
29
30
31 private int index = 0;
32
33 private void Test()
34 {
35 if (index >= imageUrl.Length)
36 {
37 index = 0;
38 }
39 LoadImage(imageUrl[index++]);
40 }
41
42 private void LoadImage(string imagePath)
43 {
44 var start = DateTime.Now;
45 Debug.LogError("图片数据开始加载!");
46 ImageDownloadHelper.GetImage(imagePath, (image) =>
47 {
48 ThreadCrossor.GetInstance()?.AddTaskToMainThread(() =>
49 {
50 var ori = rawImage.texture;
51 if (ori != null)
52 {
53 DestroyImmediate(ori);
54 rawImage.texture = null;
55 }
56 Debug.LogError($"图片尺寸{image.Width},{image.Height}");
57 Texture2D texture2D = new Texture2D(image.Width, image.Height);
58 texture2D.LoadImage(image.DataSource);
59 rawImage.texture = texture2D;
60 });
61 Debug.LogError($"完成图片加载!加载时间:{(DateTime.Now - start).TotalSeconds}s");
62 });
63 }
64
65
66 // Update is called once per frame
67 void Update()
68 {
69
70 }
71 }

写了一个简单的跨线程交互的类

public class ThreadCrossor : MonoBehaviour
{
private static ThreadCrossor threadCrossor = null; List<Action> actions = new List<Action>(); public static ThreadCrossor GetInstance()
{
return threadCrossor;
} /// <summary>
/// 往主进程添加任务
/// </summary>
/// <param name="action"></param>
public void AddTaskToMainThread(Action action)
{
lock (actions)
{
actions.Add(action);
}
} // Start is called before the first frame update
void Start()
{
threadCrossor = this;
} // Update is called once per frame
void Update()
{
lock (actions)
{
if (actions.Count > 0)
{
foreach(var action in actions)
{
action?.Invoke();
}
actions.Clear();
}
}
}
}

效果如下:

unity 加载网络图片的更多相关文章

  1. 【WPF】wpf image控件加载网络图片不显示问题,

    1.加载网络图片到内存system.drawing.image对象中2.内存中的image 转Bitmap 再转适合system.windows.controls.image 的BitmapImage ...

  2. 有关DTCoreText无法加载网络图片及应用问题

    至于DTCoreText是干嘛的,不清楚的同学自行网上脑补,这就不啰嗦了,只说一下其用法. 里面有三种控件供大家使用,DTAttributedTextView, DTAttributedLabel 和 ...

  3. [转]全面理解Unity加载和内存管理

    [转]全面理解Unity加载和内存管理 最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Resources.Load,一是通过AssetBundle,其实两者本质 ...

  4. [原创]cocos2dx加载网络图片&异步加载图片

    [动机] 之前看到一款卡牌游戏,当你要看全屏高清卡牌的时候,游戏会单独从网络上下载,本地只存了非高清的,这样可以省点包大小,所以我萌生了实现一个读取网络图片的类. [联想] 之前浏览网页的时候经常看到 ...

  5. SDWebImage 加载网络图片失败,重新运行,就能加载成功。

    现象: 使用SDWebImage 加载网络图片,偶尔会有一两张图片就是显示不出来.重新运行有时又可以了. 这个问题的原因是: 当SDWebImage 在加载图片的时候 我用的是- (void)sd_s ...

  6. Android Volley入门到精通:使用Volley加载网络图片

    在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...

  7. Android三种基本的加载网络图片方式(转)

    Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 ...

  8. 仿微信朋友圈图片查看-glide加载网络图片,photoview 实现缩放

    http://www.cnblogs.com/csonezp/p/5083286.html 这里实现的效果就和微信朋友圈点击图片后查看大图一样,如果你不清楚是什么效果,可以拿出手机,打开朋友圈,找到一 ...

  9. android官方开源的高性能异步加载网络图片的Gridview例子

    这个是我在安卓安卓巴士上看到的资料,放到这儿共享下.这个例子android官方提供的,其中讲解了如何异步加载网络图片,以及在gridview中高效率的显示图片此代码很好的解决了加载大量图片时,报OOM ...

随机推荐

  1. JavaWeb开发获取客户IP地址

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11737637.html 本地调试如果使用的是localhost进行访问, 则会获取到 0:0: ...

  2. checkstyle使用介绍

    1.我下载的是checkstyle-5.5-bin.zip:下载地址 http://sourceforge.net/projects/checkstyle/files/ 另一个是checkstyle的 ...

  3. The official raywenderlich.com Objective-C style guide.

    The official raywenderlich.com Objective-C style guide. This style guide outlines the coding convent ...

  4. k8s之yaml文件详解

    k8s之yaml文件详解 目录 k8s之yaml文件详解 1. k8s支持的文件格式 2. YAML语言格式 3. 查看api资源版本标签 4. 编写nginx-test.yaml资源配置清单 4.1 ...

  5. 总结haproxy各调度算法的实现方式及其应用场景

    一.静态算法 1.1 static-rr 基于权重的轮询调度,不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)及后端服务器慢启动,其后端主机数量没有限制,相当于LVS中的 w ...

  6. Node介绍

    https://segmentfault.com/a/1190000006121183 一. 概述 Node.js是基于Chrome JavaScript运行时建立的一个平台,实际上它是对Google ...

  7. 8、Linux基础--rpm、yum、yum私有仓库、系统优化

    笔记 1.晨考 1.文件的三种时间属性,每一种时间属性在什么情况下改变 atime : 访问时间 mtime :修改时间 ctime :修改属性时间 2.权限的类型 可读(r, 4) 可写(w, 2) ...

  8. systemverilog中奇怪的语法

    1.->运算符 expression_a->expression_b其实等效于(!expression_a || expression_b),systemverilog中利用 || 运算的 ...

  9. Python基础—函数(Day9)

    一.函数的定义 def 关键字,定义一个函数 my_len 函数名(书写规则与变量名一样) def与函数名中间一个空格. def与函数名中间一个空格. 函数名():加冒号 函数体 my_len()#函 ...

  10. 带分数--第四届蓝桥杯省赛C++B/C组

    第四届蓝桥杯省赛C++B/C组----带分数 思路: 1.先枚举全排列 2.枚举位数 3.判断是否满足要求 这道题也就是n=a+b/c,求出符合要求的abc的方案数.进行优化时,可以对等式进行改写,改 ...