浏览代码

1.添加ios定位
2.添加ios国际化

slambb 3 年之前
父节点
当前提交
396f650fc4

+ 34 - 1
Assets/BowArrow/Scripts/Expand/GPSTool.cs

@@ -3,9 +3,31 @@ using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Android;
-
+using System.Runtime.InteropServices;
+using AOT;
 public class GPSTool
 {
+
+#if UNITY_IPHONE
+    static Action<string[]> callbackOut;
+    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+    public delegate void ResultHandler(string resultString);
+    [MonoPInvokeCallback(typeof(ResultHandler))]
+    static void resultHandler(string parameter)
+    {
+        //Debug.Log("unity parameter:" + parameter);
+        string[] components = parameter.Split(new char[] { '\t' }, 6);
+        string address = components[5];
+        string[] addressOut = address.Split(new char[] { '\t' }, 3);
+        callbackOut?.Invoke(addressOut);
+
+        // 停止获取位置信息
+        LocationPluginInterface.StopUpdatingLocation();
+    }
+#endif
+
+
+
     //GPS获取地址
     //成功返回:string[]{国,省,市} (数组中不允许有null)
     //失败返回:null
@@ -60,5 +82,16 @@ public class GPSTool
             callback?.Invoke(func.Invoke());
         }
 #endif
+
+#if UNITY_IPHONE
+        callbackOut = callback;
+        ResultHandler handler = new ResultHandler(resultHandler);
+        IntPtr fp = Marshal.GetFunctionPointerForDelegate(handler);
+
+        LocationPluginInterface.StartUpdatingLocation(fp);
+#endif
+
+
     }
+
 }

+ 38 - 0
Assets/BowArrow/Scripts/Expand/LocationPluginInterface.cs

@@ -0,0 +1,38 @@
+using UnityEngine;
+using System.Runtime.InteropServices;
+using AOT;
+using System;
+
+public class LocationPluginInterface
+{
+#if UNITY_IOS
+	// 访问本地插件接口的声明
+	[DllImport("__Internal")]
+	private static extern bool _startUpdatingLocation(IntPtr resultHandler);
+
+	[DllImport ("__Internal")]
+	private static extern void _stopUpdatingLocation();
+
+#endif
+
+	// 开始获取位置信息
+	public static bool StartUpdatingLocation(IntPtr resultHandler)
+	{
+#if !UNITY_EDITOR
+		return _startUpdatingLocation(resultHandler);
+#else
+		return false;
+#endif
+	}
+
+	// 停止获取位置信息
+	public static void StopUpdatingLocation()
+	{
+#if !UNITY_EDITOR
+		_stopUpdatingLocation();
+#endif
+	}
+
+
+}
+

+ 11 - 0
Assets/BowArrow/Scripts/Expand/LocationPluginInterface.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: df3ce954608c91644a7232d95443d9fc
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 164 - 0
Assets/Plugins/iOS/LocationPlugin.mm

