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 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 callbackRecord; // public static void OnLocationUpdate() // { // if (callbackRecord != null) // { // GetAddress(callbackRecord); // callbackRecord = null; // } // } /// 兼容 MsgReceiver 等 JNI 调用链;已不再衔接 GPS 延迟回调。 public static void OnLocationUpdate() { } #endif //GPS获取地址 //成功返回:string[]{国,省,市} (数组中不允许有null) //失败返回:null public static void GetAddress(Action 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("currentActivity")) // using (var objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool")) // { // bool gpsOpened = objGPSTool.CallStatic("hasOpenGPS", objCtx); // if (!gpsOpened) // { // var view = OpenGPSView.Init(); // if (view) view.onOpened = () => GetAddress(callback); // return; // } // address = objGPSTool.CallStatic("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 permissions = new List(); // 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(); } }