Using the Torch while using ARkit?

Hi,


I'm working on a small augmented reality experience for a museum using Unity and ARkit 1.5. In some locations the light is very low so for ARkit to be able to see the markers I need to turn on the torch. I haven't found any tool for doing this within Unity so I guess I need to do it in Xcode. Does anyone have experience of this and can point me in the right direction?


Cheers

Replies

if let device = AVCaptureDevice.default(for: .video), let input = try? AVCaptureDeviceInput.init(device: device),

device.hasFlash, device.hasTorch {

var torch = input.device.torchMode

switch torch {

case .off:

torch = .on

sender.setBackgroundImage( imageLiteral(resourceName: "torch_off"), for: UIControlState.normal)

case .on:

torch = .off

sender.setBackgroundImage( imageLiteral(resourceName: "torch_on"), for: UIControlState.normal)

default:

break

}

try? input.device.lockForConfiguration()

input.device.torchMode = torch

input.device.unlockForConfiguration()

}

The answer at the above is true but the problem is if you turn on torch before starting right after starting AR session, it will close it automatically. What you should do is turn on the torch after some time. I start the AR session in viewDidLoad() and tried the turn on the torch in viewDidAppear() but OS was closing it right after. So i was able to make it work like this;


overridefunc viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
            self?.toggleFlash()
        }
    }