unity 讀取手機電量
阿新 • • 發佈:2019-02-10
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System; using UnityEngine.iOS; public enum NetworkWorkSituation { NotReachable, ReachableViaCarrierDataNetwork, ReachableViaLoaclAreaNetwork, } public class MobilePhone : MonoBehaviour { public static NetworkWorkSituation type; public static NetworkWorkSituation GetNetworkState() { //斷網 if (Application.internetReachability == NetworkReachability.NotReachable) { type = NetworkWorkSituation.NotReachable; } //手機網路 else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork) { type = NetworkWorkSituation.ReachableViaCarrierDataNetwork; } //wifi else if(Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork) { type = NetworkWorkSituation.ReachableViaLoaclAreaNetwork; } return type; } #if UNITY_ANDROID public static int GetBatteryLevel() { try { string capacityString = System.IO.File.ReadAllText("/sys/class/power_supply/battery/capacity"); return int.Parse(capacityString); } catch(Exception e) { Debug.Log("Failed to read battery power:" + e.Message); } return -1; } #endif public static float GetMobilePowerPersent() { #if UNITY_IPHONE return GetiOSBatteryLevel(); #elif UNITY_ANDROID return GetBatteryLevel() #else return 0; #endif } }