質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

2回答

5046閲覧

【Unity】バックグラウンド時にandroidプラグインからUnityの関数を呼び出す【モバイル】

tanamochi

総合スコア83

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2017/03/29 06:05

いつもお世話になっております。
毎度恐縮ですが質問させていただきます。

###前提・実現したいこと
タイトルの通りバックグラウンド時にプラグインからUnityの関数を呼び出したいです。
バックグラウンドというのはアプリを立ち上げてはいるがフォーカスされていない状態(ホーム画面にいる等)

###該当のソースコード

Java

1// Unity連携部分 2public class NativeDialog { 3 private static AudioAttributes audioAttributes; 4 private static SoundPool soundPool; 5 private static int soundOne; 6 7 static public void init(Context i_context, Class i_class) { 8 audioAttributes = new AudioAttributes.Builder() 9 .setUsage(AudioAttributes.USAGE_GAME) 10 .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) 11 .build(); 12 13 soundPool = new SoundPool.Builder() 14 .setAudioAttributes(audioAttributes) 15 .setMaxStreams(1) 16 .build(); 17 18 soundOne = soundPool.load(i_context, R.raw.one, 1); 19 } 20 21 static public void startService() { 22 Log.i("Unity", "START Service"); 23 Activity activity = UnityPlayer.currentActivity; 24 Context context = activity.getApplicationContext(); 25 activity.startService(new Intent(context, testService.class)); 26 } 27 28 static public void stopService() { 29 Log.i("Unity", "STOP Servise"); 30 testService.shouldContinue = false; 31 } 32 33 static public void callUnity() { 34 Log.i("Unity", "callUnity"); 35 // 音を鳴らしてみる 36 soundPool.play(soundOne, 1.0f, 1.0f, 0, 0, 1); 37 UnityPlayer.UnitySendMessage("Main Camera", "sendNotification", ""); 38 } 39 40 static public void sendNotification(){ 41 Log.i("Unity", "START sendNotification"); 42 43 // Intent の作成 44 Activity activity = UnityPlayer.currentActivity; 45 Intent intent = new Intent(activity.getApplicationContext(), _class); 46 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 47 48 Notification n = new Notification.Builder(context) 49 .setContentTitle("title") 50 .setContentText("msg") 51 .setSmallIcon(R.drawable.test) 52 .setContentIntent(pendingIntent) 53 .setAutoCancel(true) 54 .build(); 55 56 NotificationManager mNotifyMgr = 57 (NotificationManager) activity.getSystemService(NOTIFICATION_SERVICE); 58 59 int mNotificationId = 001; 60 // Builds the notification and issues it. 61 mNotifyMgr.notify(mNotificationId, n); 62 63 Log.i("Unity", "END sendNotification"); 64 } 65}
// Service public class testService extends IntentService { public static volatile boolean shouldContinue = true; public testService (String name) { super(name); } public testService () { super("testService"); } @Override protected void onHandleIntent(Intent intent) { shouldContinue = true; Log.d("NotifyIntentService","onHandleIntent Start"); long endTime = System.currentTimeMillis() + 6*1000; while (System.currentTimeMillis() < endTime) { synchronized (this) { try { wait(endTime - System.currentTimeMillis()); if (!shouldContinue) { stopSelf(); return; } NativeDialog.callUnity(); } catch (Exception e) { } } } } }

C#

1// Unity部分 2public class StartScript : MonoBehaviour 3{ 4 static AndroidJavaClass _plugin = null; 5 6 // Use this for initialization 7 void Start () 8 { 9 Init(); 10#if UNITY_ANDROID && !UNITY_EDITOR 11 _plugin = new AndroidJavaClass("com.example.dialog_plugin.NativeDialog"); 12#endif 13 } 14 15 void Init() 16 { 17#if UNITY_ANDROID && !UNITY_EDITOR 18 AndroidJavaClass nativeDialog = new AndroidJavaClass("com.example.dialog_plugin.NativeDialog"); 19 20 AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 21 AndroidJavaObject context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); 22 23 context.Call("runOnUiThread", new AndroidJavaRunnable(() => { 24 nativeDialog.CallStatic( 25 "init", 26 context, 27 unityPlayer 28 ); 29 })); 30#endif 31 } 32 33 void sendNotification() 34 { 35 Debug.Log("unity sendNotification"); 36#if UNITY_ANDROID && !UNITY_EDITOR 37 _plugin.CallStatic ("sendNotification"); 38#endif 39 } 40 41 void OnApplicationPause(bool pauseStatus) 42 { 43 if (pauseStatus) 44 { 45#if UNITY_ANDROID && !UNITY_EDITOR 46 _plugin.CallStatic ("startService"); 47#endif 48 } 49 else 50 { 51#if UNITY_ANDROID && !UNITY_EDITOR 52 _plugin.CallStatic("stopService"); 53#endif 54 } 55 } 56}

###試したこと
UnityからAndroidプラグインのserviceを立ち上げ一定時間後にローカル通知を行う関数を呼び出す処理を記載。
現在はバックグラウンド時に音声再生とログは確認できているが肝心の処理は行われず、フォアグラウンドになったときにはじめて処理が行われているのを確認。

###補足情報(言語/FW/ツール等のバージョンなど)
AndroidStudio2.2.3
Unity5.5.0f3

###質問したいこと
呼び出しの方法は回りくどいですが実現したいのはタイトルと最初に書いたとおりのことです。
質問したいことはMonoBehaviourはバックグラウンド時に処理が止まってしまうのかタイトルのようなことは実現可能かということです。

何卒宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

音声ファイルなどは、APKのassetsに格納されるようなので
AssetManagerからファイルを直接参照できるかもしれないです。

参照出来たら、サービス上でSoundPoolやらMediaPlayerやらで再生してしまうのはどうでしょう。

投稿2017/03/30 02:55

編集2017/03/30 03:12
abs123

総合スコア1280

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

abs123

2017/03/30 03:04

ん?そもそもUnityが寝てるから意味ないってことか
abs123

2017/03/30 03:12

がっつり修正
tanamochi

2017/03/31 10:20

回答ありがとうございます。 Unityが起こせるかという質問でした。 うまく伝えきれず申し訳ないです。
guest

0

ベストアンサー

Unityの仕組み的にできないと思います。

Application.runInBackgroundというオプションがありますが、Andoidでは使えません。

https://forum.unity3d.com/threads/application-runinbackground-is-not-working-on-android.117723/

投稿2017/03/30 02:05

fumobox

総合スコア15

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tanamochi

2017/03/30 02:18

回答ありがとうございます。 やはりそうでしたか。。 Application.runInBackgroundはPCのみなのでおそらくiOSも同じですよね。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問