I have been looking for a way to detect if Windows Night Light mode is on in Unity 3D using C#.
I have found a post with a similar question Get status of night light mode in Windows 10, but I cannot make it work and code gives me following error when I tried using it in Unity:
CS0103: The name 'Registry' does not exist in the current context
I have tried replacing Registry
with System.Environment.UserName
. This produced another error:
CS1061: 'string' does not contain a definition for 'OpenSubKey' and no accessible extension method 'OpenSubKey' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
How to solve this issue?
Here are examples of the code that does not work in Unity:
private static bool IsNightLightEnabled()
{
const string BlueLightReductionStateKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\CloudStore\Store\DefaultAccount\Current\default$windows.data.bluelightreduction.bluelightreductionstate\windows.data.bluelightreduction.bluelightreductionstate";
using (var key = Environment.UserName.OpenSubKey(BlueLightReductionStateKey))
{
//this doesn't matter
}
}
and
private static bool IsNightLightEnabled()
{
const string BlueLightReductionStateKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\CloudStore\Store\DefaultAccount\Current\default$windows.data.bluelightreduction.bluelightreductionstate\windows.data.bluelightreduction.bluelightreductionstate";
using (var key = Registry.OpenSubKey(BlueLightReductionStateKey))
{
//this doesn't matter
}
}
To get the errors I'm encountering paste these into a C# script and put into a Unity project assets.