首先,我想感谢 Michaeltaylor3d的答案 指向我指向正确的方向。
他解决了我认为我已纠正的问题的方式存在一些问题:
第一个问题是 KeyguardManager
锁定屏幕的方法。这是在API版本8中贬值的,无法在API 9+上使用。新解决方案使用设备管理API,这对于游戏来说似乎非常侵入性。
我浏览了Eclipse中的UnityPlayer定义,并找到了一个称为SetWakelock(布尔值)的函数,但是它是私人的。
我进行了定制的Android活动。在其中,我访问受保护的Unityplayer功能 setWakeLock(boolean)
并将其称为 onPause
功能。
我承认这不是理想的,但是它似乎没有副作用。我已经向Unity提交了一个错误报告,因此希望长期不需要这项工作。
public class UnityPlayerWithLockscreen extends UnityPlayerNativeActivity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
//Provides access to the private UnityPlayer.setWakeLock(boolean) method.
public void setWakeLock(boolean useWakelock) {
Method setWakeLockMethod;
//Use Reflection to get the setWakeLock(boolean) method from the UnityPlayer class
setWakeLockMethod = mUnityPlayer.getClass().getDeclaredMethod("setWakeLock");
//Set the method to me accessible
setWakeLockMethod.setAccessible(true);
//Invoke the method with our argument
setWakeLockMethod.invoke(mUnityPlayer, useWakelock);
}
@Override
protected void onPause()
{
super.onPause();
//Force unity to release the wakelock
setWakeLock(false);
}
}
然后,您需要将此活动设置为AndroidManifest文件中的主要活动:
<?xml version="1.0" encoding="utf-8"?>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".UnityPlayerWithLockscreen"
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>
该解决方案也不需要任何C#脚本或C#<-> Java Interop。
同样,我知道这是一种刺激的解决方案,但是无论API级别如何,它似乎都可以在几个设备上使用。希望这很快就会结合起来,并且将不再需要丑陋的修复。