我使用的是 vs2012生成的DLL
1使用vs创建一个 名为 TestDll 的 Win32控制台应用程序
, 应用程序类型选择: DLL, 附加类型选择:空项目.
2添加TestDll.h, TestDll.cpp文件,并生成 TestDll.dll文件
TestDll.h
#ifndef _TEST_DLL_H_
#define _TEST_DLL_H_
#endif
#if defined (EXPORTBUILD)
# define _DLLExport __declspec (dllexport)
# else
# define _DLLExport __declspec (dllimport)
#endif
extern "C" int _DLLExport add( int x, int y );
_DLLExport class TestDll
{
public:
TestDll(void);
~TestDll(void);
};
TestDll.cpp
#define EXPORTBUILD
#include "TestDll.h"
int add(int x, int y )
{
return x + y;
}
TestDll::TestDll(void)
{
}
TestDll::~TestDll(void)
{
}生成DLL
3把生成的TestDll拖到Unity的Asset-->Plugins目录(没有就创造一个)
4新建一个UnityTestDll C#文件
UnityTestDll.cs
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class UnityTestDll : MonoBehaviour {
[DllImport("TestDll")]
private static extern int add( int x, int y );
int i = add( 5, 7 );
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUI.Button( new Rect( 1, 1, 200, 100 ), "this dll i = 5+7, i is" + i );
}
}
5运行Unity显示结果
本文详细介绍了如何在Visual Studio 2012中创建一个DLL,包括添加头文件、实现函数以及生成DLL文件的过程。接着,展示了如何将生成的DLL文件拖入Unity Asset--Plugins目录,并在Unity中通过C#脚本调用DLL中的函数。最终演示了运行Unity并显示结果的过程。
579

被折叠的 条评论
为什么被折叠?



