UnitySendMessage崩溃游戏

时间:2019-03-04 09:27:48

标签: android unity3d plugins

我有这样一个问题 我为Unity Android做一个插件。 Unity 5.6.5。 我收集了Android Studio 3.3。 我通过assembleRelease组装了插件。 设置gradle:

apply plugin: 'com.android.library'

android 
{
    compileSdkVersion 28
    defaultConfig 
    {

        minSdkVersion 21
        targetSdkVersion 28

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes 
    {
        release 
        {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies 
{
    implementation fileTree(dir: 'libs', include: ['*.jar'], exclude: ['classes.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    compileOnly files('libs/classes.jar')
}

类代码:

package andlancer.unity.custom.web;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;


public class TrueWeb extends UnityPlayerActivity 
{
    public static String EXTRA_URL = "extra.url";

    static CustomTabsClient mClient;
    static String packageName = "com.android.chrome";

    private boolean mCustomTabsOpened = false;

    private static final int CHROME_CUSTOM_TAB_REQUEST_CODE = 100;

    public static Context mContext;



    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        mContext=this;

        Log.e("Unity", "OnCreate...........");
    }

    public static void startFunc(UnityPlayerActivity activity)
    {
        Log.e("test","-----------------test");
        UnityPlayer.UnitySendMessage("Core", "CloseWeb", null);
    }
}

清单插件(插入到aar中):

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="andlancer.unity.custom.web">

    <application
        android:allowBackup="true"

        android:label="@string/app_name">
        <activity
            android:name=".TrueWeb"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

统一代码:

AndroidJavaClass plugin;
plugin = new AndroidJavaClass("andlancer.unity.custom.web.TrueWeb");

using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
        plugin.CallStatic("startFunc", activity);

日志:

Log.e("Unity", "OnCreate..........."); // not shown
Log.e("test","-----------------test"); // shown

,以及在调用UnityPlayer.UnitySendMessage时 应用程序崩溃而设备上没有错误 在日志中写道:

03-04 19:24:23.183 201-201/? A/DEBUG: backtrace:
03-04 19:24:23.183 201-201/? A/DEBUG:     #00 pc 000188ac  
/system/lib/libc.so (strlen+71)
03-04 19:24:23.183 201-201/? A/DEBUG:     #01 pc 0059d270  
/data/app/bla.bla.huyar-2/lib/arm/libunity.so (UnitySendMessage+24)
03-04 19:24:23.183 201-201/? A/DEBUG:     #02 pc 005f3a2c  
/data/app/bla.bla.huyar-2/lib/arm/libunity.so
03-04 19:24:23.183 201-201/? A/DEBUG:     #03 pc 00b34b45  
/data/app/bla.bla.huyar-2/oat/arm/base.odex (offset 0x6ce000)

3 个答案:

答案 0 :(得分:0)

如果另一个类的类包含“扩展UnityPlayerActivity”,则只有第一个加载的类会使用此上下文。对我来说,为了解决熟悉的问题,我将上下文从Unity传递给aar(作为Init函数)。 在“ TrueWeb”类中创建一个函数,该函数将返回mContext。然后将此上下文用于您需要在Android aar中使用上下文的每个地方

答案 1 :(得分:0)

尝试将最后一个参数从“ null”替换为“”。

UnityPlayer.UnitySendMessage("Core", "CloseWeb", "");

答案 2 :(得分:0)

请发送空字符串作为参数,其预期的字符串null可能会导致崩溃

相关问题