[Unity2d系列教程] 002.引用外部DLL

上一篇我们学习了Unity调用C#生成的外部DLL,但是有时候我们需要访问底层,不能不适用C生成的DLL。下面就让我们一起学习下,C如何生成

1.创建一个C的控制台程序

2.点击确定->点击下一步

3.点击完成生成了空项目,然后在头文件创建TestC.h文件

#if defined (EXPORTBUILD)  
	# define _DLLExport __declspec (dllexport)  
# else 
	# define _DLLExport __declspec (dllimport) 
#endif    
extern "C"  int _DLLExport fnTestC();   

 4.在源文件创建TestC.ccp文件

//宏定义 
#define  EXPORTBUILD   
//加载头文件  
#include "TestC.h"  

//设置函数  
int _DLLExport fnTestC()
{      
	return 36;  
}   

 5.设置编译环境为X64

6.右键解决方案->生成解决方案,就可以获取DLL文件,放在Assets/plugins下面,然后代码中引用

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

public class carMove : MonoBehaviour {

        [DllImport("TestC")]
        public static extern int fnTestC();

	// Use this for initialization
	void Start () {
        Debug.Log("我来测试下c" + fnTestC());
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

 7.输出结果

 

本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 

转载自【宝宝巴士SuperDo团队】原文链接: http://www.cnblogs.com/superdo/p/5148695.html

原文地址:https://www.cnblogs.com/superdo/p/5148695.html