Har*_*oux 9 c# android unity-game-engine android-intent android-implicit-intent
我正在使用自定义隐式意图从另一个Android应用程序启动Unity应用程序.这工作正常,但我无法弄清楚如何读取Unity中的意图额外数据?
ANDROID打算推出UNITY APP
i=new Intent();
i.setAction("com.company.unityapp.MyMethod");
i.putExtra("KEY","This is the message string");
startActivity(i);
Run Code Online (Sandbox Code Playgroud)
UNITY APP AndroidManifest.xml
<intent-filter>
<action android:name="com.company.unityapp.MyMethod" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
我的场景中有一个附带脚本的GameObject.在start方法中,我有这个代码来尝试读取与intent一起传递的额外数据
AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");
bool hasExtra = intent.Call<bool> ("hasExtra", "arguments");
if (hasExtra) {
AndroidJavaObject extras = intent.Call<AndroidJavaObject> ("getExtras");
arguments = extras.Call<string> ("getString", "arguments");
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,参数总是空的.任何帮助,将不胜感激.
Har*_*oux 11
我花了很长时间来弄明白这一点.在线发现的所有解决方案只是部分完成.下面是使用自定义隐式从另一个Android应用程序启动Unity应用程序的完整解决方案,Intent以及如何访问Intent内部Unity 发送的额外数据.
要实现此目的,您需要创建一个Android插件,Unity将使用该插件来访问Intent额外数据.
ANDROID PLUGIN:
您需要将classes.jar从Unity安装文件夹复制到android插件文件夹/lib/classes.jar
public class MainActivity extends UnityPlayerActivity {
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleNewIntent(intent);
}
private void handleNewIntent(Intent intent){
String text = intent.getStringExtra("KEY");
UnityPlayer.UnitySendMessage("AccessManager","OnAccessToken", text);
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中
这里重要的是使用的包名:com.company.plugin
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.plugin">
<application
android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
android:supportsRtl="true" android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
Gradle构建文件:
将以下内容添加到app gradle构建文件中,以便能够创建与Unity一起使用的.jar
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
}
...
...
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile files('libs/classes.jar')
}
//task to delete the old jar
task deleteOldJar(type: Delete) {
delete 'release/AndroidPlugin.jar'
}
//task to export contents as jar
task exportJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
///Rename the jar
rename('classes.jar', 'AndroidPlugin.jar')
}
exportJar.dependsOn(deleteOldJar, build)
Run Code Online (Sandbox Code Playgroud)
将创建的AndroidPlugin.jar复制到Unity Assets/Plugins/Android
UNITY APP:
将包标识符PlayerSettings设置为与Android插件中设置的相同 -com.company.plugin
AndroidManifest.xml在Assets/Plugins/Android中创建自定义文件
这里重要的是使用package插件中使用的相同名称.另请注意Intent名称:com.company.plugin.do
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.plugin"
android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<application android:label="@string/app_name">
<activity android:name=".MainActivity" android:label="@string/app_name"
android:launchMode="singleTask" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="sensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.company.plugin.do" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
创建一个名为AccessManager的统一脚本,并将脚本附加到场景中的游戏对象.OnAccessToken是接收从android插件发送的消息的方法,将包含从intent发送的额外数据.
public class accessManager : MonoBehaviour {
public void OnAccessToken(string accessToken)
{
Debug.Log("Message Received!!!! :" + accessToken);
}
}
Run Code Online (Sandbox Code Playgroud)
ANDROID APP:
创建一个标准的Android应用程序,它将启动Unity应用程序并发送Intent额外的数据
public void LaunchUnityApp(){
Intent i=new Intent();
i.setAction("com.company.plugin.do");
i.setType("text/plain");
i.putExtra("KEY","This is the text message sent from Android");
startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)