|
|
|
@ -6,7 +6,6 @@ |
|
|
|
|
// |
|
|
|
|
|
|
|
|
|
import Foundation |
|
|
|
|
import cocos_view_pod |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private var cocos_warmed_up = false |
|
|
|
@ -37,7 +36,7 @@ public func InitCocosIntegrationWithOptions( |
|
|
|
|
/***********************************PLUGIN_ENTRY END**************************************/ |
|
|
|
|
|
|
|
|
|
// Load cocos framework for fisrt run |
|
|
|
|
func CocosFrameworkLoad() -> CocosFramework? { |
|
|
|
|
func CocosFrameworkLoad() -> dcocos_bridge? { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return dcocos_bridge.instance() |
|
|
|
@ -58,7 +57,7 @@ func GetCocosPlayerUtils() -> CocosPlayerUtils { |
|
|
|
|
|
|
|
|
|
/*********************************** GLOBAL FUNCS & VARS END****************************************/ |
|
|
|
|
|
|
|
|
|
var controller: CocosAppController? |
|
|
|
|
//var controller: CocosAppController? |
|
|
|
|
var sharedApplication: UIApplication? |
|
|
|
|
|
|
|
|
|
@objc protocol CocosEventListener: AnyObject { |
|
|
|
@ -67,31 +66,74 @@ var sharedApplication: UIApplication? |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@objc public class CocosPlayerUtils: UIResponder, UIApplicationDelegate, CocosFrameworkListener { |
|
|
|
|
var ufw: CocosFramework! |
|
|
|
|
@objc public class CocosPlayerUtils: UIResponder, UIApplicationDelegate { |
|
|
|
|
var ufw: dcocos_bridge! |
|
|
|
|
private var _isCocosPaused = false |
|
|
|
|
private var _isCocosReady = false |
|
|
|
|
private var _isCocosLoaded = false |
|
|
|
|
|
|
|
|
|
override init() { |
|
|
|
|
super.init() |
|
|
|
|
// 监听通知 |
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleViewWillTransition), name: NSNotification.Name("ViewWillTransition"), object: nil) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
deinit { |
|
|
|
|
// 移除通知监听 |
|
|
|
|
NotificationCenter.default.removeObserver(self) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@objc func handleViewWillTransition(notification: Notification) { |
|
|
|
|
guard let size = notification.userInfo?["size"] as? CGSize, |
|
|
|
|
let coordinator = notification.userInfo?["coordinator"] as? UIViewControllerTransitionCoordinator else { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
// 处理视图大小变化的逻辑 |
|
|
|
|
print("CocosPlayerUtils will transition to size: \(size)") |
|
|
|
|
//改正错误 |
|
|
|
|
let selector = NSSelectorFromString("viewWillTransitionToSize:withTransitionCoordinator:") |
|
|
|
|
if self.ufw.responds(to: selector) { |
|
|
|
|
self.ufw.perform(selector, with: size, with: coordinator) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func initCocos() { |
|
|
|
|
if (self.cocosIsInitiallized()) { |
|
|
|
|
self.ufw?.showCocosWindow() |
|
|
|
|
//todo |
|
|
|
|
//self.ufw?.showCocosWindow() |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 开始生成设备旋转通知 |
|
|
|
|
//UIDevice.current.beginGeneratingDeviceOrientationNotifications() |
|
|
|
|
|
|
|
|
|
self.ufw = CocosFrameworkLoad() |
|
|
|
|
|
|
|
|
|
// 获取当前Pod库的Bundle |
|
|
|
|
let bundle = Bundle(for: dcocos_bridge.self) |
|
|
|
|
|
|
|
|
|
// 获取 `YourPodResources.bundle` 的路径 |
|
|
|
|
if let bundleURL = bundle.url(forResource: "cocos_main_bundle", withExtension: "bundle") { |
|
|
|
|
let bundlePath = bundleURL.path |
|
|
|
|
print("Bundle absolute path: \(bundlePath)") |
|
|
|
|
self.ufw.configureCocos2dSearchPaths(bundlePath) |
|
|
|
|
} else { |
|
|
|
|
print("Could not find the bundle.") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//self.ufw?.setDataBundleId("com.cocos3d.framework") |
|
|
|
|
self.ufw.initPlatform() |
|
|
|
|
let application = UIApplication.shared |
|
|
|
|
self.ufw.application(application, didFinishLaunchingWithOptions: appLaunchOpts ) |
|
|
|
|
|
|
|
|
|
registerCocosListener() |
|
|
|
|
// self.ufw?.runEmbedded(withArgc: gArgc, argv: gArgv, appLaunchOpts: appLaunchOpts) |
|
|
|
|
|
|
|
|
|
if self.ufw?.appController() != nil { |
|
|
|
|
controller = self.ufw?.appController() |
|
|
|
|
controller?.cocosMessageHandler = self.cocosMessageHandlers |
|
|
|
|
controller?.cocosSceneLoadedHandler = self.cocosSceneLoadedHandlers |
|
|
|
|
self.ufw?.appController()?.window?.windowLevel = UIWindow.Level(UIWindow.Level.normal.rawValue - 1) |
|
|
|
|
} |
|
|
|
|
// if self.ufw?.appController() != nil { |
|
|
|
|
// controller = self.ufw?.appController() |
|
|
|
|
// controller?.cocosMessageHandler = self.cocosMessageHandlers |
|
|
|
|
// controller?.cocosSceneLoadedHandler = self.cocosSceneLoadedHandlers |
|
|
|
|
// self.ufw?.appController()?.window?.windowLevel = UIWindow.Level(UIWindow.Level.normal.rawValue - 1) |
|
|
|
|
// } |
|
|
|
|
_isCocosLoaded = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -107,13 +149,18 @@ var sharedApplication: UIApplication? |
|
|
|
|
// Create new cocos player |
|
|
|
|
func createPlayer(completed: @escaping (_ view: UIView?) -> Void) { |
|
|
|
|
if self.cocosIsInitiallized() && self._isCocosReady { |
|
|
|
|
completed(controller?.rootView) |
|
|
|
|
//todo |
|
|
|
|
//completed(controller?.rootView) |
|
|
|
|
completed(nil) |
|
|
|
|
|
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(forName: NSNotification.Name("CocosReady"), object: nil, queue: OperationQueue.main, using: { note in |
|
|
|
|
self._isCocosReady = true |
|
|
|
|
completed(controller?.rootView) |
|
|
|
|
//todo |
|
|
|
|
//completed(controller?.rootView) |
|
|
|
|
completed(nil) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
DispatchQueue.main.async { |
|
|
|
@ -134,7 +181,9 @@ var sharedApplication: UIApplication? |
|
|
|
|
|
|
|
|
|
self.listenAppState() |
|
|
|
|
|
|
|
|
|
completed(controller?.rootView) |
|
|
|
|
//todo |
|
|
|
|
//completed(controller?.rootView) |
|
|
|
|
completed(nil) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
@ -163,29 +212,30 @@ var sharedApplication: UIApplication? |
|
|
|
|
if !self._isCocosReady { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let cocosAppController = self.ufw?.appController() as? CocosAppController |
|
|
|
|
//todo |
|
|
|
|
//let cocosAppController = self.ufw?.appController() as? CocosAppController |
|
|
|
|
// let cocosAppController = nil |
|
|
|
|
let application = UIApplication.shared |
|
|
|
|
|
|
|
|
|
if notification?.name == UIApplication.willResignActiveNotification { |
|
|
|
|
cocosAppController?.applicationWillResignActive(application) |
|
|
|
|
self.ufw?.applicationWillResignActive(application) |
|
|
|
|
} else if notification?.name == UIApplication.didEnterBackgroundNotification { |
|
|
|
|
cocosAppController?.applicationDidEnterBackground(application) |
|
|
|
|
// self.ufw?.applicationDidEnterBackground(application) |
|
|
|
|
} else if notification?.name == UIApplication.willEnterForegroundNotification { |
|
|
|
|
cocosAppController?.applicationWillEnterForeground(application) |
|
|
|
|
// self.ufw?.applicationWillEnterForeground(application) |
|
|
|
|
} else if notification?.name == UIApplication.didBecomeActiveNotification { |
|
|
|
|
cocosAppController?.applicationDidBecomeActive(application) |
|
|
|
|
self.ufw?.applicationDidBecomeActive(application) |
|
|
|
|
} else if notification?.name == UIApplication.willTerminateNotification { |
|
|
|
|
cocosAppController?.applicationWillTerminate(application) |
|
|
|
|
self.ufw?.applicationWillTerminate(application) |
|
|
|
|
} else if notification?.name == UIApplication.didReceiveMemoryWarningNotification { |
|
|
|
|
cocosAppController?.applicationDidReceiveMemoryWarning(application) |
|
|
|
|
} |
|
|
|
|
// self.ufw?.applicationDidReceiveMemoryWarning(application) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Listener for app lifecycle eventa |
|
|
|
|
func listenAppState() { |
|
|
|
|
for name in [ |
|
|
|
|
|
|
|
|
|
UIApplication.didBecomeActiveNotification, |
|
|
|
|
UIApplication.didEnterBackgroundNotification, |
|
|
|
|
UIApplication.willTerminateNotification, |
|
|
|
@ -202,19 +252,22 @@ var sharedApplication: UIApplication? |
|
|
|
|
} |
|
|
|
|
// Pause cocos player |
|
|
|
|
func pause() { |
|
|
|
|
self.ufw?.pause(true) |
|
|
|
|
//todo |
|
|
|
|
// self.ufw?.pause(true) |
|
|
|
|
self._isCocosPaused = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Resume cocos player |
|
|
|
|
func resume() { |
|
|
|
|
self.ufw?.pause(false) |
|
|
|
|
//todo |
|
|
|
|
//self.ufw?.pause(false) |
|
|
|
|
self._isCocosPaused = false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Unoad cocos player |
|
|
|
|
func unload() { |
|
|
|
|
self.ufw?.unloadApplication() |
|
|
|
|
//todo |
|
|
|
|
//self.ufw?.unloadApplication() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func isCocosLoaded() -> Bool { |
|
|
|
@ -227,14 +280,16 @@ var sharedApplication: UIApplication? |
|
|
|
|
|
|
|
|
|
// Quit cocos player application |
|
|
|
|
func quit() { |
|
|
|
|
self.ufw?.quitApplication(0) |
|
|
|
|
//todo |
|
|
|
|
//self.ufw?.quitApplication(0) |
|
|
|
|
self._isCocosLoaded = false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Post message to cocos |
|
|
|
|
func postMessageToCocos(gameObject: String?, cocosMethodName: String?, cocosMessage: String?) { |
|
|
|
|
if self.cocosIsInitiallized() { |
|
|
|
|
self.ufw?.sendMessageToGO(withName: gameObject, functionName: cocosMethodName, message: cocosMessage) |
|
|
|
|
//todo |
|
|
|
|
//self.ufw?.sendMessageToGO(withName: gameObject, functionName: cocosMethodName, message: cocosMessage) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|