Last active
May 7, 2021 20:16
-
-
Save mao-test-h/b1cea20820832499a7e7942589db7e48 to your computer and use it in GitHub Desktop.
Unity上からiOS端末のダークモード判定を行う
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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