| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- // using UnityEngine.Android; // 旧:Permission 申请定位权限(已废弃,保留备查)
- 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
- #if UNITY_ANDROID
- // 旧:原生层在定位就绪后会通知 Unity,用于延迟再次 GetAddress(保留,勿删)
- // private static Action<string[]> callbackRecord;
- // public static void OnLocationUpdate()
- // {
- // if (callbackRecord != null)
- // {
- // GetAddress(callbackRecord);
- // callbackRecord = null;
- // }
- // }
- /// <summary>兼容 MsgReceiver 等 JNI 调用链;已不再衔接 GPS 延迟回调。</summary>
- public static void OnLocationUpdate() { }
- #endif
- //GPS获取地址
- //成功返回:string[]{国,省,市} (数组中不允许有null)
- //失败返回:null
- public static void GetAddress(Action<string[]> callback)
- {
- #if UNITY_ANDROID
- // Android:不再申请定位权限 / 不调用原生 GPS;地区取自用户资料(与排行榜一致),未设置国家则返回 null
- string c = LoginMgr.myUserInfo.country != null ? LoginMgr.myUserInfo.country.Trim() : "";
- string s = LoginMgr.myUserInfo.state != null ? LoginMgr.myUserInfo.state.Trim() : "";
- string ci = LoginMgr.myUserInfo.city != null ? LoginMgr.myUserInfo.city.Trim() : "";
- if (string.IsNullOrWhiteSpace(c))
- callback?.Invoke(null);
- else
- callback?.Invoke(new string[] { c, s, ci });
- // 旧 Android:Unity 权限申请 + 原生 GPSTool(hasOpenGPS / getAddress / OpenGPSView)(保留,勿删)
- // Action callAndroidMethod = delegate ()
- // {
- // if (Application.isEditor) return;
- // string[] address = null;
- // using (var clsU3D = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- // using (var objCtx = clsU3D.GetStatic<AndroidJavaObject>("currentActivity"))
- // using (var objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool"))
- // {
- // bool gpsOpened = objGPSTool.CallStatic<bool>("hasOpenGPS", objCtx);
- // if (!gpsOpened)
- // {
- // var view = OpenGPSView.Init();
- // if (view) view.onOpened = () => GetAddress(callback);
- // return;
- // }
- // address = objGPSTool.CallStatic<string[]>("getAddress", objCtx);
- // if (address != null)
- // {
- // foreach (var item in address)
- // {
- // if (item == null)
- // {
- // address = null;
- // break;
- // }
- // }
- // }
- // if (address != null)
- // {
- // Debug.Log("获取地理位置成功:" + string.Join(" ", address));
- // callback?.Invoke(address);
- // }
- // //如果安卓层未能获取到位置(比如gps刚打开),会等待gps更新,至到知道位置后,会通知Unity
- // //因此可以先保留callback用于下次回调
- // else if (address == null) callbackRecord = callback;
- // }
- // };
- // List<string> permissions = new List<string>();
- // permissions.Add(Permission.CoarseLocation);
- // permissions.Add(Permission.FineLocation);
- // for (int i = permissions.Count - 1; i >= 0; i--)
- // {
- // if (Permission.HasUserAuthorizedPermission(permissions[i])) permissions.RemoveAt(i);
- // }
- // if (permissions.Count > 0)
- // {
- // var requestCB = new PermissionCallbacks();
- // int needPermissionCount = permissions.Count;
- // requestCB.PermissionGranted += (s) =>
- // {
- // Debug.Log("用户同意" + s);
- // needPermissionCount--;
- // if (needPermissionCount == 0) callAndroidMethod.Invoke();
- // };
- // bool hasExecuteOnDenied = false;
- // requestCB.PermissionDenied += (s) =>
- // {
- // Debug.Log("用户拒绝" + s);
- // if (!hasExecuteOnDenied)
- // {
- // hasExecuteOnDenied = true;
- // AlertNoPermission();
- // }
- // };
- // requestCB.PermissionDeniedAndDontAskAgain += (s) =>
- // {
- // Debug.Log("用户拒绝且要求不再询问" + s);
- // if (!hasExecuteOnDenied)
- // {
- // hasExecuteOnDenied = true;
- // AlertNoPermission();
- // }
- // };
- // Permission.RequestUserPermissions(permissions.ToArray(), requestCB);
- // }
- // else
- // {
- // callAndroidMethod.Invoke();
- // }
- #endif
- #if UNITY_IPHONE
- callbackOut = callback;
- ResultHandler handler = new ResultHandler(resultHandler);
- IntPtr fp = Marshal.GetFunctionPointerForDelegate(handler);
- LocationPluginInterface.StartUpdatingLocation(fp);
- #endif
- }
- //提示用户:尚未授予定位权限
- static void AlertNoPermission()
- {
- ModalManager.ShowConfirmModal_NoLocationPermission();
- }
- }
|