[Unity基础]unity调用dll文件以及反射加载dll

来源:互联网 发布:phpcms建站 编辑:程序博客网 时间:2024/05/18 01:04

参考链接:

http://liweizhaolili.blog.163.com/blog/static/1623074420144313825921/

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

http://m.blog.csdn.net/blog/ordinary0214/20935321


一、调用dll文件中的c#类

a.新建项目,选择类库,选择.NET Framework 2.0

b.

using System;using System.Collections.Generic;using System.Text;public class CSharpDll{    public static int Add(int a, int b)    {        return a + b;    }}

c.菜单栏,生成 / 生成解决方案,生成的dll文件会出现在bin\Debug下,然后把它复制到Assets目录下

d.

using UnityEngine;using System.Collections;public class TestDll : MonoBehaviour {void Start () {        print(CSharpDll.Add(1, 2));}}

二、调用dll文件中的MonoBehaviour类

a.新建项目,选择类库,选择.NET Framework 2.0

b.菜单栏,项目,添加引用,unity安装目录\Editor\Data\Managed,找到UnityEngine.dll直接添加(即项目/添加引用,脚本中使用的命名空间来自哪些dll,就引用哪些dll)

using UnityEngine;using System.Collections;public class MonoDll : MonoBehaviour {    void Start()    {        print("Start");    }    void Update()    {        print("Update");    }    public void PrintInfo()    {        print("Hello");    }}

c.菜单栏,生成 / 生成解决方案,生成的dll文件会出现在bin\Debug下,然后把它复制到Assets目录下

d.

using UnityEngine;using System.Collections;public class TestMonoDll : MonoBehaviour {// Use this for initializationvoid Start ()     {        MonoDll m = gameObject.AddComponent<MonoDll>();        m.PrintInfo();}}


反射就是得到程序集中的属性和方法

a.将导入的.dll文件改后缀为.bytes

b.打包上面的文件,可以参考:http://blog.csdn.net/lyh916/article/details/46289407

c.

using UnityEngine;using System.Collections;using System.Reflection;using System;public class TestDll : MonoBehaviour {    private static readonly string path = "file://" + Application.dataPath + "/StreamingAssets/" + "ALL.assetbundle";    void Start()    {        StartCoroutine(loadDllScript());    }    IEnumerator loadDllScript()    {        WWW www = WWW.LoadFromCacheOrDownload(path, 10);        yield return www;        AssetBundle bundle = www.assetBundle;        TextAsset asset = bundle.Load("ClassLibrary1", typeof(TextAsset)) as TextAsset;        Assembly assembly = Assembly.Load(asset.bytes);        Type t = assembly.GetType("CSharpDll");        MethodInfo method = t.GetMethod("Add");        System.Object[] p = new System.Object[] { 1, 2 };        print(method.Invoke(t,p));        TextAsset asset2 = bundle.Load("ClassLibrary2", typeof(TextAsset)) as TextAsset;        Assembly assembly2 = Assembly.Load(asset2.bytes);        Type t2 = assembly2.GetType("MonoDll");        gameObject.AddComponent(t2);    } }


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

dll反编译:

使用工具:Reflector

下载地址:http://pan.baidu.com/s/1kTJDyyb


dll调用:

在vs菜单栏,项目/添加引用


dll混淆:

http://liweizhaolili.blog.163.com/blog/static/1623074420145110502776/


dll热更新:

http://www.cnblogs.com/plateFace/p/4790437.html

0 0