ThaHypnotoad ThaHypnotoad - 20 days ago 5
Android Question

Using volume buttons on Android in Unity3d

I am attempting to use the volume buttons in a unity3d game on Android. Unfortunately I can't see anything pertaining to volume being mapped as a KeyCode, so it seems to me that this functionality is absent from Unity3d. Is there a method of doing this that is more hidden? Is there possibly a way to get around this? The application of this is to simply send a UDP packet upon the press of the volume button so it needs not interact with the Unity3d application itself. Possibly a background process that can deal with it? I simply want advice on the path to follow as I don't want to waste time barking up the wrong tree for my solution. Thanks in advance!

Answer

You can build your app as an android studio/eclipse project and modify the UnityPlayerActivity (see: Extending the UnityPlayerActivity Java Code)

Then you can use the volume keys as described in this SO answer: Android - Volume Buttons used in my application

An example of comunication from activity to unity3d context:

UnityPlayer.UnitySendMessage("TheNameOfYourGameObject", "PublicMethodInThatGameObject", "StringArgument");

Note that there have been rumors of Samsung (not Google) not accepting an app to their stores if the button behaviour has changed.

Also, it would be good UX, if the user will still be able to change the volume of your app.

Comments