u3d链接/调用c++动态库

软件版本:vs2017 unity2018.1

打开vs2017,看左上角。  文件——>新建——>项目

看下图,Visual C++ ——>空项目——>名称(自定义个)

找到解决方案资源管理器,右键源文件——>添加——>类

  

输入类名

删除生成的.h和.cpp的代码,替换下面代码

.cpp里面

#include "JeroTestDllPro1.h"

int HappinessNum()
{
    return 10086;
}

.h里面

extern"C" __declspec(dllimport) int HappinessNum();

替换后如下图:

右键解决方案“XXXX”,配置属性——>平台——>x64——>确定

同样更改调试发布栏的配置为x64

右键JeroTestU3dRefDll——>属性

更改为动态库(.dll)

配置完成后按shift+ctrl+b生成dll文件

 

丢进unity的Plugins下,新建个脚本运行如下代码便可运行

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;

public class MyTest : MonoBehaviour
{
    [DllImport("JeroTestU3dRefDll")]
    private static extern int HappinessNum();

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            Debug.LogWarning(HappinessNum());
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/JeroChan/p/9647181.html