Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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

https://docs.unity3d.com/ScriptReference/SystemInfo.html

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by aesparza · Aug 22, 2017 at 09:01 AM

Hi @hollym16 Does anybody know how to do this on a Windows device?

Thanks!!!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Access photos, videos & music from iOS and Android library? 0 Answers

Can Google Analytics for Unity collect data on Demographics? 1 Answer

iOS Unity Plugin : Load Video from 'Videos'app and Photo Library on iOS Device 0 Answers

How to get location from ios plugin in unity? 0 Answers

Creating a native photo picker screen on iOS 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges