Android UnitySendMessage崩溃游戏

Android UnitySendMessage崩溃游戏,android,unity3d,plugins,Android,Unity3d,Plugins,我有这样一个问题 我为Unity Android做了一个插件。 统一5.6.5。 我在Android Studio 3.3中收集。 我通过AssemblerEase组装插件。 设置梯度: apply plugin: 'com.android.library' android { compileSdkVersion 28 defaultConfig { minSdkVersion 21 targetSdkVersion 28

我有这样一个问题 我为Unity Android做了一个插件。 统一5.6.5。 我在Android Studio 3.3中收集。 我通过AssemblerEase组装插件。 设置梯度:

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):

调用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)

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

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


UnityPlayer.UnitySendMessage(“Core”、“CloseWeb”和“)

请将空字符串作为参数发送,它的预期字符串为null可能是崩溃的原因

我不知道您在哪里调用它,尽管即使在gamedev中,发送消息也不是一个好的做法。您可以使用对象缓存、单例缓存或其他方式,而不是SendMessage/reflection(非常缓慢且难以重构)
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
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)