The black iOS theme is one of the most trending features of the current 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. Place this file under the Assets/Plugins/iOS folder. Code (CSharp): #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. Code (CSharp): #if UNITY_IPHONE || UNITY_IOS using System.Runtime.InteropServices; #endif public static class MyPlugin { #if UNITY_IPHONE || UNITY_IOS [DllImport("__Internal")] private static extern bool _IsDakModeSet(); #endif public static bool IsDakModeSet() { #if UNITY_IPHONE || UNITY_IOS return _IsDakModeSet(); #endif } } Now when you need to define if the user prefers to use dark or lite mode, you can do it like this: Code (CSharp): 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: Code (CSharp): using SA.iOS.UIKit; ... var userInterfaceStyle = ISN_UIScreen.MainScreen.TraitCollection.UserInterfaceStyle; if (userInterfaceStyle == ISN_UIUserInterfaceStyle.Dark) { // Use Dark Skin. } else { //use Light Skin. } I hope it was helpful
As Promised: On Android starting from api level 29 (Q) it's possible to define if the user chooses a dark mode as a preference. I won't be describing the whole native plugin creation process for the android, since it's ready done in many many articles. I like this one for example: http://markcastle.com/steps-to-crea...ity-in-java-using-android-studio-part-1-of-2/ So here is the simple Java function you need to make: Code (CSharp): public boolean IsDarkMode() { Configuration configuration = UnityPlayer.currentActivity.getResources().getConfiguration(); int currentNightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK; switch (currentNightMode) { case Configuration.UI_MODE_NIGHT_NO: // Night mode is not active, we're using the light theme return false; case Configuration.UI_MODE_NIGHT_YES: // Night mode is active, we're using dark theme return true; } return false; } And of course, it's now also available with Android Native Pro Wiki: https://github.com/StansAssets/com.stansassets.android-native/wiki/Dark-Mode-(Theme) Code Sample: Code (CSharp): using SA.Android.App; using SA.Android.Content.Res; ... // Only supported from android 10.0 (api 29) if (AN_Build.VERSION.SDK_INT < AN_Build.VERSION_CODES.Q) { Debug.Log("Not supported") return; } var configuration = AN_MainActivity.Instance.GetResources().GetConfiguration(); var currentNightMode = configuration.UIMode & AN_Configuration.UI_MODE_NIGHT_MASK; switch (currentNightMode) { case AN_Configuration.UI_MODE_NIGHT_NO: // Night mode is not active, we're using the light theme Debug.Log("Night mode is not active, we're using the light theme"); break; case AN_Configuration.UI_MODE_NIGHT_YES: // Night mode is active, we're using dark theme Debug.Log("Night mode is active, we're using dark theme"); break; } And the cross-platfrom Ultimate Mobile plugin: Wiki: https://github.com/StansAssets/com.stansassets.ultimate-mobile/wiki/Dark-Mode Code Sample: Code (CSharp): using SA.CrossPlatform.App; ... Debug.Log(UM_Application.IsDarkMode ? "Night mode is active, we're using dark theme" : "Night mode is not active, we're using the light theme");
@lacost is there a simple way to check internet connection on apple phone/tablet devices? I wrote a plugin for android but there is no initial example for ios. have any idea?
Are you referring for some specific API. 1. What is wrong with the default unity way to check the connection type? 2. The fact that you have a connection doesn't mean that you have internet. 3. The fact that you do have internet doesn't mean that you can reach the web address you need. This means, that the easier way is to the ping IP address you need.
I tried your piece of code, but I get an error at "UnityPlayer.currentActivity". It doesn't find "UnityPlayer" and I can't find any class to import or workaround to this. Could you help me out?
Hey, @pirottedamien you need to grab unity-classes.jar and add it as a dependency into your library. This jar file can be found somewhere in the Unity installation folder, I do not remember where exactly. But I can attach it to this post so you won't have a look. https://drive.google.com/file/d/1L1-q3MB3OWrp8BE8OZV1Fq5-wK20XR5e/view?usp=sharing
Hello, good morning, I had a question. I use the " Ultimate Mobile Pro " package, but I can not get this code. Can you help me? using SA.CrossPlatform.Appک Debug.Log(UM_Application.IsDarkMode ? "Night mode is active, we're using dark theme" : "Night mode is not active, we're using the light theme");
Code (CSharp): UIScreen.mainScreen.traitCollection.userInterfaceStyle This iOS code works on a fresh empty project and returns the correct userInterfaceStyle value. However, when used in an actual project, it doesn't work anymore. The userInterfaceStyle value returned is always 1 (light mode) even if I've set the device to use dark mode. Could it be that one of the plugins we are using (Facebook, Firebase, etc) is interfering with this code somehow? Are there any usual suspects? The thing is, when I display native alerts, the dialog box shown is still using dark mode, which indicates to me that the app is aware that the device is using dark mode. It seems that only the code above is unaware that the device is using dark mode.