- Home /
C# Script from Native Android Service
I have a native Android service running in the background that needs to interface with a Unity3D script. I'm aware of the following method:
com.unity3d.player.UnityPlayer.UnitySendMessage(String gameObject, String arg1, String arg2)
But it requires a gameObject parameter. Since the service is running in the background, I can't be 100% sure that a gameObject attached to a particular scene has not yet been destroyed. Is this a fair assumption (since the user might not even be in the app)?
If so, is there a way to instantiate a gameObject that is never destroyed? Is there any other way to call a unity script from native Java code?
Answer by whydoidoit · Mar 04, 2014 at 03:10 AM
You need to create an object in your initial scene and use DontDestroyOnLoad(gameObject) on it.
Is there any other way, so that it will still work if Android kills the game process (my service is running in a separate process)?
How do Unity apps trigger special behavior when push notifications occur? I assume doing something similar would work
You can register an intent for your game which responds by starting it if it isn't already running - is that what you need?
An intent would allow a url scheme to launch the game or some other action.
Sounds like it. An intent coupled with your previous solution. How do you register/use an intent for the game? Thanks!
Here's my core activity which registers intents and extends UnityPlayerActivity:
package com.threeradical.urlscheme;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.content.Intent;
import android.content.pm.Package$$anonymous$$anager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerNativeActivity;
public class UrlSchemeNativePlayerActivity extends UnityPlayerNativeActivity {
public class Execute$$anonymous$$ethod extends TimerTask
{
public String uri;
@Override
public void run() {
// TODO Auto-generated method stub
Log.e("URLScheme","Running Uri::" + uri);
UnityPlayer.UnitySend$$anonymous$$essage("!URLSchemeHandler", "OnOpenWithUrl", uri);
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
UrlResolver.currentContext = getApplicationContext();
Intent intent = getIntent();
if(intent != null && intent.getAction() == Intent.ACTION_VIEW)
{
Uri uri = intent.getData();
if(uri != null)
{
Timer t = new Timer();
Execute$$anonymous$$ethod mtd = new Execute$$anonymous$$ethod();
mtd.uri = uri.toString();
t.schedule(mtd, 1500);
}
}
}
@Override
protected void onNewIntent(Intent intent)
{
Log.e("URLScheme", "onNewIntent");
super.onNewIntent(intent);
HandleURLIntent(intent);
}
protected boolean HandleURLIntent(Intent intent)
{
if(intent == null)
return false;
Uri uri = intent.getData();
if(uri == null)
return false;
UnityPlayer.UnitySend$$anonymous$$essage("!URLSchemeHandler", "OnOpenWithUrl", uri.toString());
return true;
}
}
It reacts to a URL scheme opening the app or focussing it if it is already open.
Your answer
Follow this Question
Related Questions
UnitySendMessage on Android 1 Answer
App in background still receive UnityPlayer.UnitySendMessage from Android plugin, normal ? 0 Answers
Is there any way to create a transparent view on Android? 0 Answers
Android SendMessage 1 Answer
Old camera view flickers for a couple of seconds when camera is changed 0 Answers