首页 > 解决方案 > Unity View 位于当前视图之上

问题描述

我正在尝试将适用于 iOS 的 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
        }
    }


}

下面是视图控制器代码:

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

        }

    }

}

我希望按钮始终保持在统一视图的顶部。知道问题出在哪里吗?

标签: swiftunity3d

解决方案


ViewContoller,在哪里Unity加载,创建按钮的出口并以viewDidload()这种方式调用此函数:

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

推荐阅读