Last active
March 12, 2024 10:20
-
-
Save ddutchie/4bfc880fbb056c3d72a9d8a1954287ab to your computer and use it in GitHub Desktop.
Native Flash Light Controller for Unity3D and 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
#import <AVFoundation/AVFoundation.h> | |
extern "C" { | |
void _EnableFlashlight(bool enable) { | |
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; | |
if ([device hasTorch]) { | |
[device lockForConfiguration:nil]; | |
[device setTorchMode:enable ? AVCaptureTorchModeOn : AVCaptureTorchModeOff]; | |
[device unlockForConfiguration]; | |
} | |
} | |
bool _DeviceHasFlashlight() { | |
return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo].hasTorch; | |
} | |
void _SetFlashlightLevel(float level) { | |
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; | |
if ([device hasTorch]) { | |
[device lockForConfiguration:nil]; | |
if (level <= 0.0) { | |
[device setTorchMode:AVCaptureTorchModeOff]; | |
} else { | |
if (level >= 1.0) { | |
level = AVCaptureMaxAvailableTorchLevel; | |
} | |
BOOL success = [device setTorchModeOnWithLevel:level error:nil]; | |
} | |
[device unlockForConfiguration]; | |
} | |
} | |
} |
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
#if UNITY_IOS | |
using System.Runtime.InteropServices; | |
using UnityEngine; | |
/// <summary> | |
/// Class to interact with flashlight | |
/// </summary> | |
[PublicAPI] | |
public static class IGFlashlight | |
{ | |
/// <summary> | |
/// Indicates whether current device has a flashlight | |
/// </summary> | |
/// <returns><code>true</code> if current device has a flashlight, <code>false</code> otherwise</returns> | |
[PublicAPI] | |
public static bool HasTorch | |
{ | |
get | |
{ | |
if (IGUtils.IsIosCheck()) | |
{ | |
return false; | |
} | |
return _DeviceHasFlashlight(); | |
} | |
} | |
/// <summary> | |
/// Toggle the flashlight | |
/// </summary> | |
/// <param name="enable">Whether to enable or disable the flashlight</param> | |
[PublicAPI] | |
public static void EnableFlashlight(bool enable) | |
{ | |
if (IGUtils.IsIosCheck()) | |
{ | |
return; | |
} | |
_EnableFlashlight(enable); | |
} | |
/// <summary> | |
/// Enables flashlight with the provided intensity | |
/// </summary> | |
/// <param name="intensity">Intensity of the flashlight to set. Clamped between 0 and 1</param> | |
[PublicAPI] | |
public static void SetFlashlightIntensity(float intensity) | |
{ | |
intensity = Mathf.Clamp01(intensity); | |
if (IGUtils.IsIosCheck()) | |
{ | |
return; | |
} | |
_SetFlashlightLevel(intensity); | |
} | |
[DllImport("__Internal")] | |
static extern void _EnableFlashlight(bool enable); | |
[DllImport("__Internal")] | |
static extern void _SetFlashlightLevel(float val); | |
[DllImport("__Internal")] | |
static extern bool _DeviceHasFlashlight(); | |
} | |
#endif |
You can also check out MyAwesomeFlashlight unitypackage for Android.
You can also check out MyAwesomeFlashlight unitypackage for Android.
Can this code work on android? Can you share the code on android?
@namthangnguyen that isn't my code, but you can find the repository here:
https://github.com/glazunov/MyAwesomeFlashlight/
and the unitypackage by the link I posted above — just import that into your unity project.
can someone wrap it so it's cross platform iOS / Android?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone wondering — this works (iPhone X, Unity 2020.3.14f), and you can remove all the
and