0评论

Unity3d 获取电量电池状态

文章来自CSDN博客 2018-10-26 73浏览

想免费获取内部独家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;
    }