Skip to content

Instantly share code, notes, and snippets.

@mao-test-h
Last active May 7, 2021 20:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mao-test-h/b1cea20820832499a7e7942589db7e48 to your computer and use it in GitHub Desktop.
Save mao-test-h/b1cea20820832499a7e7942589db7e48 to your computer and use it in GitHub Desktop.
Unity上からiOS端末のダークモード判定を行う
using System.Runtime.InteropServices;
namespace iOSNative
{
/// <summary>
/// `UITraitCollection`のBridge
/// </summary>
/// <remarks>
/// - https://developer.apple.com/documentation/uikit/uitraitcollection
/// </remarks>
public static class UITraitCollectionBridge
{
/// <summary>
/// ダークモード判定
/// </summary>
/// <remarks>
/// NOTE:
/// - iOS実機以外は常にfalseを返す
/// - iOS13より前なら常にfalseを返す
/// </remarks>
public static bool IsDarkMode
{
get
{
#if !UNITY_EDITOR && UNITY_IOS
return IsDarkModeNative();
#endif
return false;
}
}
#region P/Invoke
// NOTE: 「ダークモードかどうか?」を判定するメソッドは存在しないので、ネイティブコード側で自前判定を行っている
[DllImport("__Internal", EntryPoint = "isDarkMode")]
static extern bool IsDarkModeNative();
#endregion P/Invoke
}
}
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#ifdef __cplusplus
extern "C" {
#endif
// ダークモード判定
// NOTE: iOS13より前なら常にfalseを返す
bool isDarkMode() {
if (@available(iOS 13, *)) {
UITraitCollection *currentTraitCollection = [UITraitCollection currentTraitCollection];
return currentTraitCollection.userInterfaceStyle == UIUserInterfaceStyleDark;
} else {
return FALSE;
}
}
#ifdef __cplusplus
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment