Unity 实现跳转ios 设置界面

Unity 实现跳转ios 设置界面

一、功能需求 iOS网络设置检查,如果没有网络可能是用户选择关闭了 网络链接,ios可以从游戏内直接跳转到设置网络开关。

二、网络检测 直接上代码


        public static bool HasNetwork() {
            return Application.internetReachability != NetworkReachability.NotReachable;
        }

        public static bool IsWiFi() {
            return Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork;
        }

        public static bool IsCellular() {
            return Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork;
        }

三、实现unity调用 ios原生界面
1)unity 调用ios方法

using System;
using UnityEngine;
using System.Runtime.InteropServices;


public class Platform {
#if UNITY_IPHONE
    [DllImport("__Internal")]
    public static extern void __OpenSystemTimeSetting();
    [DllImport("__Internal")]
    public static extern void __TelPhoneNumber(string str);
    [DllImport("__Internal")]
    public static extern bool _IsHadInstalledWeixin();
    [DllImport("__Internal")]
    public static extern string __GetDeviceVersion(); 
#endif
    public static DeviceGeneration GetiOSDeviceGeneration() {
        string str = GetIphoneVersion();
        switch (str) {
            case "iPhone1,1":
                return DeviceGeneration.iPhone;
            case "iPhone1,2":
                return DeviceGeneration.iPhone3G;
            case "iPhone2,1":
                return DeviceGeneration.iPhone3GS;
            case "iPhone3,1":
            case "iPhone3,2":
            case "iPhone3,3":
                return DeviceGeneration.iPhone4;
            case "iPhone4,1":
                return DeviceGeneration.iPhone4S;
            case "iPhone5,1":
            case "iPhone5,2":
                return DeviceGeneration.iPhone5;
            case "iPhone5,3":
            case "iPhone5,4":
                return DeviceGeneration.iPhone5C;
            case "iPhone6,1":
            case "iPhone6,2":
                return DeviceGeneration.iPhone5S;
            case "iPhone7,2":
                return DeviceGeneration.iPhone6;
            case "iPhone7,1":
                return DeviceGeneration.iPhone6Plus;
            case "iPhone8,1":
                return DeviceGeneration.iPhone6S;
            case "iPhone8,2":
                return DeviceGeneration.iPhone6SPlus;
            case "iPhone8,4":
                return DeviceGeneration.iPhoneSE1Gen;
            case "iPhone9,1":
            case "iPhone9,3":
                return DeviceGeneration.iPhone7;
            case "iPhone9,2":
            case "iPhone9,4":
                return DeviceGeneration.iPhone7Plus;
            case "iPhone10,1":
            case "iPhone10,4":
                return DeviceGeneration.iPhone8;
            case "iPhone10,2":
            case "iPhone10,5":
                return DeviceGeneration.iPhone8Plus;
            case "iPhone10,3":
            case "iPhone10,6":
                return DeviceGeneration.iPhoneX;
            case "iPad1,1":
                return DeviceGeneration.iPad1Gen;
            case "iPad2,1":
            case "iPad2,2":
            case "iPad2,3":
            case "iPad2,4":
                return DeviceGeneration.iPad2Gen;
            case "iPad3,1":
            case "iPad3,2":
            case "iPad3,3":
                return DeviceGeneration.iPad3Gen;
            case "iPad3,4":
            case "iPad3,5":
            case "iPad3,6":
                return DeviceGeneration.iPad4Gen;
            case "iPad4,1":
            case "iPad4,2":
            case "iPad4,3":
                return DeviceGeneration.iPadAir1;
            case "iPad5,3":
            case "iPad5,4":
                return DeviceGeneration.iPadAir2;
            case "iPad6,7":
            case "iPad6,8":
                return DeviceGeneration.iPadPro1Gen;
            case "iPad6,3":
            case "iPad6,4":
                return DeviceGeneration.iPadPro10Inch1Gen;
            case "iPad7,1":
            case "iPad7,2":
                return DeviceGeneration.iPadPro2Gen;
            case "iPad7,3":
            case "iPad7,4":
                return DeviceGeneration.iPadPro10Inch2Gen;
            case "iPad2,5":
            case "iPad2,6":
            case "iPad2,7":
                return DeviceGeneration.iPadMini1Gen;
            case "iPad4,4":
            case "iPad4,5":
            case "iPad4,6":
                return DeviceGeneration.iPadMini2Gen;
            case "iPad4,7":
            case "iPad4,8":
            case "iPad4,9":
                return DeviceGeneration.iPadMini3Gen;
            case "iPad5,1":
            case "iPad5,2":
                return DeviceGeneration.iPadMini4Gen;
            case "iPod1,1":
                return DeviceGeneration.iPodTouch1Gen;
            case "iPod2,1":
                return DeviceGeneration.iPodTouch2Gen;
            case "iPod3,1":
                return DeviceGeneration.iPodTouch3Gen;
            case "iPod4,1":
                return DeviceGeneration.iPodTouch4Gen;
            case "iPod5,1":
                return DeviceGeneration.iPodTouch5Gen;
            case "iPod7,1":
                return DeviceGeneration.iPodTouch6Gen;
            default:
                return DeviceGeneration.Unknown;
        }
    }

    public static string GetIphoneVersion() {
#if !UNITY_EDITOR
#if !UNITY_ANDROID
            return __GetDeviceVersion();
#endif
#endif
        return "";
    }

