Skip to content

Commit 1380851

Browse files
committed
Android volume button pluggin
0 parents  commit 1380851

42 files changed

Lines changed: 2948 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# This .gitignore file should be placed at the root of your Unity project directory
2+
#
3+
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
4+
#
5+
/[Ll]ibrary/
6+
/[Tt]emp/
7+
/[Oo]bj/
8+
/[Bb]uild/
9+
/[Bb]uilds/
10+
/[Ll]ogs/
11+
/[Uu]ser[Ss]ettings/
12+
13+
# MemoryCaptures can get excessive in size.
14+
# They also could contain extremely sensitive data
15+
/[Mm]emoryCaptures/
16+
17+
# Asset meta data should only be ignored when the corresponding asset is also ignored
18+
!/[Aa]ssets/**/*.meta
19+
20+
# Uncomment this line if you wish to ignore the asset store tools plugin
21+
# /[Aa]ssets/AssetStoreTools*
22+
23+
# Autogenerated Jetbrains Rider plugin
24+
/[Aa]ssets/Plugins/Editor/JetBrains*
25+
26+
# Visual Studio cache directory
27+
.vs/
28+
29+
# Gradle cache directory
30+
.gradle/
31+
32+
# Autogenerated VS/MD/Consulo solution and project files
33+
ExportedObj/
34+
.consulo/
35+
*.csproj
36+
*.unityproj
37+
*.sln
38+
*.suo
39+
*.tmp
40+
*.user
41+
*.userprefs
42+
*.pidb
43+
*.booproj
44+
*.svd
45+
*.pdb
46+
*.mdb
47+
*.opendb
48+
*.VC.db
49+
50+
# Unity3D generated meta files
51+
*.pidb.meta
52+
*.pdb.meta
53+
*.mdb.meta
54+
55+
# Unity3D generated file on crash reports
56+
sysinfo.txt
57+
58+
# Builds
59+
*.apk
60+
*.unitypackage
61+
62+
# Crashlytics generated file
63+
crashlytics-build.properties
64+
65+
# Packed Addressables
66+
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*
67+
68+
# Temporary auto-generated Android Assets
69+
/[Aa]ssets/[Ss]treamingAssets/aa.meta
70+
/[Aa]ssets/[Ss]treamingAssets/aa/*

Assets/Plugins.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/Android.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.camobap.unity3d">
3+
<application android:icon="@drawable/app_icon" android:label="@string/app_name">
4+
<activity android:name="org.camobap.unity3d.VolumeButtonsActivity"
5+
android:label="@string/app_name"
6+
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
7+
<intent-filter>
8+
<action android:name="android.intent.action.MAIN" />
9+
<category android:name="android.intent.category.LAUNCHER" />
10+
</intent-filter>
11+
</activity>
12+
</application>
13+
</manifest>

Assets/Plugins/Android/AndroidManifest.xml.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/VolumeButtonsPlugin.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/VolumeButtonsPlugin/Android.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.camobap.unity3d;
2+
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
6+
import android.os.Bundle;
7+
import android.util.Log;
8+
import android.view.KeyEvent;
9+
10+
import com.unity3d.player.UnityPlayer;
11+
import com.unity3d.player.UnityPlayerActivity;
12+
13+
public class VolumeButtonsActivity extends UnityPlayerActivity {
14+
15+
/**
16+
* Keep in sync with VolumeButton.Event in C#
17+
*/
18+
private enum Event {
19+
VOLUME_DOWN(-1),
20+
VOLUME_MUTE(0),
21+
VOLUME_UP(+1);
22+
23+
private final String mValue;
24+
private Event(int type) {
25+
mValue = Integer.toString(type);
26+
}
27+
28+
public String getValue() {
29+
return mValue;
30+
}
31+
}
32+
33+
private static final String MESSAGE_NAME = "_OnVolumeButtonEvent";
34+
private List<String> gameObjectNames = new ArrayList<>();
35+
36+
protected void onCreate(Bundle savedInstanceState) {
37+
super.onCreate(savedInstanceState);
38+
// print debug message to logcat
39+
Log.d("VolumeButtonsActivity", "onCreate called!");
40+
}
41+
42+
public void addGameObjectListener(String gameObjectName)
43+
{
44+
synchronized (gameObjectNames)
45+
{
46+
gameObjectNames.add(gameObjectName);
47+
}
48+
}
49+
50+
public void removeGameObjectListener(String gameObjectName)
51+
{
52+
synchronized (gameObjectNames)
53+
{
54+
gameObjectNames.remove(gameObjectName);
55+
}
56+
}
57+
58+
@Override
59+
public boolean dispatchKeyEvent(KeyEvent event) {
60+
int action = event.getAction();
61+
int keyCode = event.getKeyCode();
62+
switch (keyCode) {
63+
case KeyEvent.KEYCODE_VOLUME_UP:
64+
if (action == KeyEvent.ACTION_DOWN) {
65+
for (String gameObjectName : gameObjectNames) {
66+
UnityPlayer.UnitySendMessage(gameObjectName, MESSAGE_NAME, Event.VOLUME_UP.getValue());
67+
}
68+
}
69+
break;
70+
case KeyEvent.KEYCODE_VOLUME_DOWN:
71+
if (action == KeyEvent.ACTION_DOWN) {
72+
for (String gameObjectName : gameObjectNames) {
73+
UnityPlayer.UnitySendMessage(gameObjectName, MESSAGE_NAME, Event.VOLUME_DOWN.getValue());
74+
}
75+
}
76+
break;
77+
}
78+
79+
return super.dispatchKeyEvent(event);
80+
}
81+
}

