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

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

喜闻乐见的WWW方式  喜闻乐见的WWW方式之所以喜闻乐见,这是因为这是我们最为熟悉的一种,我们都知道通过WWW可以从网络上加载文本、图片、音频等形式的内容,那么通过WWW能否加载本地外部(相对于应用程序)资源呢?答案是肯定的,这是因为WWW可以支持http和file两种协议。我们通常接触到的WWW默认都是指http协议,现在我们来说说file协议,该协议可以用来访问本地资源( 绝对路径)。例如我们希望加载文件D:\TestFile\pic001.png这个文件,则此时对应的C#脚本为:
[AppleScript] 纯文本查看 复制代码
?
1
2
3
4
5
6
7
8
9
/ / 请求WWW
WWW www = new WWW ( " file : / / D : \\TestFile\\pic 001. png ) ;
yield return www;       
if ( www ! = null & & string .IsNullOrEmpty ( www. error ) )
{
     / / 获取Texture
     Texture texture = www.texture;  
     / / 更多操作...      
}

注意到这里出现了yield return结构,这表示这里使用到了协程,因此我们需要付出的代价就是需要在项目中使用StartCoroutine等协程相关的方法来调用这些协程。虽然在 Unity3d中使用协程是件简单的事情,可是如果我们随随便便地使用协程而不注意去维护这些协程,那么这些让我们引以为傲的简单代码可能就会变成我们痛苦不堪的无尽深渊。
亘古不变的传统IO方式  好了,下面我们隆重推出亘古不变的传统IO方式,这种方式相信大家都没有接触过,所以这里将这种方法和大家分享。既然是传统的IO方式,那么无非就是各种IO流的处理啦。好,我们一起来看下面这段代码:
[AppleScript] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
/ / 创建文件读取流
FileStream fileStream = new FileStream ( screen , 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 = 800 ;
int height = 640 ;
Texture 2 D texture = new Texture 2 D ( width , height ) ;
texture.LoadImage ( bytes ) ;

可以看到在使用这种方式读取图片文件的时候主要是将图片文件转化为byte[]数组,再利用Texture2D的LoadImage方法转化为 Unity3D中的Texture2D。这种方法需要在创建过程中传入图片的大小,在这里我们创建了一张800X640的图片。经过博主的研究发现,这种方式加载外部图片相对于使用WWW加载外部图片效率更高,所以如果大家遇到类似的需求,博主个人推荐大家使用这种方式进行加载。
  到目前为止我们解决了如何从外部加载图片到Unity3D中,现在我们回到最开始的问题,我们从外部读取到这些图片以后需要将它们加载到游戏界面中。比如当我们使用UGUI的时候,UGUI中的Image控件需要一个 Sprite来作为它的填充内容,那么此时我们就需要将Texture转化为Sprite.号了,下面我们给出一个简单的例子:
[AppleScript] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 ;
         Texture 2 D texture = new Texture 2 D ( width , height ) ;
         texture.LoadImage ( bytes ) ;
 
         / / 创建Sprite
         Sprite sprite = Sprite.Create ( texture , new Rect ( 0 , 0 , texture.width , texture.height ) , new Vector 2 ( 0.5 f , 0.5 f ) ) ;
         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
             Texture 2 D texture = www.texture;
 
             / / 创建Sprite
             Sprite sprite = Sprite.Create ( texture , new Rect ( 0 , 0 , texture.width , texture.height ) , new Vector 2 ( 0.5 f , 0.5 f ) ) ;
             image .sprite = sprite;
 
             startTime = ( double ) Time.time - startTime;
             Debug.Log ( "WWW加载用时:" + startTime ) ;
         }
     }
}

现在我们运行程序可以发现两种方式均可以让图片加载进来,为了对比两种方式在执行效率上的高低,我们在脚本中加入了相关代码,通过对比可以发现使用IO方式加载一张227k的图片需要的时间为0s,而使用WWW方式加载需要0.0185s,因此传统的IO方式具有更高的效率,建议大家在遇到这类问题时尽可能地使用这种方式。好了,今天的内容就是这样啦。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值