查看: 1414|回复: 2
打印 上一主题 下一主题

[其他] unity3d动态加载脚本

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

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

跳转到指定楼层
楼主
发表于 2014-12-30 00:37:35 |只看该作者 |倒序浏览
本文记录如何通过unity3d进行脚本资源打包加载

  1、创建TestDll.cs文件
  1. public class TestDll : MonoBehaviour {

  2. void Start () {

  3. print(“Hi U_tansuo!”);

  4. }

  5. }
复制代码
2、生成dll文件

  (1)使用vs打包

  (2) 使用mono打包

  (3) 命令行打包 mac下(亲测):  /Applications/Unity/Unity.app/Contents/Frameworks/Mono/bin/gmcs -r:/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEngine.dll -target:library 脚本路径

  win下(未试过):mcs -r: /unity安装根目录\Unity\Editor\Data\Managed/UnityEngine.dll -target:library 脚本路径

  3、更改文件后缀

  至关重要一步  更改上一步生成的TestDLL.dll 为 TestDLL.bytes  否则 打包加载会错

  4、使用 BuildPipeline.BuildAssetBundle进行打包 资源为 TestDll.unity3d

  5、加载
  1. IEnumerator Test()

  2. {

  3. string url=“file://”+Application.dataPath+“/TestDll.unity3d”;

  4. print(url);

  5. WWW www = WWW.LoadFromCacheOrDownload (url, 1);

  6. // Wait for download to complete

  7. yield return www;

  8. // Load and retrieve the AssetBundle

  9. AssetBundle bundle = www.assetBundle;

  10. //TestDll 是资源的名字

  11. TextAsset txt = bundle.Load(“TestDll”, typeof(TextAsset)) as TextAsset;

  12. print(txt.bytes.Length);

  13. // Load the assembly and get a type (class) from it

  14. var assembly = System.Reflection.Assembly.Load(txt.bytes);

  15. var type = assembly.GetType(“TestDll”);

  16. // Instantiate a GameObject and add a component with the loaded class

  17. gameObject.AddComponent(type);

  18. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

heise    

4

主题

3

听众

4249

积分

中级设计师

Rank: 5Rank: 5

纳金币
105
精华
0

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

沙发
发表于 2014-12-30 02:16:54 |只看该作者
感谢分享!!!!!!!
回复

使用道具 举报

0

主题

1

听众

786

积分

初级设计师

Rank: 3Rank: 3

纳金币
169
精华
0

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

板凳
发表于 2014-12-30 09:08:54 |只看该作者
大尉不愧是纳金网论坛的功臣之一
回复

使用道具 举报

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

关闭

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

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

GMT+8, 2024-5-14 23:32 , Processed in 0.093865 second(s), 32 queries .

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

© 2008-2019 Narkii Inc.

回顶部