【问题标题】:Calling an android method from Unity3D with a JAR plugin使用 JAR 插件从 Unity3D 调用 android 方法
【发布时间】:2014-10-04 20:01:12
【问题描述】:

我正在使用 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 文件,该文件覆盖了统一创建的文件,这是它的代码:

<?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# 文件来启动插件的共享方法,这是它的代码:

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 设备时没有任何反应,那么我做错了什么?还有什么建议吗?

非常感谢

【问题讨论】:

    标签: c# android eclipse unity3d social-networking


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-12
      • 2016-02-22
      • 2013-05-10
      • 2019-09-10
      • 2016-01-23
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      相关资源
      最近更新 更多