一、使用vs2017生成c動(dòng)態(tài)庫(kù)
1、 文件->新建->項(xiàng)目

2、 visual C++ -> Windows桌面 -> Windows桌面向?qū)?,并修改下邊文件名和路?/h2>
應(yīng)用程序類(lèi)型選擇:動(dòng)態(tài)鏈接庫(kù)(.dll) 勾選空項(xiàng)目
3.添加c文件 在《源文件》右鍵選擇 添加->新建項(xiàng)
4選擇c++文件(.cpp) ,下邊的文件名改成dll_test.c,注意是.c后綴 點(diǎn)擊添加
5.添加頭文件 《頭文件》右鍵選擇 添加->新建項(xiàng)
選擇頭文件并更改文件名 dll_test.h 點(diǎn)擊添加
6,在c文件中紅添加測(cè)試代碼。注意一定要包含剛在自己建的頭文件,因?yàn)槔镞呌袑?dǎo)出的語(yǔ)句


3.添加c文件 在《源文件》右鍵選擇 添加->新建項(xiàng)


5.添加頭文件 《頭文件》右鍵選擇 添加->新建項(xiàng)


6,在c文件中紅添加測(cè)試代碼。注意一定要包含剛在自己建的頭文件,因?yàn)槔镞呌袑?dǎo)出的語(yǔ)句
#include <stdio.h>
#include "dll_test.h" //剛才新建的頭文件一定要包含
int add(int a, int b)
{
????return a + b;
}
在.h文件中添加代碼
#ifndef? _DLL_TEST_
#define _DLL_TEST_
#pragma once
__declspec(dllexport) int add(int a, int b);
#endif
7.將項(xiàng)目改成x64的編譯環(huán)境,和unity環(huán)境對(duì)應(yīng)

8.生成->生成解決方案 就會(huì)看到生成的dll了

二、unity3d使用dll庫(kù)
1、unity新建個(gè)工程

2、隨便添加個(gè)物體 GameObject->3D Object->Cube

3、添加腳本 ,并將腳本拖拽到物體上


將生成的dll拖拽到Assets處,也可以建個(gè)文件夾放進(jìn)去

4編輯main文件添加測(cè)試代碼([DllImport("c_dll")] 導(dǎo)入dll庫(kù))
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class main : MonoBehaviour {
????[DllImport("c_dll")]
????private static extern int add(int a, int b);
????// Use this for initialization
????int a = add(3, 5);
????void Start () {
????????print("a+b= " + a);
????}
????// Update is called once per frame
????void Update () {
????}
}
4、運(yùn)行查看輸出程序正常運(yùn)行