Assets/Plugins/VolumeButtonsPlugin/Android/VolumeButtonsActivity.java.meta

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
using UnityEngine;
6+
7+
public class VolumeButtons : MonoBehaviour
8+
{
9+
#if !UNITY_ANDROID && !UNITY_IOS
10+
private readonly RuntimePlatform[] SupportedPlatforms = new RuntimePlatform[] { RuntimePlatform.Android, RuntimePlatform.IPhonePlayer};
11+
private readonly string UnsupportedError = "Unsupported platform '{0}'! At the moment we support only {1}";
12+
#endif
13+
14+
public VolumeButtonsEvent OnVolumeButtonEvent;
15+
16+
void Start()
17+
{
18+
#if UNITY_ANDROID
19+
_CallActivity("addGameObjectListener", gameObject.name);
20+
#elif UNITY_IOS
21+
// FIXME
22+
#else
23+
Debug.LogFormat(UnsupportedError, Application.platform, string.Join("", SupportedPlatforms));
24+
#endif
25+
}
26+
27+
void OnDisable()
28+
{
29+
#if UNITY_ANDROID
30+
_CallActivity("removeGameObjectListener", gameObject.name);
31+
#elif UNITY_IOS
32+
// FIXME
33+
#else
34+
Debug.LogFormat(UnsupportedError, Application.platform, string.Join("", SupportedPlatforms));
35+
#endif
36+
}
37+
38+
private void _OnVolumeButtonEvent(string value)
39+
{
40+
if (OnVolumeButtonEvent != null)
41+
{
42+
this.OnVolumeButtonEvent.Invoke((VolumeButtonsEventType)Int32.Parse(value));
43+
}
44+
else
45+
{
46+
Debug.Log("No event subscribed!");
47+
}
48+
}
49+
50+
private void _CallActivity(string methodName, string arg0)
51+
{
52+
AndroidJNIHelper.debug = true;
53+
using (AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
54+
using (AndroidJavaObject activity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity")) {
55+
Debug.LogFormat("Call {0}({1})", methodName, arg0);
56+
activity.Call(methodName, arg0);
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)