Unity3d 获取电量电池状态

发表于2018-10-26
评论0 265浏览

想免费获取内部独家PPT资料库?观看行业大牛直播?点击加入腾讯游戏学院游戏程序行业精英群

711501594
获取手机电量、电池信息功能,相信大多数人首先想到的是android或IOS获取后传到unity里面这样来做,但这篇要给大家分享另外一个思路,直接在unity里面获取电量与电池状态信息,这样大家也方便了大家。

获取电量

C#
    public static int GetBatteryLevel()
    {
        int level = 0;
#if UNITY_ANDROID
        try
        {
            AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            AndroidJavaClass unityPluginLoader = new AndroidJavaClass("java类全名");
            level = unityPluginLoader.CallStatic<int>("GetBatteryLevel", currentActivity);
        }
        catch (System.Exception e)
        {
        }
#elif UNITY_IOS
        level = (int)_IOS_GetBatteryLevel();
#endif
        return level;
    }

java
    public static int GetBatteryLevel(Activity currentActivity) {
        int level = 0;
        try {
            IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            Intent intent = currentActivity.registerReceiver(null, ifilter);
            level = intent.getIntExtra("level", 0);// 获得当前电量
            // int scale = intent.getIntExtra("scale", 0);//获得总电量
            // int status = intent.getIntExtra("status", 0);//电池充电状态
            // int health = intent.getIntExtra("health", 0);//电池健康状况
            // int batteryV = intent.getIntExtra("voltage", 0); //电池电压(mv)
            // int temperature = intent.getIntExtra("temperature", 0);
            // //电池温度(数值)
        } catch (Exception e) {
            }
        }
        return level;
    }

ios
    float _IOS_GetBatteryLevel()
    {
        UIDevice *myDevice = [UIDevice currentDevice];
        [myDevice setBatteryMonitoringEnabled:YES];
        float level = [myDevice batteryLevel] * 100;
        return level;
    }

获取电池状态

C#
  public static bool GetBatteryChargingState()
    {
        bool isCharging = false;
#if UNITY_ANDROID
        try
        {
            AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            AndroidJavaClass unityPluginLoader = new AndroidJavaClass("java类全名");
            isCharging = unityPluginLoader.CallStatic<bool>("GetBatteryChargingState", currentActivity);
        }
        catch (System.Exception e)
        {
        }
#elif UNITY_IOS
        isCharging = (bool)_IOS_GetBatteryChargingState();
#endif
        return isCharging;
    }

java
    public static boolean GetBatteryChargingState(Activity currentActivity) {
        boolean isCharging = false;
        try {
            IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            Intent intent = currentActivity.registerReceiver(null, ifilter);
            int status = intent.getIntExtra("status", 0);// 电池充电状态
            if (status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL) {
                isCharging = true;
            }
        } catch (Exception e) {
            }
        }
        return isCharging;
    }

IOS
    bool _IOS_GetBatteryChargingState()
    {
        UIDevice *myDevice = [UIDevice currentDevice];
        [myDevice setBatteryMonitoringEnabled:YES];
        bool isCharging = false;
        UIDeviceBatteryState state = [myDevice batteryState];
        if(state == UIDeviceBatteryStateCharging)
        {
            isCharging = true;
        }
        return isCharging;
    }

原文链接

著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

游戏学院公众号二维码
腾讯游戏学院
微信公众号

提供更专业的游戏知识学习平台