iOS 应用程序中未显示嵌入式 Unity 应用程序
Embedded Unity Application not displaying in iOS app
我目前有一个 iOS 应用程序,并希望将 Unity 应用程序作为子视图嵌入到我的应用程序中。在过去的几个月里,我一直在努力解决这个问题。
一段时间以来我在构建应用程序时遇到问题,但现在它正在构建并且 运行 正常(几乎)。我希望 Unity 应用程序在其中显示的视图没有显示任何内容。
作为参考,我正在使用 Unity 2019.1.12f1 和 XCode 10.3
我没有使用 Vuforia,它只是一个非常基本的 Unity 应用程序,带有模型和基本动画。
我的 AppDelegate 文件是:
@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)
// 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) {f isUnityRunning {
currentUnityController.applicationWillResignActive(application)
}
}
func applicationDidEnterBackground(_ application: UIApplication) {
if isUnityRunning {
currentUnityController.applicationDidEnterBackground(application)
}
}
func applicationWillEnterForeground(_ application: UIApplication) {
if isUnityRunning {
currentUnityController.applicationWillEnterForeground(application)
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
if isUnityRunning {
currentUnityController.applicationDidBecomeActive(application)
}
}
func applicationWillTerminate(_ application: UIApplication) {
if isUnityRunning {
currentUnityController.applicationWillTerminate(application)
}
}
func startUnity() {
if !isUnityRunning {
isUnityRunning = true
currentUnityController.applicationDidBecomeActive(application!)
}
}
func stopUnity() {
if isUnityRunning {
currentUnityController.applicationWillResignActive(application!)
isUnityRunning = false
}
}
然后我使用这段代码在子视图中显示 Unity 代码
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.startUnity()
self.unityView = UnityGetGLView()
我完全不明白为什么这不起作用。任何帮助将不胜感激。
希望这些信息足够了。如果您还有其他需要了解的,请告诉我。
您的代表看起来一切正常,但您的 ViewController 缺少一些部分。试试这个代码:
import UIKit
class UnityViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.startUnity()
showUnitySubView()
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.stopUnity()
}
}
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:|-64-[view]-49-|", options: [], metrics: nil, views: views)
view.addConstraints(w + h)
}
}
}
我目前有一个 iOS 应用程序,并希望将 Unity 应用程序作为子视图嵌入到我的应用程序中。在过去的几个月里,我一直在努力解决这个问题。
一段时间以来我在构建应用程序时遇到问题,但现在它正在构建并且 运行 正常(几乎)。我希望 Unity 应用程序在其中显示的视图没有显示任何内容。
作为参考,我正在使用 Unity 2019.1.12f1 和 XCode 10.3
我没有使用 Vuforia,它只是一个非常基本的 Unity 应用程序,带有模型和基本动画。
我的 AppDelegate 文件是:
@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)
// 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) {f isUnityRunning {
currentUnityController.applicationWillResignActive(application)
}
}
func applicationDidEnterBackground(_ application: UIApplication) {
if isUnityRunning {
currentUnityController.applicationDidEnterBackground(application)
}
}
func applicationWillEnterForeground(_ application: UIApplication) {
if isUnityRunning {
currentUnityController.applicationWillEnterForeground(application)
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
if isUnityRunning {
currentUnityController.applicationDidBecomeActive(application)
}
}
func applicationWillTerminate(_ application: UIApplication) {
if isUnityRunning {
currentUnityController.applicationWillTerminate(application)
}
}
func startUnity() {
if !isUnityRunning {
isUnityRunning = true
currentUnityController.applicationDidBecomeActive(application!)
}
}
func stopUnity() {
if isUnityRunning {
currentUnityController.applicationWillResignActive(application!)
isUnityRunning = false
}
}
然后我使用这段代码在子视图中显示 Unity 代码
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.startUnity()
self.unityView = UnityGetGLView()
我完全不明白为什么这不起作用。任何帮助将不胜感激。
希望这些信息足够了。如果您还有其他需要了解的,请告诉我。
您的代表看起来一切正常,但您的 ViewController 缺少一些部分。试试这个代码:
import UIKit
class UnityViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.startUnity()
showUnitySubView()
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.stopUnity()
}
}
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:|-64-[view]-49-|", options: [], metrics: nil, views: views)
view.addConstraints(w + h)
}
}
}