Skip to content

Instantly share code, notes, and snippets.

@ddutchie
Last active March 12, 2024 10:20
Show Gist options
  • Save ddutchie/4bfc880fbb056c3d72a9d8a1954287ab to your computer and use it in GitHub Desktop.
Save ddutchie/4bfc880fbb056c3d72a9d8a1954287ab to your computer and use it in GitHub Desktop.
Native Flash Light Controller for Unity3D and iOS
#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];
}
}
}
#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
@mtpython
Copy link

mtpython commented Aug 4, 2021

You can also check out MyAwesomeFlashlight unitypackage for Android.

@namthangnguyen
Copy link

You can also check out MyAwesomeFlashlight unitypackage for Android.

Can this code work on android? Can you share the code on android?

@mtpython
Copy link

mtpython commented Feb 9, 2023

@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.

@yosun
Copy link

yosun commented Jun 13, 2023

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