1

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.

Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
DiscoLlife
  • 31
  • 7
  • 2
    Welcome to Stack Overflow! Please copy the error messages as _text_ (using `code formatting`) in the question itself. [related meta post](https://meta.stackoverflow.com/a/285557/479251) – Pac0 May 10 '21 at 06:09
  • 1
    @Pac0 I have added examples of my code I trialed, the other two answers gave have to reference it but not how i could implement into my code – DiscoLlife May 10 '21 at 08:02
  • My main problem here is how do I access Microsoft.Win32.Registry without producing an error in unity. The linked post was using a different api which hasn't translated over to unity when I tried using it. – DiscoLlife May 10 '21 at 10:39
  • yep, Unity is based on a cross platform API. Obviously, cross-platform means, there is no function to access windows registry specifically (this will prevent the application to be built for Mac / linux / android / IOS / WebGL etc...) – Pac0 May 10 '21 at 10:53
  • What I think you could do, is code those registry-reading functions in a simple .NET framework library project from Visual Studio (as a different project from the Unity one), and then add the compiled DLL as a dependency in your Unity project. You could read more about this there for instance: https://stackoverflow.com/questions/20703689/unity3d-can-i-use-net-4-5-assembly-as-external-library . It's a bit outdated, I don't even know if Unity3D still uses Mono platform (crossplatform .NET) anymore, but some principles should stay the same – Pac0 May 10 '21 at 10:59
  • Thank you for sticking with me and helping. How would someone do this exactly? I'm not exactly knowledgeable in .NET and don't where exactly to begin for this. – DiscoLlife May 10 '21 at 11:22

1 Answers1

1

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.

DiscoLlife
  • 31
  • 7