- Home /
 
               Question by 
               hollym16 · Jun 26, 2017 at 11:44 AM · 
                scripting problemiosplugindevicesysteminfo  
              
 
              SystemInfo.batteryStatus integration
I want to put in some scripting into my Project that will tell me if the device is charging or not.
I've found some documentation on it but can't for the life of me work out how to integrate it.
Can anyone show my how to format the script?
https://docs.unity3d.com/560/Documentation/ScriptReference/SystemInfo-batteryStatus.html
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by hollym16 · Jun 28, 2017 at 11:27 AM
I found this tutorial to be just what I was looking for:
http://mizutanikirin.net/unity-ios-native-code-pluginsでバッテリ取得
This used UIDevice rather than SystemInfo but only took 2 scripts. The first is an .mm file that needs to go in Assets > Plugins > iOS
 extern "C" {
     float BatteryLevelNative() {
         
         [UIDevice currentDevice].batteryMonitoringEnabled = YES;
         float batteryLevel = [UIDevice currentDevice].batteryLevel;
         
         return batteryLevel;
     }
     
     int BatteryStateNative() {
         UIDeviceBatteryState batteryState = [UIDevice currentDevice].batteryState;
         [UIDevice currentDevice].batteryMonitoringEnabled = YES;
         int state = -1;
         
         // バッテリーの充電状態を取得する
         switch ([UIDevice currentDevice].batteryState)
         {
             case UIDeviceBatteryStateFull:
                 // Full
                 state = 2;
                 break;
                 
             case UIDeviceBatteryStateCharging:
                 // Charging
                 state = 1;
                 break;
                 
             case UIDeviceBatteryStateUnplugged:
                 // Unplugged
                 state = 0;
                 break;
                 
             case UIDeviceBatteryStateUnknown:
                 // Unknown
                 state = -1;
                 break;
                 
             default:
                 break;
         }
         return state;
     }
 }
The second goes on a GameObject in the Hierarchy of your scene:
 using UnityEngine;
 using System.Collections;
 using System.Runtime.InteropServices;
 using UnityEngine.UI;
  
 public class BatteryScript : MonoBehaviour {
  
     [DllImport("__Internal")]
     private static extern float BatteryLevelNative();
  
     [DllImport("__Internal")]
     private static extern int BatteryStateNative();
  
     void Start()
     {
         float batteryLevel = BatteryLevelNative();
         int batteryState = BatteryStateNative ();
         Debug.Log ("level: " + batteryLevel + "  state:" + batteryState);
     }
 }
I hope this helps anyone else who's struggling with this
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                