using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Class1
{
[DllImport("Dll1")]
private static extern int Add(int a, int b);
public static int C_Add(int a, int b)
{
return Add(a,b);
}
}
}
5.生成dll
6.将生成和引用到的dll都放入unity工程中(至少要放入引用的dll)
2.unity调用
简单调用
Debug.Log(ClassLibrary1.Class1.C_Add(1, 2));
加载调用
void T1()
{
var p = @"D:\Projects\CPlusProjects\ClassLibrary1\ClassLibrary1\bin\Release\ClassLibrary1.dll";
byte[] bin;
using (var fs = new FileStream(p, FileMode.Open))
{
using (var br = new BinaryReader(fs))
{
bin = br.ReadBytes((int)fs.Length);
}
}
var assembly = Assembly.Load(bin);
var t = assembly.GetType("ClassLibrary1.Class1");
var instance = Activator.CreateInstance(t);
var m = t.GetMethod("C_Add");
Debug.Log(m.Invoke(instance, new object[] { 1, 2 }));
}