@@ -0,0 +1,164 @@
+// LocationPlugin.mm
+#import <Foundation/Foundation.h>
+#import <CoreLocation/CoreLocation.h>
+
+typedef void (*ResultHandler) (const char *object);
+static ResultHandler gHandler;
+
+@interface LocationPlugin : NSObject<CLLocationManagerDelegate>
+// 采用CLLocationManagerDelegate协议
+{
+    CLLocationManager *locationManager;
+    bool isReverseGeocoding;            // 是否处于反地理编码处理
+}
+
++ (LocationPlugin *)sharedInstance;
+
+@end
+
+// LocationPlugin类定义
+@implementation LocationPlugin
+
+// 单例
+static LocationPlugin *sharedInstance = nil;
++ (id)sharedInstance
+{
+    @synchronized(self)
+    {
+        if(sharedInstance == nil)
+        {
+            sharedInstance = [[self alloc] init];
+        }
+    }
+    return sharedInstance;
+}
+
+// 开始获取位置信息
+- (BOOL)startUpdatingLocation
+{
+  
+    if(locationManager == nil)
+    {
+        locationManager = [[CLLocationManager alloc] init];
+    }
+
+    // 确认位置信息服务是否有效且被许可
+    BOOL isEnabledAndAuthorized = NO;
+    if([CLLocationManager locationServicesEnabled])
+    {
+        CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
+        if(status == kCLAuthorizationStatusAuthorizedAlways ||
+           status == kCLAuthorizationStatusAuthorizedWhenInUse)
+        {
+            isEnabledAndAuthorized = YES;
+        }
+    }
+    if(!isEnabledAndAuthorized)
+    {
+        // 位置信息服务无效且未被许可时,请求用户授权
+        [locationManager requestWhenInUseAuthorization];
+        return NO;
+    }
+    
+    // 开始获取位置信息
+    locationManager.delegate = self;
+    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
+    [locationManager startUpdatingLocation];
+
+    return YES;
+}
+
+// 停止获取位置信息
+- (void)stopUpdatingLocation
+{
+    if(locationManager != nil)
+    {
+        [locationManager stopUpdatingLocation];
+    }
+}
+
+#pragma mark - CLLocationManagerDelegate协议的方法实现
+
+// 更新位置信息时调用
+- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
+{
+    if(isReverseGeocoding)
+    {
+        return;
+    }
+    
+    // 基于获取的位置信息进行反地理编码以获取地址
+    isReverseGeocoding = YES;
+    
+    CLLocation *location = [locations lastObject];
+    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
+    [geocoder reverseGeocodeLocation:location
+                   completionHandler:^(NSArray *placemarks, NSError *error) {
+        isReverseGeocoding = NO;
+
+        // 从CLPlacemark对象的FormattedAddressLines中获取地址的字符串
+        NSString *addressString = @"";
+        NSString *addressOut = @"";
+        if(placemarks.count >= 1)
+        {
+            CLPlacemark *placemark = [placemarks firstObject];
+            NSArray *addressLines =
+                [placemark addressDictionary][@"FormattedAddressLines"];
+            addressString = [addressLines componentsJoinedByString:@" "];
+            addressOut = [NSString stringWithFormat:@"%@\t%@\t%@",placemark.country,placemark.administrativeArea,placemark.locality];
+        }
+
+        // 创建作为参数传递的字符串
+        NSString *parameter = [NSString stringWithFormat:@"%f\t%f\t%f\t%f\t%@\t%@",
+                               location.coordinate.latitude,
+                               location.coordinate.longitude,
+                               location.speed, location.horizontalAccuracy,
+                               addressString,addressOut];
+        gHandler (parameter.UTF8String);
+    }];
+}
+//监听权限变化
+- (void) locationManagerDidChangeAuthorization:(CLLocationManager *)manager{
+    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
+    NSLog(@"locationManagerDidChangeAuthorizatio1:%d",status);
+    if (status == kCLAuthorizationStatusAuthorizedAlways
+        || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
+        NSLog(@"locationManagerDidChangeAuthorizatio2:%@",@"11111");
+    }
+}
+
+// 其他必须的方法
+- (void)locationManager:(CLLocationManager *)manager
+      didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {}
+
+- (void)locationManagerDidPauseLocationUpdates:(CLLocationManager *)manager {}
+
+- (void)locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager {}
+
+@end
+
+#pragma mark - 接口
+
+// 为回避C++命名粉碎,以C linkage进行声明
+extern "C" {
+    // 用于调用开始获取位置信息方法的接口
+    BOOL _startUpdatingLocation(ResultHandler resultHandler)
+    {
+        gHandler = resultHandler;
+        LocationPlugin *instance = [LocationPlugin sharedInstance];
+        @synchronized(instance)
+        {
+            return [instance startUpdatingLocation];
+        }
+    }
+
+    // 用于调用停止获取位置信息方法的接口
+    void _stopUpdatingLocation()
+    {
+        LocationPlugin *instance = [LocationPlugin sharedInstance];
+        @synchronized(instance)
+        {
+            [instance stopUpdatingLocation];
+        }
+    }
+}

+ 80 - 0
Assets/Plugins/iOS/LocationPlugin.mm.meta

