zoukankan      html  css  js  c++  java
  • unity中获取手机电量

    注意:只能在真机中显示,编辑器没法显示

    a.Android版
      1.通过C#直接读取,下面的GetBatteryLevel()方法
    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;
    }
    }
      b.iOS版
      iOS需要用到xcode编写.a静态链接库
      1.在xcode编写.mm文件,实现C++调用iOS的API得到手机电量,部分代码如下:
      
      float getiOSBatteryLevel()
        {
          [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
          return [[UIDevice currentDevice] batteryLevel];
        }
      
      2.将上面.mm编译好的.a文件放入Unity工程Assets-Plugins-iOS下(路径不能错);在C#中使用下面的方法调用:
    [ DllImport( "__Internal" )]
    private static extern float getiOSBatteryLevel();

    在C#调用此函数就可获得iOS电量

  • 相关阅读:
    oracle 误删除表的几种恢复方法
    解决js在alert或者断点调试时才能赋值
    常用的Debug方式
    字节对齐
    CWnd::SetWindowPos的注意事项
    网络模块代码调试要点
    stub和mock
    全局变量的缺陷
    SVN切换地址
    C/C++如何得到int型最大值
  • 原文地址:https://www.cnblogs.com/FingerCaster/p/7742815.html
Copyright © 2011-2022 走看看