下面的代码是完整的插件代码。我正在尝试使用此插件检索纬度和经度。我创建了一个C#脚本,粘贴了此代码并将其重命名为Position.mm文件。我添加了一个包含插件文件夹的SDK因此,最初我将其复制到该文件夹中。它显示错误 EntryPointNotFoundException:_GetCoordinates Getposition.GetCoordinates()。我创建了一个文件夹Assets / Plugins文件夹并将插件复制到该文件夹中不起作用。
#import "Position.h"
#import <CoreLocation/CoreLocation.h>
#import <CoreMotion/CoreMotion.h>
//Convert a C++ string to NSString
NSString* CPPStringToIOSString(const char* cppString)
{
return cppString?[NSString stringWithUTF8String: cppString] : nullptr;
}
//Convert an NSString to C++ string.
const char* IOSStringToCPPString(NSString * iosString)
{
if ([iosString length])
{
const char* str = [iosString UTF8String];
if (str && strlen(str))
{
char* unityString = static_cast<char*>(malloc(strlen(str) + 1));
strcpy(unityString, str);
return unityString;
}
}
return nullptr;
}
static LocationServiceInfo GetLocationServiceInfo()
{
static LocationServiceInfo info;
return info;
}
//Exports:
extern "C" {
//When called, returns a CLLocationCoordinate2D to Unity.
CLLocationCoordinate2D _GetCoordinates()
{
CLLocationManager* manager = GetLocationServiceInfo().GetLocationManager();
return [manager getCurrentLocation];
}
//When called, registers a listen with LocationManager.
//The listener is called whenever the location has updated/changed.
void _RegisterLocationUpdateHandler(void(* callback)(double, double))
{
//Implement your own logic for this..
//Then call `callback` with the lat and lon.
GetLocationServiceInfo().onLocationDidUpdate = ^ (double lat, double lon) {
if (callback)
{
callback(lat, lon);
}
};
}
}
这个插件或我访问它的方式有任何问题。下面是我尝试从插件访问值的方式。
//Struct representing coordinates received from the iOS plugin.
public struct CLLocationCoordinate2D
{
double latitude;
double longitude;
}
//Imported function.
[DllImport("__Internal")]
private static extern CLLocationCoordinate2D _GetCoordinates();
//Call the imported `GetCoordinates` when this function is called.
public static CLLocationCoordinate2D GetCoordinates()
{
return _GetCoordinates();
}
在Update方法中
void Update ()
{
if (GetCoordinates().ToString() != null)
{
Debug.Log("Getcordinates Plugin " + GetCoordinates().ToString() );
}
}