1. 程式人生 > >[Unity基礎]unity呼叫dll檔案以及反射載入dll

[Unity基礎]unity呼叫dll檔案以及反射載入dll

參考連結:

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 initialization
	void 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