Unity & iOS Dark Mode

The black iOS theme is one of the most trending features for the next year, and I think Unity Game and App developers would also like to know if the user is currently configured his device to use the black or light theme, to be able to adjust the game or application UI appearance accordingly.

I want to share an example of how you can get this info when building with Unity for iOS.

1. Make The *.mm file (MyPlugin.mm) from example. And place the following code inside 

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

extern "C" {
    bool _IsDakModeSet() {
        if (@available(iOS 12.0, *)) {
            if(UIScreen.mainScreen.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
                return true;
            }
        }
        
        return false;
    }
}

2. On the C# side, you can now make the MyPlugin.cs file that will query our native function.

#if UNITY_IPHONE 
using System.Runtime.InteropServices;
#endif

public static class MyPlugin
{
    #if UNITY_IPHONE
    [DllImport("__Internal")]
    private static extern bool _IsDakModeSet();
    #endif

    public static bool IsDakModeSet()
    {
        #if UNITY_IPHONE
        return _IsDakModeSet();
        #endif
    }
}

Now when you need to define if the user prefers to use dark or like mode, you can do it like this:

if (MyPlugin.IsDakModeSet())
{
    // Use Dark Skin.
}
else
{
    //use Light Skin.
}

It makes sense to check this flag on your application lunch. And also, if your UI supports a hot skin swap, you can check if the device system preference was changed each time your application is restored from the background. You can do this with Unity OnApplicationPause callback.

If you like to use already made solution that is constantly supported, take a look at IOS Native Pro or the Ultimate Mobile plugins. Here is the code snippet how you can retrieve the same preference using any of those plugins:

using SA.iOS.UIKit;
...

var userInterfaceStyle = ISN_UIScreen.MainScreen.TraitCollection.UserInterfaceStyle;
if (userInterfaceStyle == ISN_UIUserInterfaceStyle.Dark)
{
    // Use Dark Skin.
}
else
{
    //use Light Skin.
}

 

 

User replies

No replies yet