AndroidJavaClass/AndroidJavaObject导致构建的应用程序崩溃



我有一个Android应用程序,它在将以下代码引入场景中的对象之前构建并运行find。我的目标是为我的应用程序以不同的长度振动,而不仅仅是使用默认的Handheld.Vibrate()函数。一旦添加了这些代码,我的应用程序仍然可以很好地构建到我的设备上,但在启动时会崩溃。

我使用的是Unity 2019.2.9f1,我的最低API级别设置为25,使用的是Mono和.NET 2.0。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HapticController : Singleton<HapticController> {
#if UNITY_ANDROID && !UNITY_EDITOR
public static AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
public static AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
public static AndroidJavaObject vibrator = currentActivity.Call<AndroidJavaObject>("getSystemService", "vibrator");
#else
public static AndroidJavaClass unityPlayer;
public static AndroidJavaObject currentActivity;
public static AndroidJavaObject vibrator;
#endif
bool isVibrating;
...
// Update is called once per frame
void Update()
{
if(!isVibrating && ( *other condition* ) ){
isVibrating = true; //this will later be changed, just trying to get it to vibrate once            
Vibrate(200);
}
}
public static void Vibrate(long milliseconds)
{
if(isAndroid() && vibrator != null){
print("vibrating");
vibrator.Call("vibrate", milliseconds);
} else {
Handheld.Vibrate();
}
}
private static bool isAndroid()
{
#if UNITY_ANDROID && !UNITY_EDITOR
return true;
#else
return false;
#endif
}
}

我知道是顶部的AndroidJavaClass/AndroidJavaObject调用导致了崩溃。我已经在安卓工作室的logcat中检查了日志,但没有明显的输出与崩溃有关。是不是有什么我没有解释的原因会导致这种情况?

编辑:我应该补充一点,我使用的是一个SDK(Mapbox(,它有自己的AndroidManifest.xml。它指示你重命名它,并将它移到Plugins/Android文件夹中。你可以在这里看到:

<!--
Android Manifest for UniAndroid Permission (2016/03/19 sanukin39)
--- if already have AndroidManifest file at Assets/Plugins/Android/ ----
Copy the activity and meta-data sentence to your AndroidManifest.xml
--- if not ---
Rename this file to AndroidManifest.xml and add permission you want to add And move the file to Assets/Plugins/Android
-->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.product">
<application android:icon="@drawable/app_icon" android:label="@string/app_name">
<activity android:name="net.sanukin.OverrideUnityActivity"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
</application>
</manifest>

对于其他对此感到困惑的人,我能够通过结合Farhan的答案并执行以下操作来解决这个问题:

我没有初始化顶部的AndroidJavaClass和AndroidJavaObjects,而是创建了一个负责初始化的初始化函数。我添加了行

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]

以便强制它在场景加载之前初始化它们。我不确定为什么需要这个,但它为我修复了它。你可以看到下面的完整功能:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HapticController : Singleton<HapticController> {
public static AndroidJavaClass unityPlayer = null;
public static AndroidJavaObject currentActivity = null;
public static AndroidJavaObject vibrator = null;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void Initialize(){
#if UNITY_ANDROID && !UNITY_EDITOR
if (Application.platform == RuntimePlatform.Android) {
unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
vibrator = currentActivity.Call<AndroidJavaObject>("getSystemService", "vibrator");
}
#endif
}
...
}

您应该在{Projectlocation}\Assets\Plugins\Android\AndroidManifest.xml中添加以下振动权限

<uses-permission android:name="android.permission.VIBRATE"/>

所以你的最后一个AndroidManifest变成了。

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:theme="@style/UnityThemeSelector"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:debuggable="true">
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>

相关内容

  • 没有找到相关文章

最新更新