@@ -0,0 +1,80 @@
+fileFormatVersion: 2
+guid: bbd9a9b88fbf1d745b211ea6fd6d7b9f
+PluginImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  iconMap: {}
+  executionOrder: {}
+  defineConstraints: []
+  isPreloaded: 0
+  isOverridable: 0
+  isExplicitlyReferenced: 0
+  validateReferences: 1
+  platformData:
+  - first:
+      : Any
+    second:
+      enabled: 0
+      settings:
+        Exclude Android: 1
+        Exclude Editor: 1
+        Exclude Linux64: 1
+        Exclude OSXUniversal: 1
+        Exclude Win: 1
+        Exclude Win64: 1
+        Exclude iOS: 0
+  - first:
+      Android: Android
+    second:
+      enabled: 0
+      settings:
+        CPU: ARMv7
+  - first:
+      Any: 
+    second:
+      enabled: 0
+      settings: {}
+  - first:
+      Editor: Editor
+    second:
+      enabled: 0
+      settings:
+        CPU: AnyCPU
+        DefaultValueInitialized: true
+        OS: AnyOS
+  - first:
+      Standalone: Linux64
+    second:
+      enabled: 0
+      settings:
+        CPU: None
+  - first:
+      Standalone: OSXUniversal
+    second:
+      enabled: 0
+      settings:
+        CPU: None
+  - first:
+      Standalone: Win
+    second:
+      enabled: 0
+      settings:
+        CPU: x86
+  - first:
+      Standalone: Win64
+    second:
+      enabled: 0
+      settings:
+        CPU: x86_64
+  - first:
+      iPhone: iOS
+    second:
+      enabled: 1
+      settings:
+        AddToEmbeddedBinaries: false
+        CPU: AnyCPU
+        CompileFlags: 
+        FrameworkDependencies: CoreLocation;
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 108 - 10
ProjectSettings/ProjectSettings.asset

@@ -9,11 +9,11 @@ PlayerSettings:
   AndroidFilterTouchesWhenObscured: 0
   AndroidEnableSustainedPerformanceMode: 0
   defaultScreenOrientation: 2
-  targetDevice: 2
+  targetDevice: 0
   useOnDemandResources: 0
   accelerometerFrequency: 60
   companyName: JssF
-  productName: "\u9752\u51E4\u9E3E"
+  productName: HOUYI
   defaultCursor: {fileID: 0}
   cursorHotspot: {x: 0, y: 0}
   m_SplashScreenBackgroundColor: {r: 0.13725491, g: 0.12156863, b: 0.1254902, a: 1}
@@ -154,13 +154,14 @@ PlayerSettings:
   applicationIdentifier:
     Android: com.xmjssvr.BowArrow.mi
     Standalone: com.JssF.BowArrow
+    iPhone: com.xmjssvr.BowArrow
   buildNumber:
     Standalone: 0
     iPhone: 0
     tvOS: 0
   overrideDefaultApplicationIdentifier: 1
-  AndroidBundleVersionCode: 5
-  AndroidMinSdkVersion: 23
+  AndroidBundleVersionCode: 2
+  AndroidMinSdkVersion: 19
   AndroidTargetSdkVersion: 31
   AndroidPreferredInstallLocation: 1
   aotOptions: 
@@ -225,9 +226,9 @@ PlayerSettings:
   appleDeveloperTeamID: 
   iOSManualSigningProvisioningProfileID: 
   tvOSManualSigningProvisioningProfileID: 
-  iOSManualSigningProvisioningProfileType: 0
+  iOSManualSigningProvisioningProfileType: 1
   tvOSManualSigningProvisioningProfileType: 0
-  appleEnableAutomaticSigning: 0
+  appleEnableAutomaticSigning: 2
   iOSRequireARKit: 0
   iOSAutomaticallyDetectAndAddCapabilities: 1
   appleEnableProMotion: 0
@@ -242,7 +243,7 @@ PlayerSettings:
   useCustomBaseGradleTemplate: 0
   useCustomGradlePropertiesTemplate: 0
   useCustomProguardFile: 0
-  AndroidTargetArchitectures: 2
+  AndroidTargetArchitectures: 1
   AndroidSplashScreenScale: 0
   androidSplashScreen: {fileID: 0}
   AndroidKeystoreName: "{inproject}: Documents/App\u79C1\u94A5/2022-11-17/smartbow.keystore"
