原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0427/473.html
Unity自带了游戏开发运营Input.Location的方式去获取GPS数据,但是假如你想后台获取GPS数据是没法完成的。
那就只能运用IOS原生代码去完成这个功用了。办法也很简单。
1.设置Info.plist
<key>NSLocationAlwaysUsageDescription</key>
<string>I need Location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
</array>
2.添加CoreLocation库.
3.修正UnityAppController.h文件(蓝色局部内容)
#import <QuartzCore/CADisplayLink.h>
#import <CoreLocation/CoreLocation.h>
#include "PluginBase/RenderPluginDelegate.h"
@class UnityView;
@class DisplayConnection;
@interface UnityAppController :NSObject<CLLocationManagerDelegate>
4.修正UnityAppController.mm文件
//添加内容
//>>>>>>>>>>>>>>>>>>>>>>
static UnityAppController *globalSelf;
CLLocationManager *locationManager;
CLLocation *myOldLocation;
extern "C"void StartGPSUpdate(){
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=globalSelf;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;//定位精度
locationManager.distanceFilter=1;//kCLDistanceFilterNone定时更新 1:每隔一米更新一次
locationManager.pausesLocationUpdatesAutomatically=NO;//设置能否允许系统自动暂停定位,这里要设置为NO,刚开端我没有设置,后台定位持续20分钟左右就中止了!
[locationManager requestWhenInUseAuthorization];
[locationManager startMonitoringSignificantLocationChanges];
if(![CLLocationManager locationServicesEnabled]){
NSLog(@"请开启定位:设置 >隐私 > 位置 >定位效劳");
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >=8) {
//[_locationManager requestWhenInUseAuthorization];//⓵只在前台开启定位
[locationManager requestAlwaysAuthorization];//⓶在后台也可定位
}
// 5.iOS9新特性:将允许呈现这种场景:同一app中多个location manager:一些常州手游开发只能在前台定位,另一些可在后台定位(并可随时制止其后台定位)。
if ([[[UIDevice currentDevice] systemVersion] floatValue] >=9) {
locationManager.allowsBackgroundLocationUpdates =YES;
}
[locationManager startUpdatingLocation];
NSLog(@"start gps update");
}
extern "C"void StopGPSUpdate(){
[locationManager stopUpdatingLocation];
NSLog(@"stop gps update");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
myOldLocation=currentLocation;
NSString *strResult=@"didUpdateToLoation";
if (currentLocation !=nil) {
NSString *lat = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
NSString *lon=[NSString stringWithFormat:@"%.8f",currentLocation.coordinate.longitude];
NSString *ele=[NSString stringWithFormat:@"%0.8f",currentLocation.altitude];
strResult=[NSString stringWithFormat:@"{\"state\":\"UpdateGPS\",\"latitude\":\"%@\",\"longitude\":\"%@\",\"altitude\":\"%@\" }",lat,lon,ele];
UnitySendMessage("GPSSDK","IOSGPSUpdate", [strResult UTF8String]);
/*
_longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
_verticalAccuracy.text=[NSString stringWithFormat:@"%.8f",currentLocation.altitude];
_horizontalAccuracy.text=[NSString stringWithFormat:@"%.8f",currentLocation.horizontalAccuracy];
_vertical.text=[NSString stringWithFormat:@"%.8f",currentLocation.verticalAccuracy];*/
}
// Reverse Geocoding
NSLog(@"Resolving the Address:%@",strResult);
}
//<<<<<<<<<<<<<<<<<<<<<<<
- (id)init
{
if( (self = [super init]) )
{
// due to clang issues with generating warning for overriding deprecated methods
// we will simply assert if deprecated methods are present
// NB: methods table is initied at load (before this call), so it is ok to check for override
NSAssert(![self respondsToSelector:@selector(createUnityViewImpl)],
@"createUnityViewImpl is deprecated and will not be called. Override createUnityView"
);
NSAssert(![self respondsToSelector:@selector(createViewHierarchyImpl)],
@"createViewHierarchyImpl is deprecated and will not be called. Override willStartWithViewController"
);
NSAssert(![self respondsToSelector:@selector(createViewHierarchy)],
@"createViewHierarchy is deprecated and will not be implemented. Use createUI"
);
}
globalSelf=self;//留意常州游戏开发培训一定要添加该行代码
returnself;
}
这样就完成了GPS开启中止以及后台更新GPS了。
上篇:上一篇:Unity3D IOS后台推送
下篇:下一篇:U3D 事情系统