Skip to content

Instantly share code, notes, and snippets.

@mao-test-h
Last active May 7, 2021 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mao-test-h/95b8de9147c95a58570683ce67b864de to your computer and use it in GitHub Desktop.
Save mao-test-h/95b8de9147c95a58570683ce67b864de to your computer and use it in GitHub Desktop.
Unity上からiOS端末の画面輝度を取得/設定
using System.Runtime.InteropServices;
using UnityEngine;
namespace iOSNative
{
/// <summary>
/// `UIScreen`のBridge
/// </summary>
/// <remarks>
/// - https://developer.apple.com/documentation/uikit/uiscreen
/// </remarks>
public static class UIScreenBridge
{
/// <summary>
/// 画面の輝度
/// </summary>
/// <remarks>
/// NOTE:
/// - 値は[0f~1f]の範囲
/// - iOS実機以外は常に1fを返す
/// - APIの性質からアプリ上で設定した輝度は端末がロックされるまで有効
/// </remarks>
public static float Brightness
{
get
{
#if !UNITY_EDITOR && UNITY_IOS
return GetBrightnessNative();
#else
return 1f;
#endif
}
set
{
var val = Mathf.Clamp01(value);
#if !UNITY_EDITOR && UNITY_IOS
SetBrightnessNative(val);
#endif
}
}
#region P/Invoke
[DllImport("__Internal", EntryPoint = "getBrightness")]
static extern float GetBrightnessNative();
[DllImport("__Internal", EntryPoint = "setBrightness")]
static extern void SetBrightnessNative(float brightness);
#endregion P/Invoke
}
}
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#ifdef __cplusplus
extern "C" {
#endif
// 画面の輝度の取得
// NOTE: 値は[0f~1f]の範囲
// ref: https://developer.apple.com/documentation/uikit/uiscreen/1617830-brightness?language=objc
float getBrightness() {
return [[UIScreen mainScreen] brightness];
}
// 画面の輝度の設定
// NOTE: 値は[0f~1f]の範囲
// ref: https://developer.apple.com/documentation/uikit/uiscreen/1617830-brightness?language=objc
void setBrightness(float brightness) {
[UIScreen mainScreen].brightness = brightness;
}
#ifdef __cplusplus
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment