|
| 1 | +// |
| 2 | +// appDelegateFile.swift |
| 3 | +// UEKit |
| 4 | +// |
| 5 | +// Created by Eric Miller on 10/11/17. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +extension File { |
| 11 | + |
| 12 | + class func appDelegateFile() -> Data? { |
| 13 | + let file = """ |
| 14 | + import UIKit |
| 15 | +
|
| 16 | + @UIApplicationMain |
| 17 | + class AppDelegate: UIResponder, UIApplicationDelegate { |
| 18 | +
|
| 19 | + var window: UIWindow? |
| 20 | + @objc var currentUnityController: UnityAppController! |
| 21 | + private var application: UIApplication? |
| 22 | + private var isUnityRunning = false |
| 23 | +
|
| 24 | + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { |
| 25 | + self.application = application |
| 26 | +
|
| 27 | + unity_init(CommandLine.argc, CommandLine.unsafeArgv) |
| 28 | + currentUnityController = UnityAppController() |
| 29 | + currentUnityController!.application(application, didFinishLaunchingWithOptions: launchOptions) |
| 30 | +
|
| 31 | + // first call to startUnity will do some init stuff, so just call it here and directly stop it again |
| 32 | + startUnity() |
| 33 | + stopUnity() |
| 34 | +
|
| 35 | + return true |
| 36 | + } |
| 37 | +
|
| 38 | + func applicationWillResignActive(_ application: UIApplication) { |
| 39 | + if isUnityRunning { |
| 40 | + currentUnityController?.applicationWillResignActive(application) |
| 41 | + } |
| 42 | + } |
| 43 | +
|
| 44 | + func applicationDidEnterBackground(_ application: UIApplication) { |
| 45 | + if isUnityRunning { |
| 46 | + currentUnityController?.applicationDidEnterBackground(application) |
| 47 | + } |
| 48 | + } |
| 49 | +
|
| 50 | + func applicationWillEnterForeground(_ application: UIApplication) { |
| 51 | + if isUnityRunning { |
| 52 | + currentUnityController?.applicationWillEnterForeground(application) |
| 53 | + } |
| 54 | + } |
| 55 | +
|
| 56 | + func applicationDidBecomeActive(_ application: UIApplication) { |
| 57 | + if isUnityRunning { |
| 58 | + currentUnityController?.applicationDidBecomeActive(application) |
| 59 | + } |
| 60 | + } |
| 61 | +
|
| 62 | + func applicationWillTerminate(_ application: UIApplication) { |
| 63 | + if isUnityRunning { |
| 64 | + currentUnityController?.applicationWillTerminate(application) |
| 65 | + } |
| 66 | + } |
| 67 | +
|
| 68 | + func startUnity() { |
| 69 | + if !isUnityRunning, let app = application, let unityController = currentUnityController { |
| 70 | + isUnityRunning = true |
| 71 | + unityController.applicationDidBecomeActive(app) |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | + func stopUnity() { |
| 76 | + if isUnityRunning, let app = application, let unityController = currentUnityController { |
| 77 | + unityController.applicationWillResignActive(app) |
| 78 | + isUnityRunning = false |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + """.data(using: .utf8) |
| 83 | + return file |
| 84 | + } |
| 85 | +} |
0 commit comments