Reputation: 31
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.
Upvotes: 1
Views: 283
Reputation: 31
Looks like I wont need to do any special things with .NET framework library projects. Finding the answer was hard but I managed to track it down to my unity preferences.
To fix this all you need to do is open a unity project, go to Edit > Project Settings > Player. Next select the settings for PC and go to Other Settings > Configuration > API level compatibility and select .NET 4.x
This will allow you to use most of .NET 4.5 methods functions all that in your Unity Projects which includes Registry and all of its methods and fields.
Upvotes: 1