Toad
November 19, 2010, 5:17pm
1
Is there a way to get an iPhone/iPod/iPad’s current battery level and display it inside a game in Unity?
I’ve seen quite a few people on forums say it would be useful to be able to access the battery level and time when playing a game (e.g. in a pause menu).
User340
November 19, 2010, 8:45pm
2
Oh yes, it’s totally possible. You just need an xcode plugin though.
User340
November 19, 2010, 9:24pm
3
Here’s a Unity Package:
Just call BatteryLevel.GetBatteryLevel (). This will return a number ranging from 0.0 to 1.0.
But you have to add the BatteryLevel.mm script into the xcode project though.
433526–15073–$BatteryLevel.zip (2.75 KB)
Toad
November 19, 2010, 9:58pm
4
Thanks! I’ll give that a try.
Toad
November 24, 2010, 6:09pm
5
This works great! Thanks Daniel!!
is there an android method for this
public static float GetBatteryLevel()
{
#if UNITY_IOS
UIDevice device = UIDevice.CurrentDevice();
device.batteryMonitoringEnabled = true; // need to enable this first
Debug.Log("Battery state: " + device.batteryState);
Debug.Log("Battery level: " + device.batteryLevel);
return device.batteryLevel*100;
#elif UNITY_ANDROID
if (Application.platform == RuntimePlatform.Android)
{
try
{
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
if (null != unityPlayer)
{
using (AndroidJavaObject currActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
if (null != currActivity)
{
using (AndroidJavaObject intentFilter = new AndroidJavaObject("android.content.IntentFilter", new object[]{ "android.intent.action.BATTERY_CHANGED" }))
{
using (AndroidJavaObject batteryIntent = currActivity.Call<AndroidJavaObject>("registerReceiver", new object[]{null,intentFilter}))
{
int level = batteryIntent.Call<int>("getIntExtra", new object[]{"level",-1});
int scale = batteryIntent.Call<int>("getIntExtra", new object[]{"scale",-1});
// Error checking that probably isn't needed but I added just in case.
if (level == -1 || scale == -1)
{
return 50f;
}
return ((float)level / (float)scale) * 100.0f;
}
}
}
}
}
}
} catch (System.Exception ex)
{
}
}
return 100;
#endif
}
koeman
April 5, 2016, 9:29am
8
alok_androider:
public static float GetBatteryLevel()
{
#if UNITY_IOS
UIDevice device = UIDevice.CurrentDevice();
device.batteryMonitoringEnabled = true; // need to enable this first
Debug.Log("Battery state: " + device.batteryState);
Debug.Log("Battery level: " + device.batteryLevel);
return device.batteryLevel*100;
#elif UNITY_ANDROID
if (Application.platform == RuntimePlatform.Android)
{
try
{
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
if (null != unityPlayer)
{
using (AndroidJavaObject currActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
if (null != currActivity)
{
using (AndroidJavaObject intentFilter = new AndroidJavaObject("android.content.IntentFilter", new object[]{ "android.intent.action.BATTERY_CHANGED" }))
{
using (AndroidJavaObject batteryIntent = currActivity.Call<AndroidJavaObject>("registerReceiver", new object[]{null,intentFilter}))
{
int level = batteryIntent.Call<int>("getIntExtra", new object[]{"level",-1});
int scale = batteryIntent.Call<int>("getIntExtra", new object[]{"scale",-1});
// Error checking that probably isn't needed but I added just in case.
if (level == -1 || scale == -1)
{
return 50f;
}
return ((float)level / (float)scale) * 100.0f;
}
}
}
}
}
}
} catch (System.Exception ex)
{
}
}
return 100;
#endif
}
can not find uidevice namespace.
1 Like
Mr. Toad? Can i see the UI of the Battery Logo in Unity Game