[Unity3D学习]Unity代码热更新 源码下载
2015.05.01转载地址:http://blog.gamerisker.com/archives/608.html之前的一篇文章《[Unity3D学习]Unity代码热更新解决方案测试结果总结》只是说了一下方案的流程,今天刚好有时间,又再看了一下热更新这一块!就直接将代码分享出来。代码热更新的核心基本实现,只是需要处理一些依赖等等。
资源都是放在我blog的服务器上,提供给大家测试。
时间有点晚了1:30,直接贴代码睡觉了!(最近一个月拼死的加班,实在太累。)
demo : http://pan.baidu.com/s/1c0cuTZa
Index.cs
1
2
3
4
5
6
7
8
9
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
|
using
UnityEngine
;
using
System
.
Collections
;
/// <summary>
/// 游戏壳的入口,用于加载真实的代码
/// </summary>
public
class
Index
:
MonoBehaviour
{
private
WWW
www
;
public
static
WWW
uiWWW
;
private
System
.
Reflection
.
Assembly
assembly
;
// Use this for initialization
void
Start
(
)
{
StartCoroutine
(
loadSprite
(
)
)
;
//GameObject go = new GameObject();
//go.AddComponent<Game>();
}
void
Update
(
)
{
}
private
IEnumerator
loadSprite
(
)
{
www
=
new
WWW
(
"http://game.gamerisker.com/assets/Core.assetbundle"
)
;
yield
return
www
;
if
(
www
.
isDone
)
{
AssetBundle
bundle
=
www
.
assetBundle
;
TextAsset
asset
=
bundle
.
Load
(
"Core"
,
typeof
(
TextAsset
)
)
as
TextAsset
;
assembly
=
System
.
Reflection
.
Assembly
.
Load
(
asset
.
bytes
)
;
System
.
Type
script
=
assembly
.
GetType
(
"Game"
)
;
Debug
.
Log
(
"Load Game Script Complete..."
)
;
gameObject
.
AddComponent
(
script
)
;
}
}
}
|
Game.cs
1
2
3
4
5
6
7
8
9
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
|
using
UnityEngine
;
using
System
.
Collections
;
/// <summary>
/// 这个类是真正的游戏入口类,这个类中存放业务逻辑 代码热更新的就是这边的代码!~
/// </summary>
public
class
Game
:
MonoBehaviour
{
private
WWW
www
;
private
WWW
fontWWW
;
// Use this for initialization
void
Start
(
)
{
Debug
.
Log
(
"Start Load UIAtlas!"
)
;
StartCoroutine
(
LoadAtlas
(
)
)
;
StartCoroutine
(
LoadFont
(
)
)
;
}
void
Update
(
)
{
if
(
www
!=
null
&&
!
www
.
isDone
)
{
Debug
.
Log
(
"atlas progress : "
+
www
.
progress
)
;
}
if
(
fontWWW
!=
null
&&
!
fontWWW
.
isDone
)
{
Debug
.
Log
(
"font progress : "
+
fontWWW
.
progress
)
;
}
}
private
IEnumerator
LoadFont
(
)
{
fontWWW
=
new
WWW
(
"http://game.gamerisker.com/assets/Font.assetbundle"
)
;
yield
return
fontWWW
;
Complete
(
)
;
}
private
IEnumerator
LoadAtlas
(
)
{
www
=
new
WWW
(
"http://game.gamerisker.com/assets/Atlas.assetbundle"
)
;
yield
return
www
;
Complete
(
)
;
}
private
void
Complete
(
)
{
if
(
fontWWW
.
isDone
&&
www
.
isDone
)
{
GameObject
atlas
=
www
.
assetBundle
.
Load
(
"Default"
,
typeof
(
GameObject
)
)
as
GameObject
;
UISprite
sprite
=
NGUITools
.
AddChild
<
UISprite
>
(
gameObject
)
;
UIAtlas
uiatlas
=
atlas
.
GetComponent
<
UIAtlas
>
(
)
;
sprite
.
atlas
=
uiatlas
;
sprite
.
spriteName
=
"default_tishikuang"
;
sprite
.
width
=
300
;
sprite
.
height
=
300
;
//这里的字体 是NGUI 例子里面的字体,我直接将他的prefab 打成了assetbundle 使用
GameObject
font
=
fontWWW
.
assetBundle
.
Load
(
"SciFi Font - Normal"
,
typeof
(
GameObject
)
)
as
GameObject
;
UIFont
uifont
=
font
.
GetComponent
<
UIFont
>
(
)
;
UILabel
label
=
NGUITools
.
AddChild
<
UILabel
>
(
sprite
.
gameObject
)
;
label
.
text
=
"Load Script Succeed!"
;
label
.
transform
.
localPosition
=
Vector3
.
zero
;
label
.
transform
.
localScale
=
Vector3
.
one
;
label
.
width
=
300
;
label
.
bitmapFont
=
uifont
;
Debug
.
Log
(
"Complete."
)
;
}
}
}
|
参考文章: