@@ -14,10 +14,26 @@ public class VolumeButtons : MonoBehaviour
1414
1515 public VolumeButtonsEvent OnVolumeButtonEvent ;
1616
17+ //[Range(0.0f, 1.0f)] // -1.0f in case of error or not supported
18+ public float SystemVolumeLevel
19+ {
20+ get
21+ {
22+ #if UNITY_ANDROID
23+ return _CallActivityFloat ( "getSystemVolumeLevel" ) ;
24+ #elif UNITY_IOS
25+ return _GetSystemVolumeLevel ( ) ;
26+ #else
27+ Debug . LogFormat ( UnsupportedError , Application . platform , string . Join ( "" , SupportedPlatforms ) ) ;
28+ return - 1.0f ;
29+ #endif
30+ }
31+ }
32+
1733 void Start ( )
1834 {
1935 #if UNITY_ANDROID
20- _CallActivity ( "addGameObjectListener" , gameObject . name ) ;
36+ _CallActivityVoid ( "addGameObjectListener" , gameObject . name ) ;
2137 #elif UNITY_IOS
2238 _AddGameObjectListener ( gameObject . name , gameObject . name . Length ) ;
2339 #else
@@ -28,7 +44,7 @@ void Start()
2844 void OnDisable ( )
2945 {
3046 #if UNITY_ANDROID
31- _CallActivity ( "removeGameObjectListener" , gameObject . name ) ;
47+ _CallActivityVoid ( "removeGameObjectListener" , gameObject . name ) ;
3248 #elif UNITY_IOS
3349 _RemoveGameObjectListener ( gameObject . name , gameObject . name . Length ) ;
3450 #else
@@ -40,7 +56,10 @@ private void _OnVolumeButtonEvent(string value)
4056 {
4157 if ( OnVolumeButtonEvent != null )
4258 {
43- this . OnVolumeButtonEvent . Invoke ( ( VolumeButtonsEventType ) Int32 . Parse ( value ) ) ;
59+ #if DEBUG
60+ Debug . LogFormat ( "Volume event {0} delivered!" , value ) ;
61+ #endif
62+ this . OnVolumeButtonEvent . Invoke ( ( VolumeButtonsEventType ) Int32 . Parse ( value ) , this ) ;
4463 }
4564 else
4665 {
@@ -50,25 +69,42 @@ private void _OnVolumeButtonEvent(string value)
5069
5170#if UNITY_ANDROID
5271
53- private void _CallActivity ( string methodName , string arg0 )
72+ private float _CallActivityFloat ( string methodName )
5473 {
55- AndroidJNIHelper . debug = true ;
5674 using ( AndroidJavaClass unityPlayerClass = new AndroidJavaClass ( "com.unity3d.player.UnityPlayer" ) )
5775 {
5876 using ( AndroidJavaObject activity = unityPlayerClass . GetStatic < AndroidJavaObject > ( "currentActivity" ) )
5977 {
60- Debug . LogFormat ( "Call {0}({1})" , methodName , arg0 ) ;
61- activity . Call ( methodName , arg0 ) ;
78+ #if DEBUG
79+ Debug . LogFormat ( "Call {0}()" , methodName ) ;
80+ #endif
81+ return activity . Call < float > ( methodName ) ;
6282 }
6383 }
6484 }
6585
86+ private void _CallActivityVoid ( string methodName , string args0 )
87+ {
88+ using ( AndroidJavaClass unityPlayerClass = new AndroidJavaClass ( "com.unity3d.player.UnityPlayer" ) )
89+ {
90+ using ( AndroidJavaObject activity = unityPlayerClass . GetStatic < AndroidJavaObject > ( "currentActivity" ) )
91+ {
92+ #if DEBUG
93+ Debug . LogFormat ( "Call {0}({1})" , methodName , args0 ) ;
94+ #endif
95+ activity . Call ( methodName , args0 ) ;
96+ }
97+ }
98+ }
99+
66100#elif UNITY_IOS
67101
68102 [ DllImport ( "__Internal" , EntryPoint = "VBP_addGameObjectListener" ) ]
69103 private static extern void _AddGameObjectListener ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string gameObjectName , int gameObjectNameLen ) ;
70104 [ DllImport ( "__Internal" , EntryPoint = "VBP_removeGameObjectListener" ) ]
71105 private static extern void _RemoveGameObjectListener ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string gameObjectName , int gameObjectNameLen ) ;
106+ [ DllImport ( "__Internal" , EntryPoint = "VBP_getSystemVolumeLevel" ) ]
107+ private static extern float _GetSystemVolumeLevel ( ) ;
72108
73109#endif
74110}
0 commit comments