2 Replies
      Latest reply on Apr 4, 2019 10:11 AM by yasincan
      Skote Level 1 Level 1 (0 points)

        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

        • Re: Using the Torch while using ARkit?
          manutd100 Level 1 Level 1 (0 points)

          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()

                  }

          • Re: Using the Torch while using ARkit?
            yasincan Level 1 Level 1 (0 points)

            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()
                    }
                }