@@ -365,6 +366,103 @@ PlayerSettings:
       m_Height: 36
       m_Kind: 0
       m_SubKind: 
+  - m_BuildTarget: iPhone
+    m_Icons:
+    - m_Textures: []
+      m_Width: 120
+      m_Height: 120
+      m_Kind: 3
+      m_SubKind: iPhone
+    - m_Textures: []
+      m_Width: 80
+      m_Height: 80
+      m_Kind: 3
+      m_SubKind: iPhone
+    - m_Textures: []
+      m_Width: 80
+      m_Height: 80
+      m_Kind: 3
+      m_SubKind: iPad
+    - m_Textures: []
+      m_Width: 40
+      m_Height: 40
+      m_Kind: 3
+      m_SubKind: iPad
+    - m_Textures: []
+      m_Width: 87
+      m_Height: 87
+      m_Kind: 1
+      m_SubKind: iPhone
+    - m_Textures: []
+      m_Width: 58
+      m_Height: 58
+      m_Kind: 1
+      m_SubKind: iPhone
+    - m_Textures: []
+      m_Width: 29
+      m_Height: 29
+      m_Kind: 1
+      m_SubKind: iPhone
+    - m_Textures: []
+      m_Width: 58
+      m_Height: 58
+      m_Kind: 1
+      m_SubKind: iPad
+    - m_Textures: []
+      m_Width: 29
+      m_Height: 29
+      m_Kind: 1
+      m_SubKind: iPad
+    - m_Textures: []
+      m_Width: 60
+      m_Height: 60
+      m_Kind: 2
+      m_SubKind: iPhone
+    - m_Textures: []
+      m_Width: 40
+      m_Height: 40
+      m_Kind: 2
+      m_SubKind: iPhone
+    - m_Textures: []
+      m_Width: 40
+      m_Height: 40
+      m_Kind: 2
+      m_SubKind: iPad
+    - m_Textures: []
+      m_Width: 20
+      m_Height: 20
+      m_Kind: 2
+      m_SubKind: iPad
+    - m_Textures: []
+      m_Width: 1024
+      m_Height: 1024
+      m_Kind: 4
+      m_SubKind: App Store
+    - m_Textures: []
+      m_Width: 180
+      m_Height: 180
+      m_Kind: 0
+      m_SubKind: iPhone
+    - m_Textures: []
+      m_Width: 120
+      m_Height: 120
+      m_Kind: 0
+      m_SubKind: iPhone
+    - m_Textures: []
+      m_Width: 167
+      m_Height: 167
+      m_Kind: 0
+      m_SubKind: iPad
+    - m_Textures: []
+      m_Width: 152
+      m_Height: 152
+      m_Kind: 0
+      m_SubKind: iPad
+    - m_Textures: []
+      m_Width: 76
+      m_Height: 76
+      m_Kind: 0
+      m_SubKind: iPad
   m_BuildTargetBatching:
   - m_BuildTarget: Standalone
     m_StaticBatching: 1
@@ -449,8 +547,8 @@ PlayerSettings:
   enableInternalProfiler: 0
   logObjCUncaughtExceptions: 1
   enableCrashReportAPI: 0
-  cameraUsageDescription: 
-  locationUsageDescription: 
+  cameraUsageDescription: "App\u9700\u8981\u60A8\u7684\u540C\u610F,\u624D\u80FD\u8BBF\u95EE\u7167\u76F8\u673A"
+  locationUsageDescription: "\u6211\u4EEC\u9700\u8981\u901A\u8FC7\u60A8\u7684\u5730\u7406\u4F4D\u7F6E\u4FE1\u606F\u83B7\u53D6\u60A8\u5468\u8FB9\u7684\u76F8\u5173\u6570\u636E"
   microphoneUsageDescription: 
   switchNMETAOverride: 
   switchNetLibKey: 
@@ -694,7 +792,7 @@ PlayerSettings:
   additionalCompilerArguments: {}
   platformArchitecture: {}
   scriptingBackend:
-    Android: 1
+    Android: 0
     Standalone: 0
   il2cppCompilerConfiguration:
     Standalone: 1