Here is how I solved this:
It’s based on an answer by Khalos … and before that MichaelTaylor3D
It accesses the Android wakelock for your Unity project, which has 4 different levels to choose from: PARTIAL_WAKE_LOCK, SCREEN_DIM_WAKE_LOCK, SCREEN_BRIGHT_WAKE_LOCK, FULL_WAKE_LOCK
1. Build this java plugin below and put in your .jar file in a subdirectory named “Assets\Plugins\Android”.
(for help on building a .jar here are some instructions)
package com.YourCompany.YourAppName;
import com.unity3d.player.UnityPlayerNativeActivity;
import android.content.Context;
import android.util.Log;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
public class UnityPlayerWithWakeLock extends UnityPlayerNativeActivity
{
private PowerManager.WakeLock wl;
//protected void onCreate(Bundle savedInstanceState)
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
int wakeLock = PowerManager.SCREEN_DIM_WAKE_LOCK; // .. or PARTIAL_WAKE_LOCK or SCREEN_DIM_WAKE_LOCK or SCREEN_BRIGHT_WAKE_LOCK or FULL_WAKE_LOCK
Log.i("Unity", "Setting wake lock to " + Integer.toString(wakeLock) );
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(wakeLock, "UnityPlayerWithWakeLock");
}
@Override
protected void onPause()
{
super.onPause();
wl.release();
}
@Override
protected void onResume()
{
super.onResume();
wl.acquire();
}
}
**2.** You need to [edit your AndroidManifest.xml](http://forum.unity3d.com/threads/modifying-android-manifest.113094/) file. Once in there, just change this
```
android:name="com.unity3d.player.UnityPlayerActivity"
```
.. to this .
android:name=".UnityPlayerWithWakeLock"
… and also add this
uses-permission android:name="android.permission.WAKE_LOCK"
^(thanks to Obsurveyor for noting that one)
You don’t need to add any script hooks, and it should all just work.
Also here’s the batch file I last used to build the .jar files… i’ve called it BuildJava.bat and run it from Assets/Plugins/Android
"C:\Program Files\Java\jdk1.7.0_79\bin\javac.exe" UnityPlayerWithWakeLock.java -classpath "C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar" -bootclasspath C:\Users\[YOUR USER]\AppData\Local\Android\sdk\platforms\android-23\android.jar -d . -Xlint:deprecation
pause
"C:\Program Files\Java\jdk1.7.0_79\bin\javap.exe" -s com.[YOUR PACKAGE NAME HERE].UnityPlayerWithWakeLock
pause
"C:\Program Files\Java\jdk1.7.0_79\bin\jar.exe" cvfM ../UnityPlayerWithWakeLock.jar com/
pause