关于mono dll和native dll在unity中的使用
在使用mono的dll文件时,先利用VS2015生成dll(C#源代码),然后在unity的C#脚本中使用,使用方法如下:
例如C#代码源文件如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpDllTest
{
public class Class1
{
public static int sum(int a, int b)
{
return a + b;
}
}
}
然后会在工程目录的bin/debug/目录下生成对应dll文件
同理C/C++的动态库也如此
我们在unity中某个脚本上使用上述dll文件中的接口,代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class TestDll : MonoBehaviour {
[DllImport("CppDLL", CallingConvention = CallingConvention.Cdecl, EntryPoint = "Add")]
private extern static int CppAdd(int a, int b);
// Use this for initialization
void Start () {
Debug.Log("CSharp Dll");
Debug.Log("1 + 1 = " + CSharpDLL.TestDLL.Add(1, 1));
Debug.Log("Cpp Dll");
Debug.Log("1 + 1 = " + CppAdd(1, 1));
}
// Update is called once per frame
void Update () {
}
}