Skip to content

Instantly share code, notes, and snippets.

@superbderrick
Last active January 12, 2022 08:28
Show Gist options
  • Save superbderrick/2134b9217b051e5c9b729071ea3cc820 to your computer and use it in GitHub Desktop.
Save superbderrick/2134b9217b051e5c9b729071ea3cc820 to your computer and use it in GitHub Desktop.
import UIKit
import UnityFramework
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UnityFrameworkListener , NativeCallsProtocol {
func showHostMainWindow(_ color: String!) {
if(color == Constants.COLOR.BLUE) {
self.unitySampleView.nativeTitleLable.backgroundColor = .red
self.unitySampleView.nativeTitleLable.textColor = .white
} else if(color == Constants.COLOR.RED) {
self.unitySampleView.nativeTitleLable.backgroundColor = .white
self.unitySampleView.nativeTitleLable.textColor = .black
}else if(color == Constants.COLOR.WHITE) {
self.unitySampleView.nativeTitleLable.backgroundColor = .blue
self.unitySampleView.nativeTitleLable.textColor = .white
}
}
var window: UIWindow?
var appLaunchOpts: [UIApplication.LaunchOptionsKey: Any]?
var unitySampleView: UnityUIView!
var didQuit: Bool = false
@objc var unityFramework: UnityFramework?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
unityFramework = getUnityFramework()
appLaunchOpts = launchOptions
let storyboard = UIStoryboard(name: "Main", bundle: .main)
let viewController = storyboard.instantiateViewController(withIdentifier: "Host")
self.window = UIWindow.init(frame: UIScreen.main.bounds)
if let nativeWindow = self.window {
nativeWindow.rootViewController = viewController;
nativeWindow.makeKeyAndVisible()
}
return true
}
func applicationWillResignActive(_ application: UIApplication) {
if let unityFramework = self.unityFramework {
unityFramework.appController()?.applicationWillResignActive(application)
}
}
func applicationDidEnterBackground(_ application: UIApplication) {
if let unityFramework = self.unityFramework {
unityFramework.appController()?.applicationDidEnterBackground(application)
}
}
func applicationWillEnterForeground(_ application: UIApplication) {
if let unityFramework = self.unityFramework {
unityFramework.appController()?.applicationWillEnterForeground(application)
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
if let unityFramework = self.unityFramework {
unityFramework.appController()?.applicationDidBecomeActive(application)
}
}
func applicationWillTerminate(_ application: UIApplication) {
if let unityFramework = self.unityFramework {
unityFramework.appController()?.applicationWillTerminate(application)
}
}
// MARK: Unity API
private func getUnityFramework() -> UnityFramework? {
let bundlePath: String = Bundle.main.bundlePath + "/Frameworks/UnityFramework.framework"
let bundle = Bundle(path: bundlePath )
if bundle?.isLoaded == false {
bundle?.load()
}
let ufw = bundle?.principalClass?.getInstance()
if ufw?.appController() == nil {
let machineHeader = UnsafeMutablePointer<MachHeader>.allocate(capacity: 1)
machineHeader.pointee = _mh_execute_header
ufw!.setExecuteHeader(machineHeader)
}
return ufw
}
func unityIsInitialized( ) -> Bool {
return (self.unityFramework != nil && self.unityFramework?.appController() != nil)
}
func initUnity() {
if let nativeWindow = self.window {
if unityIsInitialized() {
UnitySampleUtils.showAlert(Constants.ERRORMESSAGES.ALREADY_INIT, Constants.ERRORMESSAGES.UNLOAD_FIREST, window: nativeWindow)
return
}
if didQuit {
UnitySampleUtils.showAlert(Constants.ERRORMESSAGES.CANNOTBE_INITIALIZED, Constants.ERRORMESSAGES.USE_UNLOAD, window:nativeWindow)
return
}
}
self.unityFramework = getUnityFramework()
if let unityframework = self.unityFramework {
unityframework.setDataBundleId("com.unity3d.framework")
unityframework.register(self)
NSClassFromString("FrameworkLibAPI")?.registerAPIforNativeCalls(self)
unityframework.runEmbedded(withArgc: CommandLine.argc, argv: CommandLine.unsafeArgv, appLaunchOpts: appLaunchOpts)
attachUnityView()
}
}
func attachUnityView() {
guard let unityRootView = unityFramework?.appController()?.rootView else {
return
}
self.unitySampleView = UnityUIView(frame: UIScreen.main.bounds)
unityRootView.addSubview(self.unitySampleView)
}
func unloadButtonTouched(_ sender: UIButton) {
unloadUnity()
}
func quitButtonTouched(_ sender: UIButton) {
if !unityIsInitialized() {
UnitySampleUtils.showAlert(Constants.ERRORMESSAGES.NOT_INITIALIZED, Constants.ERRORMESSAGES.INIT_FIREST, window: self.window)
} else {
if let unityFramework = getUnityFramework() {
unityFramework.quitApplication(0)
}
}
}
private func unloadUnityInternal() {
if let unityFramework = self.unityFramework {
unityFramework.unregisterFrameworkListener(self)
}
self.unityFramework = nil
if let nativeWindow = self.window {
nativeWindow.makeKeyAndVisible()
}
}
private func unloadUnity() {
if !unityIsInitialized() {
UnitySampleUtils.showAlert(Constants.ERRORMESSAGES.NOT_INITIALIZED, Constants.ERRORMESSAGES.INIT_FIREST, window: self.window)
return
} else {
if let unityFramework = getUnityFramework() {
unityFramework.unloadApplication()
}
}
}
func unityDidUnload(_ notification: Notification!) {
unloadUnityInternal()
}
func unityDidQuit(_ notification: Notification!) {
unloadUnityInternal()
self.didQuit = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment