0

I have been looking for a way to detect if window night light mode is on in unity3d using c# but have been unable to find that would work in unity. I have found a post with a similar question here, however this doesn't work with unity and have given me this error when I tried using it in unity.See Here. I have tried switching registry out for System.Environment.UserName. This produced another new error.See Error 2 here. If someone knows how to solve this issue I would be glad if you helped. Heres an example of the code that unity is refusing:

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.

DiscoLlife
  • 21
  • 6
  • 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
  • 21
  • 6