- Home /
[Android] How to dim screen without locking device?
Hi!
I keep finding how to prevent device from locking (if that is what led You to this Answer its `Screen.sleepTimeout = SleepTimeout.NeverSleep;`).
In fact its part of solution to my problem. What I aim to achive is to keep device from locking but dim screen/backlight to save battery. Application should stay responsive underneath so that user can bring it up with one tap.
Thanks in advance!
Answer by Zumi · Mar 24, 2016 at 09:42 PM
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 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
Any idea how to do this with Unity 2017.3.1f1?
I've been trying to get this to work with no luck. The app just crashes on launch.
I've just tried this with 2017.3.0f3 and it's still working fine for me - or did it just break recently for you?
I just started trying to implement it so I've never had it work. If it works for you then I assume I'm doing something wrong. $$anonymous$$aybe something in the compile step from that other link. Any chance you could update your answer with the compilation steps? I followed that URL you link to, to build the JAR but I think its out of date. I found that I had to specify a -target and -source to javac to even get it to build.
Answer by Tekksin · Mar 04, 2015 at 09:17 PM
I would love an answer on this. Would be great for games that need to stay active and rely on audio cues for the user to do things in the real world, while the device remains dimmed without eventually shutting off.
Answer by Riskjockey · Apr 01, 2019 at 08:50 AM
Hi, sorry to de-necrotize this thread but this is for anyone who is still looking for a solution.
I recently had the same problem and despite quite a lot of googling, there didn't seem to be any answers. So I built a tool in Unity to do it, it's called Dimmer, and I've made it available on the asset store: https://assetstore.unity.com/packages/tools/integration/dimmer-141555
To darken the screen and in combination with Screen.sleepTimeout you can use Dimmer like this:
Screen.sleepTimeout = SleepTimeout.NeverSleep;
dimmer.brightness = 0.1f;
So it's pretty easy to use, it works on iOS and Android and it doesn't require any android permissions to work. Hope someone finds it helpful.
Your answer
Follow this Question
Related Questions
Screen brightness on Android 2 Answers
Screen dims when its not supposed to 2 Answers
How do I rotate an object on one axis to face android touch? 0 Answers
White screen before Splash screen change to black 0 Answers
Flickering screen after Android device lock/unlock,Flickering screen after device lock 0 Answers