转载自: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." ) ;
         }
     }
}

参考文章:

http://blog.csdn.net/janeky/article/details/25923151

http://docs.unity3d.com/Manual/scriptsinassetbundles.html

[Unity3D学习]Unity代码热更新 源码下载相关推荐

  1. SELinux零知识学习四、Audit应用层源码下载、编译和安装

    接前一篇文章:SELinux零知识学习三.SELinux应用层源码下载.编译和安装 在前篇文章中编译SELinux源码时,出现了如下错误: seusers_local.c:11:10: 致命错误:li ...

  2. Edxposed学习研究(四)Magisk(面具)源码下载编译详细实战教程

    说明:本文操作在Windows 平台实现. 一.Magisk源码下载 在终端执行如下git命令下载Magisk源码. E:\workspace\androidstudio\EdXposedProj\2 ...

  3. 华为鸿蒙系统学习笔记4-方舟编译器源码下载及安装

    2019华为全球开发者大会将在8月9日-11日在华为松山湖基地召开.本次开发者大会邀请了1500位合作伙伴.5000名全球开发者,将是华为历来规模最大的一次会议.在这次大会上,华为方舟编译器也是关注的 ...

  4. 方舟编译器编写鸿蒙软件,华为鸿蒙系统学习笔记4-方舟编译器源码下载及安装...

    2019华为全球开发者大会将在8月9日-11日在华为松山湖基地召开.本次开发者大会邀请了1500位合作伙伴.5000名全球开发者,将是华为历来规模最大的一次会议.在这次大会上,华为方舟编译器也是关注的 ...

  5. php getid3,PHP getID3类的使用方法学习笔记【附getID3源码下载】

    本文实例讲述了PHP getID3类的使用方法.分享给大家供大家参考,具体如下: 网上描述: getID3()这个PHP脚本能够从MP3或其它媒体文件中提取有用的信息如:ID3标签,bitrate,播 ...

  6. html div初始隐藏点击可见_3种CSS3移动手机隐藏菜单UI界面代码解析/附源码下载...

    这是一款效果非常酷的jQuery和CSS3移动手机隐藏菜单UI界面设计.这个UI设计共有三种不同的打开隐藏菜单的效果,分别为滑动显示,Material Design风格效果和展开式效果. 使用方法 H ...

  7. 三国杀c语言游戏编程代码,三国杀游戏 - 源码下载|源代码 - 源码中国

    压缩包 : 三国杀.rar 列表 [原创]一步一步用C#编写三国杀(二):牌堆的设计 - 忘却之都 - 博客园.files\1787546.jpg [原创]一步一步用C#编写三国杀(二):牌堆的设计 ...

  8. iOS代码质量要求_Unity移动端代码热更新技术学习总结

    为什么需要热更新 游戏总是伴随着不断的开发与维护,我们不能要求玩家每次都将游戏客户端卸载重装,所以需要热更新技术来在不需要重装客户端的情况下下载更新游戏里的代码(其实资源也需要热更新,但是因为操作系统 ...

  9. 基于Unity3D经典消消乐游戏源码,代码详细注释,c#版方块消消乐源代码

    使用Unity2017开发,实现功能有: 基本消除功能 UI动画以及代码控制动画 消除动画以及手势识别 消除判定.连续消除判定 UI是自己独立完成比较简单,打包平台我设置的是webGL平台,有需要改成 ...

最新文章

  1. JQuery 扩展多语言支持
  2. [ARM异常]-ARM Core中与中断相关的寄存器
  3. 全球及中国磁性分离滑轮行业竞争战略及未来产销需求预测报告2022版
  4. js加版本号 webpack_webpack脚手架增加版本号
  5. Shell(8)——for、while、until
  6. 前端学习(1488):jsonp回顾
  7. java单车吧_暑假一个月,撸个共享单车后台
  8. 【Day08】请简述虚拟 DOM 中 Key 的作用和好处
  9. On intelligence by Jeff Hawkins
  10. java开发app后端_app后端用什么开发
  11. 微软资深副总裁洪小文:人工智能十年预测(演讲全文、61PPT)
  12. docker MySQL 双主_DockerMysql数据库实现双主同步配置详细·TesterHome
  13. 风影版OllyDBG 1.10 2016-12-09修改版
  14. 转:C语言面试题大汇总 (图像处理方向)
  15. Python中字典创建、遍历、添加
  16. 解决模糊查询问题 element UI 从服务器搜索数据,输入关键字进行查找
  17. WebStorm+Vue-cli 配置alias 点击跳转无效问题
  18. vscode快速生成代码块
  19. 如何用晶体管进行计算
  20. 【方法】关于递推式的公式求法

热门文章

  1. 2018 年终总结 - 随笔
  2. 不要和太磨叽的人交易,他们太过于优柔寡断,
  3. 回梦游仙-仙剑奇侠传4主题曲:再一次感动仙剑
  4. Java向下转型的意义详解
  5. js闭包理解与基本实现
  6. SEO中影响网站排名的49个因素(完整版)
  7. python 手动实现gelu,sigmod
  8. 2023济南眼健康展,山东近视矫正展,CEYEE中国眼博会
  9. Linear Algebra(MIT)
  10. 有什么制作证件照的方法?这个小妙招了解一下