unity 读取手机电量

来源:互联网 发布:电脑卸载软件推荐 编辑:程序博客网 时间:2024/05/16 02:03

  



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    }}


原创粉丝点击