    public static void OpenSystemSetting() {
#if !UNITY_EDITOR
#if UNITY_IPHONE
        __OpenSystemTimeSetting();
#elif UNITY_ANDROID
        callAndroidFunc("OpenSystemTimeSetting");
#endif
#endif
    }

    public static string GetChannel() {
#if !UNITY_EDITOR
#if UNITY_ANDROID
            return callAndroidFunc<string>("GetChannel");
#else
            return "App Store";
#endif
#else
        return "Editor";
#endif
    }

    public static void TelPhoneNumber(string phoneNumber) {
#if !UNITY_EDITOR
#if UNITY_IPHONE
            //__TelPhoneNumber(phoneNumber);
#elif UNITY_ANDROID
            callAndroidFunc("TelPhoneNumber",phoneNumber);
#endif
#endif
    }

    public static bool IsWeixinAvailable() {
        try {
#if !UNITY_EDITOR
#if UNITY_IPHONE
                        return _IsHadInstalledWeixin();
//                      return true;
#elif UNITY_ANDROID
                        return callAndroidFunc<bool>("isWeixinAvilable");
#else

#endif
#endif
            return true;

        } catch (Exception e) {
            LogException(e);
        }
        return false;
    }

    public static string GetDeviceBrand() {
#if !UNITY_EDITOR
#if UNITY_ANDROID
                    return callAndroidFunc<string>("GetDeviceBrand");
#else
                    return "Apple";
#endif
#endif
        return "MacOS";
    }

    public static string GetSoucre() {
        try {
#if UNITY_IPHONE
            return "iOS";
#elif UNITY_ANDROID
            return "android";
#else
                return "iOS";
#endif
        } catch (Exception e) {
            LogException(e);
        }
        return string.Empty;
    }

    public static string GetVersion() {
        return Application.version;
    }

    public static string GetOSVersion() {
        return SystemInfo.operatingSystem;
    }

    public static string GetDeviceModel() {
        return SystemInfo.deviceModel;
    }

    public static bool IsIpx() {
#if UNITY_IPHONE
            if (GetiOSDeviceGeneration() == DeviceGeneration.iPhoneX) {
                return true;
            }
            return false;           
#else
        return false;
#endif
    }

    public static bool IsIPad() {
#if UNITY_IPHONE
            if (GetiOSDeviceGeneration () == DeviceGeneration.iPad1Gen
               || GetiOSDeviceGeneration () == DeviceGeneration.iPad2Gen
               || GetiOSDeviceGeneration () == DeviceGeneration.iPad3Gen
               || GetiOSDeviceGeneration () == DeviceGeneration.iPad4Gen
               || GetiOSDeviceGeneration () == DeviceGeneration.iPadAir1
               || GetiOSDeviceGeneration () == DeviceGeneration.iPadAir2
               || GetiOSDeviceGeneration () == DeviceGeneration.iPadPro1Gen
               || GetiOSDeviceGeneration () == DeviceGeneration.iPadPro10Inch1Gen
               || GetiOSDeviceGeneration () == DeviceGeneration.iPadPro2Gen
               || GetiOSDeviceGeneration () == DeviceGeneration.iPadPro10Inch2Gen
               || GetiOSDeviceGeneration () == DeviceGeneration.iPadMini1Gen
               || GetiOSDeviceGeneration () == DeviceGeneration.iPadMini2Gen
               || GetiOSDeviceGeneration () == DeviceGeneration.iPadMini3Gen
               || GetiOSDeviceGeneration () == DeviceGeneration.iPadMini4Gen) {
                return true;
            }
            return false;
#else
        return false;
#endif
    }

    private static T callAndroidFunc(string funName, params object[] args) {
        try {
           using (AndroidJavaClass jcla = new AndroidJavaClass("com.Platform")) {
               using (AndroidJavaObject instance = jcla.CallStatic("getInstance")) {
                   return instance.Call(funName, args);
               }
           }
        } catch (Exception e) {
           LogException(e);
        }
        return default(T);
    }

    private static void callAndroidFunc(string funName, params object[] args) {
        try {
           using (AndroidJavaClass jcla = new AndroidJavaClass("com.Platform")) {
               using (AndroidJavaObject instance = jcla.CallStatic("getInstance")) {
                   instance.Call(funName, args);
               }
           }
        } catch (Exception e) {
           LogException(e);
        }
    }
}

2)ios 方法实现


#import "OpenSetting.h"

@implementation OpenSetting

@end

extern "C" {
    void __OpenSystemTimeSetting()
    {
        NSURL * url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];
        if ( [[UIApplication sharedApplication] canOpenURL: url] ) {
            NSURL*url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:url];
        }
    }

    void __TelPhoneNumber(char* cPhoneNum){
        if (cPhoneNum == nullptr) {
            return;
        }
        NSString *phoneNum = [NSString stringWithUTF8String:cPhoneNum];
        if (phoneNum == nil) {
            return;
        }
        NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", phoneNum];
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
            /// 10及其以上系统
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
        } else {
            /// 10以下系统
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
        }
    }

    bool _IsHadInstalledWeixin(){
        NSURL * url1 = [NSURL URLWithString:@"weixin://"];
        NSURL * url2 = [NSURL URLWithString:@"Whatapp://"];
        if ([[UIApplication sharedApplication] canOpenURL:url1] ||
            [[UIApplication sharedApplication] canOpenURL:url2]) {
            return true;
        }
        return false;
    }
}

至此xcode 打包即可

你可能感兴趣的:(Unity3D,基础知识)