[英]Calling an android method from Unity3D with a JAR plugin
我正在使用Unity开发的游戏仅适用于Android平台,并且需要通过意图共享我的游戏内容,因此我根据一些使用以下代码的教程实现了JAR插件:
package a.b.c;
import android.content.Intent;
import android.os.Bundle;
import com.unity3d.player.UnityPlayerActivity;
public class UnityBridge extends UnityPlayerActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void callShareIntent() {
Intent shareIntent = new Intent (Intent.ACTION_VIEW);
startActivity(shareIntent);
}
}
JAR位于Assets / Plugins / Android中,带有一个AndroidManifest文件,该文件将覆盖由unity创建的文件,这是其代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest android:theme="@*android:style/Theme.NoTitleBar" android:versionCode="1" android:versionName="1.0" android:installLocation="auto" package="a.b.c"
xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" />
<application android:label="@string/app_name" android:icon="@drawable/app_icon" android:debuggable="false">
<activity android:label="@string/app_name" android:name="com.unity3d.player.UnityPlayerNativeActivity" android:launchMode="singleTask" android:screenOrientation="portrait" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|fontScale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
<activity android:name=".UnityBridge"></activity>
</application>
<uses-feature android:glEsVersion="0x20000" />
</manifest>
我只是复制了Unity创建的清单,并添加了以下行:
<activity android:name=".UnityBridge"></activity>
... 如你看到的。
另一方面,我创建了一个C#文件以启动插件的share方法,这是其代码:
using UnityEngine;
using System.Collections;
public class BotonCompartir : MonoBehaviour {
AndroidJavaClass androidClass;
// Use this for initialization
void Start () {
if (Application.platform == RuntimePlatform.Android) {
AndroidJNI.AttachCurrentThread ();
androidClass = new AndroidJavaClass ("a.b.c.UnityBridge");
}
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0) && Application.platform == RuntimePlatform.Android) {
androidClass.Call("callShareIntent");
}
}
}
...并且我已将其附加到游戏对象上。
当我将apk部署到android设备时,什么都没发生,那我在做什么错呢? 还有其他建议吗?
非常感谢你
因为您已在UnityBridge中扩展了UnityPlayerActivity,所以必须将其设置为AndroidManifest.xml文件中的主要活动。
<activity
android:name=".UnityBridge"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.