Unity视图位于当前视图之上

时间:2019-07-30 10:39:36

标签: swift unity3d

我正在尝试将iOS的Unity构建集成到预先存在的本机应用程序中。我使用两个按钮来启动和停止单位,但是当我单击开始时,Unity视图出现在当前视图的顶部,两个按钮都消失在它的后面。我使用Unity 2019.1.11f1构建统一项目。

以下是我的应用委托代码:

//
//  AppDelegate.swift
//  blue
//
//  Created by Vikas Roy on 01/07/19.
//  Copyright © 2019 MedleyOne. All rights reserved.
//

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: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        self.application = application

        unity_init(CommandLine.argc, CommandLine.unsafeArgv)

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

        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:.
    }

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

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


}

下面是View Controller代码:

//
//  Unity3DViewController.swift
//  blue
//
//  Created by Vikas Roy on 25/07/19.
//  Copyright © 2019 MedleyOne. All rights reserved.
//

import UIKit

class Unity3DViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func showUnitySubView() {
        if let unityView = UnityGetGLView() {
            view?.insertSubview(unityView, at: 0)
        }
    }


    @IBAction func StartUnity(_ sender: Any) {

        if let appDelegate = UIApplication.shared.delegate as? AppDelegate
        {
            appDelegate.startUnity()
            showUnitySubView()
        }

    }

    @IBAction func StopUnity(_ sender: Any) {

        if let appDelegate = UIApplication.shared.delegate as? AppDelegate
        {
            appDelegate.stopUnity()

        }

    }

}

我希望按钮始终位于统一视图的顶部。知道问题可能在哪里吗?

1 个答案:

答案 0 :(得分:0)

ViewContoller正在加载的Unity中, 创建按钮的插座 并以viewDidload()的方式调用此函数:

let unityView = UnityGetGLView()
unityView.addsubView(yourbutton)