unity调用C++动态链接库dll

unity调用C++的dll。传入摄像头的图片,传出三个参数。

1.C++要设置release X64版本的,生成的dll文件要放到Assets的plugins文件夹内(没有自己新建)
2.因为unity和opencv的坐标原点不同,所以要flip(一个左上角一个左下角);通道不同,所以要用CV_RGBA2BGR转换
3.如果像是x,y,z这种要传回来的指针,需要在c#中加ref
4.在C#中调用dll要先申明如下
[DllImport("PHOTOJ")]
    private static extern void Unity2OpenCVImage(IntPtr data, int width, int height,ref float x,ref float  y,ref float z);
5.memcpy图片数据是要width*heiget*4,因为mat和webCamTexture.GetPixels32()都是4通道
6.我暂时就想到这么多。。
   

   C++头文件

extern "C" _declspec(dllexport)void Unity2OpenCVImage(char* imageData, int width, int height , float*x, float*y, float*z);


    C++ cpp文件

#include  
#include   
#include   
#include "main.h"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;

extern "C" _declspec(dllexport) void Unity2OpenCVImage(char* imageData, int width, int height, float*x,float*y,float*z)
{
	if (NULL == imageData)
	{
		return;
	}
	cv::Mat opencvImage(height, width, CV_8UC4);

	
	
	memcpy(opencvImage.data, imageData, width*height*4);
	
	cvtColor(opencvImage, opencvImage, CV_RGBA2BGR);
	
	flip(opencvImage, opencvImage, 0);
	 
	cv::imshow("11",opencvImage);  
	 cv::waitKey(1); 
	*x = 0;
	*y = 0;
	*z = 0;
}
C#代码

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

public class opcnecvtest : MonoBehaviour {
    public WebCamTexture webCamTexture;
    int width = 640;
    int height = 480;
    Mat rgbaMat;
    private string cameraName = "";
    public float x =1;
    float y;
    float z;
    IntPtr pixelPointer;
    bool show = false;

    // Use this for initialization
    [DllImport("PHOTOJ")]
    private static extern void Unity2OpenCVImage(IntPtr data, int width, int height,ref float x,ref float  y,ref float z);
   
    void Start () {
        StartCoroutine (Test());
        

    }
     IEnumerator Test()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if(Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
	//开启摄像头
            WebCamDevice[] devices = WebCamTexture.devices;
            cameraName = devices[0].name;
            webCamTexture = new WebCamTexture(cameraName, width, height, 15);
            webCamTexture.Play();
            

        }
    }

    // Update is called once per frame
    void Update () {
//用opencv中的imshow函数显示unity调用的摄像头捕捉到的画面
        if (show)
        {
            Color32[] data = new Color32[width * height];
            data = webCamTexture.GetPixels32();           
            GCHandle pixelHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
            pixelPointer = pixelHandle.AddrOfPinnedObject();
//分别表示传入图片data,图片宽度,图片高度,x,y,z,后面的三个参数是要在dll中赋值后传回来的,所以前面要加ref,表示传入地址        
            Unity2OpenCVImage(pixelPointer, width, height, ref x, ref y, ref z);
            pixelHandle.Free();
        }

    }
   void OnGUI()  
    {
        
        if (GUI.Button(new UnityEngine.Rect(50, 50, 100, 100), "test"))
        {
            show = !show;
            Debug.Log(x + y + z);//如果得数为1,表示没有成功调用dll中的函数,如果为0,结果正确

        }
        GUI.DrawTexture(new UnityEngine.Rect(200, 200, 400, 300), webCamTexture, ScaleMode.ScaleToFit);
        
    }
}



你可能感兴趣的:(unity)