使用JAR插件从Unity3D调用android方法

时间:2014-10-04 20:01:13

标签: c# android eclipse unity3d social-networking

我正在开发一款仅适用于Android平台的Unity游戏,我要求通过意图分享我的游戏内容,所以我根据以下代码的几个教程实现了一个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设备时没有任何反应,所以我做错了什么?还有其他建议吗?

非常感谢

1 个答案:

答案 0 :(得分:1)

因为您在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>