Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JOsojca · Oct 08, 2013 at 07:11 PM · androidscreenbatterydim

[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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Obsurveyor · Mar 25, 2018 at 12:45 AM 0
Share

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.

avatar image Zumi Obsurveyor · Mar 26, 2018 at 01:02 PM 0
Share

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?

avatar image Obsurveyor Zumi · Mar 26, 2018 at 01:17 PM 0
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges