Unity—获取安卓设备状态(电量、时间、网络)

  • Post author:
  • Post category:其他


    /// <summary>
    /// 获取时间
    /// </summary>
    private void GetTime()
    {
        txt_TimeVal.text = System.DateTime.Now.ToString("hh:mm tt", System.Globalization.CultureInfo.CreateSpecificCulture("en-us")); //示例输出: "8:30 AM"
    }

    /// <summary>
    /// 获取电量
    /// </summary>
    private void GetBattaryVal()
    {
        var level = SystemInfo.batteryLevel == -1 ? 1 : SystemInfo.batteryLevel;
        img_BattaryVal.fillAmount = level;
    }

    /// <summary>
    /// 获取网络状态
    /// </summary>
    private void GetNetworkInfo()
    {
        switch (Application.internetReachability)
        {
            case NetworkReachability.NotReachable: //断网
                img_IntOff.gameObject.SetActive(true);
                GameLogger.LogYellow("无网络");
                break;
            default:
                img_IntOff.gameObject.SetActive(false);
                break;
        }
    }



版权声明:本文为Erenlui原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。