查看: 971|回复: 1
打印 上一主题 下一主题

[其他] Unity3D技术之在资源包中加入脚本详解

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-1-31 19:16:38 |只看该作者 |倒序浏览
在资源包中加入脚本

脚本可作为文本资源 (TextAssets) 添加至资源包 (AssetBundles),但实际上,如此情况下的脚本将成为不可运行的代码。如需在资源包 (AssetBundles) 中添加可在应用程序中运行的代码,则需要将其预编译为程序集,并使用 Mono 映射 (Reflection) 类加载程序集(注意:映射不适用于 iOS)。

您可在任意普通的 C# 集成开发环境 (IDE)(如 Monodevelop,Visual Studio)中创建程序集,或使用 mono/.net 编译器创建任何文本编辑器。
  1. string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";
  2. IEnumerator Start () {
  3. // Start a download of the given URL
  4. WWW www = WWW.LoadFromCacheOrDownload (url, 1);

  5. // Wait for download to complete
  6. yield return www;

  7. // Load and retrieve the AssetBundle
  8. AssetBundle bundle = www.assetBundle;

  9. // Load the TextAsset object
  10. TextAsset txt = bundle.Load("myBinaryAsText", typeof(TextAsset)) as TextAsset;

  11. // Load the assembly and get a type (class) from it
  12. var assembly = System.Reflection.Assembly.Load(txt.bytes);
  13. var type = assembly.GetType("MyClassDerivedFromMonoBehaviour");

  14. // Instantiate a GameObject and add a component with the loaded class
  15. GameObject go = new GameObject();
  16. go.AddComponent(type);
  17. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

100

主题

3

听众

7683

积分

高级设计师

Rank: 6Rank: 6

纳金币
2378
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2015-1-31 20:50:47 |只看该作者
感谢分享。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-3 07:43 , Processed in 0.628886 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部