在Unity3D中加载外部图片的两种方法

来源:互联网 发布:数据接口开发 编辑:程序博客网 时间:2024/04/30 14:10

转载自:

喜欢我的博客请记住我的名字:秦元培,我的博客地址是:http://qinyuanpei.com 
转载请注明出处,本文作者:秦元培, 本文出处:http://blog.csdn.net/qinyuanpei/article/details/48262583

  各位朋友大家好,我是秦元培,欢迎大家关注我的博客。最近在做项目的过程中遇到这样的一个需求:玩家可以在游戏过程中进行实时存档,在存档过程中会保存当前游戏进度,同时会截取当前游戏画面并加载到游戏存档界面中。当下一次进入游戏的时候,将读取本地存档图片并加载到游戏界面中。这在单机游戏中是特别常见的一种功能,这里主要有两个关键点。首先是截取游戏画面,这个问题大家可以在《Unity3D游戏开发之截屏保存精彩瞬间》这篇文章中找到答案。其次是从本地加载图片,因为这里要保证可读可写,因此传统的Resources.Load()方式和AssetBundle方式均无法实现这样的功能。那么怎样从外部加载图片到游戏中,这就是我们今天要讨论的内容啦。好了,这里介绍两种方法来实现这一目的。

喜闻乐见的WWW方式

  喜闻乐见的WWW方式之所以喜闻乐见,这是因为这是我们最为熟悉的一种,我们都知道通过WWW可以从网络上加载文本、图片、音频等形式的内容,那么通过WWW能否加载本地外部(相对于应用程序)资源呢?答案是肯定的,这是因为WWW可以支持http和file两种协议。我们通常接触到的WWW默认都是指http协议,现在我们来说说file协议,该协议可以用来访问本地资源(绝对路径)。例如我们希望加载文件D:\TestFile\pic001.png这个文件,则此时对应的C#脚本为:

<code class="hljs 1c has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//请求WWW</span>WWW www = new WWW(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"file://D:\\TestFile\\pic001.png);</span>yield return www;        if(www != null && string.IsNullOrEmpty(www.error)){    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取Texture</span>    Texture texture=www.texture;       <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//更多操作...       </span>}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

注意到这里出现了yield return结构,这表示这里使用到了协程,因此我们需要付出的代价就是需要在项目中使用StartCoroutine等协程相关的方法来调用这些协程。虽然在Unity3D中使用协程是件简单的事情,可是如果我们随随便便地使用协程而不注意去维护这些协程,那么这些让我们引以为傲的简单代码可能就会变成我们痛苦不堪的无尽深渊。

亘古不变的传统IO方式

  好了,下面我们隆重推出亘古不变的传统IO方式,这种方式相信大家都没有接触过,所以这里将这种方法和大家分享。既然是传统的IO方式,那么无非就是各种IO流的处理啦。好,我们一起来看下面这段代码:

<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建文件读取流</span>FileStream fileStream = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> FileStream(screen, FileMode.Open, FileAccess.Read);fileStream.Seek(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, SeekOrigin.Begin);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建文件长度缓冲区</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] bytes = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[fileStream.Length]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//读取文件</span>fileStream.Read(bytes, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>)fileStream.Length);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//释放文件读取流</span>fileStream.Close();fileStream.Dispose();fileStream = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建Texture</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> width=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">800</span>;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> height=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">640</span>;Texture2D texture = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Texture2D(width, height);texture.LoadImage(bytes);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>

可以看到在使用这种方式读取图片文件的时候主要是将图片文件转化为byte[]数组,再利用Texture2D的LoadImage方法转化为Unity3D中的Texture2D。这种方法需要在创建过程中传入图片的大小,在这里我们创建了一张800X640的图片。经过博主的研究发现,这种方式加载外部图片相对于使用WWW加载外部图片效率更高,所以如果大家遇到类似的需求,博主个人推荐大家使用这种方式进行加载。

注:原文用的是UGUI,我用的是NGUI,代码也有所不同。此处给我原文代码和本人的代码

原文代码

