原文地址:http://www.unitymanual.com/thread-33321-1-1.html?_dsign=b3aeb908
游戏开发中需要从服务器上加载一下资源包,场景等,这时就需要使用unity的WWW加载类。最近研究WWW加载发现很多问题和报错,这里接写出来和大家共享一下。
关于加载:首先是检查本地文件里是否存在相同的资源包文件(检查和校验版本号),如果都是正确的就不需要从服务器端下载了直接从本地(手机就是SD卡里了)加载就可以了。
如果版本号不对那么就要下载最新版本的资源了,当然要把老版本的从本地删除,不然在手机里会很占储存空间的。
新建一个Resource类,作为加载工具类继承与MonoBehaviour。
新建方法,传入必要信息提供加载:
System.IO包里提供了一个File类,其Exists方法就是查询指定路径下是否有指定的文件存在。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
public static
string
suffix
=
“.unity
3
d”;
/
/
资源包后缀
public static Dictionary
<
string
,
GameObject
>
cache
=
new
Dictionary
<
string
,
GameObject
>
(
)
;
public void load
(
string
path
,
string
name
,
int
version
)
{
if
(
!File.Exists
(
Application.persistentDataPath
+
name
+
suffix
+
version
)
)
{
StartCoroutine
(
loader
(
path
,
name
,
version
)
)
;
/
/
网络加载
}
else
{
StartCoroutine
(
loadBundleFromLocal
(
path
,
name
,
version
)
)
;
/
/
本地加载
}
}
|
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
|
private IEnumerator loadBundle
(
string
path
,
string
name
,
int
version
)
{
print
(
“fuck you”
)
;
int oldVersion
=
version
-
1
;
/
/
写死了
string
pathName
=
Application.persistentDataPath
+
path
+
name
;
string
url
=
path
+
name
+
suffix
;
using
(
WWW www
=
new
WWW
(
url
)
)
{
yield
return
www;
if
(
www.
error
=
=
null
)
{
if
(
File.Exists
(
pathName
+
oldVersion
)
)
{
File.Delete
(
pathName
+
oldVersion
)
;
Debug.Log
(
“
delete
old
version
data
succeed...”
)
;
}
if
(
!File.Exists
(
pathName
+
version
)
)
{
/
/
write
file
in
local
File.WriteAllBytes
(
pathName
+
version
,
www.bytes
)
;
/
/
other function
/
/
FileStream fs
=
new
FileStream
(
Application.persistentDataPath
+
path
+
name
+
version
,
FileMode.OpenOrCreate
)
;
/
/
fs.Write
(
www.bytes
,
0
,
www.bytes.Length
)
;
/
/
fs.Flush
(
)
;
/
/
fs.Close
(
)
;
Debug.Log
(
“load
bundle
succeed
,
write
file
path
:
”
+
pathName
+
version
)
;
}
else
{
Debug.Log
(
“
write
file
error
...”
)
;
}
AssetBundle ab
=
www.assetBundle;
GameObject go
=
ab.mainAsset
as
GameObject;
cache[
name
]
=
go;
Resource.go
=
go;
/
/
ab.Unload
(
false
)
;
}
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
private IEnumerator loadBundleFromLocal
(
string
path
,
string
name
,
int
version
)
{
using
(
WWW www
=
new
WWW
(
“
file
:
/
/
/
”
+
Application.persistentDataPath
+
path
+
name
+
version
)
)
{
yield
return
www;
if
(
www.
error
=
=
null
)
{
AssetBundle ab
=
www.assetBundle;
GameObject go
=
ab.mainAsset
as
GameObject;
cache[
name
]
=
go;
Resource.go
=
go;
Debug.Log
(
“load
data
frome
local
succeed...”
)
;
}
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
using
UnityEngine;
using
System.Collections;
public
class
LoadUIResponse
:
MonoBehaviour
{
public Resource rs;
void Start
(
)
{
if
(
rs
=
=
null
)
{
rs
=
new
Resource
(
)
;
}
int
version
=
1
;
rs.load
(
“http
:
/
/
192.1
68.1
.
122
/
resource
/
”
,
“monster
1
”
,
version
)
;
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
private Resource rs;
string
path
=
“ int
version
=
1
;
string
name
=
”monster
1
“;
void start
(
)
{
rs
=
gameObject.GetComponent
(
typeof
(
Resource
)
)
as
Resource;
Resource.instance
=
rs;
}
void OnMouseDown
(
)
{
Resource.instance.load
(
path
,
name
,
version
)
;
}
|
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
|
private static Resource _instance;
/
/
添加
get
和
set
public static Resource instance
{
set
{
_instance
=
value
;
}
get
{
if
(
_instance
=
=
null
)
{
/
/
_instance
=
new
Resource
(
)
;
Debug.Log
(
”
no
instance.“
)
;
}
return
_instance;
}
}
|