赞
踩
方便不知道如何再unity调用各平台动态库的同学参考,实测可用:
window案例
动态库名
tracker_model.dll
位置
unity工程 Assets\Plugins目录下
c# 关键调用
代码
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.Collections.Generic;
//[StructLayout(LayoutKind.Sequential)]
public struct Point3F {
public float x;
public float y;
public float z;
}
public class Control : MonoBehaviour {
System.IntPtr center_data_prt;
System.IntPtr fingertips_data_prt;
[DllImport("tracker_model")]
public static extern long points_init (string iin);
........
void Start () {
handle = points_init(fileName);
...................
center_data_prt = Marshal. AllocHGlobal(Marshal.SizeOf(typeof(Point3F)));
fingertips_data_prt = Marshal. AllocHGlobal(Marshal.SizeOf(typeof(Point3F))*maxFingerTips);
tri_cnt = points_get_fingertips(handle, center_data_prt, fingertips_data_prt
}
void Update () {
Point3F center = (Point3F)Marshal.PtrToStructure(
(System.IntPtr)(center_data_prt.ToInt64()),typeof(Point3F));
for (int i = 0; i < tri_cnt; i++) {
Point3F point = (Point3F)Marshal.PtrToStructure(
(System.IntPtr)(fingertips_data_prt.ToInt64()+i*Marshal.SizeOf(typeof(Point3F))),typeof(Point3F));
}
}
void OnDestroy(){
Marshal.FreeHGlobal(center_data_prt);
Marshal.FreeHGlobal(fingertips_data_prt);
}
对应tracker_model.dll c++头文件接口
#ifndef _model_api_H_
#define _model_api_H_
#ifdef WIN32
#ifdef HTM_DLL_EXPORT
#define HTM_API extern "C" _declspec(dllexport)
#else
#define HTM_API extern "C" _declspec(dllimport)
#endif
#else
#define HTM_API
#endif
struct Point3F
{
float x;
float y;
float z;
};
HTM_API long points_init(const char *config_file);
HTM_API int points_get_tri(long handle, Point3F* triangle_vertex_list, int list_len);
~~~~~~~
具体动态库编译过程不叙述
Android jni
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。