using UnityEngine;using System.Collections;using UnityEngine.UI;using System.IO;public class TestLoading : MonoBehaviour {    /// <summary>    /// Image控件    /// </summary>    private Image image;    void Start ()     {        image = this.transform.Find("Image").GetComponent<Image>();        //为不同的按钮绑定不同的事件        this.transform.Find("LoadByWWW").GetComponent<Button>().onClick.AddListener        (           delegate(){LoadByWWW();}        );        this.transform.Find("LoadByIO").GetComponent<Button>().onClick.AddListener        (          delegate(){LoadByIO();}        );    }    /// <summary>    /// 以IO方式进行加载    /// </summary>    private void LoadByIO()    {        double startTime = (double)Time.time;        //创建文件读取流        FileStream fileStream = new FileStream("D:\\test.jpg", FileMode.Open, FileAccess.Read);        fileStream.Seek(0, SeekOrigin.Begin);        //创建文件长度缓冲区        byte[] bytes = new byte[fileStream.Length];        //读取文件        fileStream.Read(bytes, 0, (int)fileStream.Length);        //释放文件读取流        fileStream.Close();        fileStream.Dispose();        fileStream = null;        //创建Texture        int width = 300;        int height = 372;        Texture2D texture = new Texture2D(width, height);        texture.LoadImage(bytes);        //创建Sprite        Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));        image.sprite = sprite;        startTime=(double)Time.time-startTime;        Debug.Log("IO加载用时:" + startTime);    }    /// <summary>    /// 以WWW方式进行加载    /// </summary>    private void LoadByWWW()    {        StartCoroutine(Load());    }    IEnumerator Load()    {        double startTime = (double)Time.time;        //请求WWW        WWW www = new WWW("file://D:\\test.jpg");        yield return www;                if(www != null && string.IsNullOrEmpty(www.error))        {            //获取Texture            Texture2D texture=www.texture;            //创建Sprite            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));            image.sprite = sprite;            startTime = (double)Time.time - startTime;            Debug.Log("WWW加载用时:" + startTime);        }    }}

本人代码:(Mac环境下)

using UnityEngine;using UnityEngine.UI;using System.IO;using System.Collections;public class Test : MonoBehaviour {public UISprite image;public UITexture tx1;public UITexture tx2;// Use this for initializationvoid Start () {//为不同的按钮绑定不同的事件EventDelegate.Add( this.transform.Find ("LoadByWWW").GetComponent<UIButton>().onClick, LoadByWWW);EventDelegate.Add( this.transform.Find ("LoadByIO").GetComponent<UIButton>().onClick, LoadByIO);}/// <summary>/// 以IO方式进行加载/// </summary>private void LoadByIO(){double startTime = (double)Time.time;//创建文件读取流FileStream fileStream = new FileStream("/Users/apple/Documents/Photo/IMG_2152.JPG", FileMode.Open, FileAccess.Read);fileStream.Seek(0, SeekOrigin.Begin);//创建文件长度缓冲区byte[] bytes = new byte[fileStream.Length];//读取文件fileStream.Read(bytes, 0, (int)fileStream.Length);//释放文件读取流fileStream.Close();fileStream.Dispose();fileStream = null;//创建Textureint width = 300;int height = 372;Texture2D texture = new Texture2D(width, height);texture.LoadImage(bytes);tx1.mainTexture = texture;startTime=(double)Time.time-startTime;Debug.Log("IO加载用时:" + startTime);}/// <summary>/// 以WWW方式进行加载/// </summary>private void LoadByWWW(){StartCoroutine(Load());}IEnumerator Load(){double startTime = (double)Time.time;//请求WWWWWW www = new WWW("file:/Users/apple/Documents/Photo/IMG_6425.JPG");yield return www;        if(www != null && string.IsNullOrEmpty(www.error)){//获取TextureTexture2D texture=www.texture;tx2.mainTexture = texture;startTime = (double)Time.time - startTime;Debug.Log("WWW加载用时:" + startTime);}}}

现在我们运行程序可以发现两种方式均可以让图片加载进来,为了对比两种方式在执行效率上的高低,我们在脚本中加入了相关代码,通过对比可以发现使用IO方式加载一张227k的图片需要的时间为0s,而使用WWW方式加载需要0.0185s,因此传统的IO方式具有更高的效率,建议大家在遇到这类问题时尽可能地使用这种方式。好了,今天的内容就是这样啦,欢迎大家在我的博客中留言、欢迎大家关注和支持我的博客,谢谢大家!

0 0
原创粉丝点击