开发者社区> 问答> 正文

在ARFoundation中从AR摄像机切换到iOS摄像机

我在ARFoundation中有一个Unity3D项目,我已经成功地使用这方法。

曾经Unity加载我将增加一个3D模型的进入AR,直到这一点,所有的工作良好。现在我试着用普通的摄像机拍摄视频UIImagePickerController在增强后发生。我能够从AR相机切换到IOS相机,但当我试图切换回Unity现实世界的追踪冻结了。当我从iOS摄像头切换回来时,我希望现实世界的跟踪重新开始。有办法解决这个问题吗?

编辑:

我一直在跟踪迅速-团结GitHub将把统一项目嵌入到IOS中。我在GitHub页面中添加了所有所需的文件。这些是我修改过的一些文件

我的AppDelgate

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

var application: UIApplication?

@objc var currentUnityController: UnityAppController!

var isUnityRunning = false

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    self.application = application
    unity_init(CommandLine.argc, CommandLine.unsafeArgv)

    currentUnityController = UnityAppController()
    currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)

    // first call to startUnity will do some init stuff, so just call it here and directly stop it again
    startUnity()
    stopUnity()

    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

    if isUnityRunning {
        currentUnityController.applicationWillResignActive(application)
    }
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    if isUnityRunning {
        currentUnityController.applicationDidEnterBackground(application)
    }
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

    if isUnityRunning {
        currentUnityController.applicationWillEnterForeground(application)
    }
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    if isUnityRunning {
        currentUnityController.applicationDidBecomeActive(application)
    }
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    if isUnityRunning {
        currentUnityController.applicationWillTerminate(application)
    }
}

func startUnity() {
    if !isUnityRunning {
        isUnityRunning = true
        currentUnityController.applicationDidBecomeActive(application!)
    }
}

func stopUnity() {
    if isUnityRunning {
        currentUnityController.applicationWillResignActive(application!)
        isUnityRunning = false
    }
}
}

UnityViewController

class ViewController: UIViewController {


override func viewDidLoad() {
    super.viewDidLoad()
    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
        appDelegate.startUnity()

        NotificationCenter.default.addObserver(self, selector: #selector(handleUnityReady), name: NSNotification.Name("UnityReady"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(handleUnityToggleRotation(_:)), name: NSNotification.Name("UnityToggleRotation"), object: nil)
    }
}

@objc func handleUnityReady() {
    showUnitySubView()
}



@IBAction func handleSwitchValueChanged(sender: UISwitch) {
    UnityPostMessage("NATIVE_BRIDGE", "StopCamera")
}

func showUnitySubView() {
    if let unityView = UnityGetGLView() {
        // insert subview at index 0 ensures unity view is behind current UI view
        view?.insertSubview(unityView, at: 0)

        unityView.translatesAutoresizingMaskIntoConstraints = false
        let views = ["view": unityView]
        let w = NSLayoutConstraint.constraints(withVisualFormat: "|-0-[view]-0-|", options: [], metrics: nil, views: views)
        let h = NSLayoutConstraint.constraints(withVisualFormat: "V:|-75-[view]-0-|", options: [], metrics: nil, views: views)
        view.addConstraints(w + h)
    }
}
    private func showPickerCamera(){if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera)){
        imagePicker.sourceType = .camera
        if viewModel.uploadType?.value == .videos {
            imagePicker.mediaTypes = [ kUTTypeMovie as String]
            imagePicker.cameraCaptureMode  = .video
            imagePicker.videoQuality = .typeMedium
        }else{
            imagePicker.mediaTypes = [ kUTTypeImage  as String]
            imagePicker.cameraCaptureMode  = .photo
        }

        imagePicker.allowsEditing = true
        imagePicker.modalPresentationStyle = .fullScreen

        self.present(imagePicker, animated: true, completion: nil)
    }
}
}

UnityUtils.mm

#include "RegisterMonoModules.h"
#include "RegisterFeatures.h"
 #include <csignal>
 static const int constsection = 0;

 void UnityInitTrampoline();

 extern "C" void unity_init(int argc, char* argv[])
 {
  @autoreleasepool
{
    UnityInitTrampoline();
    UnityInitRuntime(argc, argv);

    RegisterMonoModules();
    NSLog(@"-> registered mono modules %p\n", &constsection);
    RegisterFeatures();


    std::signal(SIGPIPE, SIG_IGN);
  }
}
 extern "C" void UnityPostMessage(NSString* gameObject, NSString* 
   methodName, NSString* message)
   {
  UnitySendMessage([gameObject UTF8String], [methodName UTF8String], 
 [message UTF8String]);
  }

展开
收起
游客5akardh5cojhg 2019-12-16 21:29:08 1374 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
3D/AR技术在新零售商业场景中的应用 立即下载
从2D到3D ,AR发展中的关键技术 立即下载
从2D到3D,AR发展中 的关键技术 立即下载