ar_tourism_flutter_unity/ios/Runner/AppDelegate.swift
2025-05-16 10:43:23 +08:00

88 lines
2.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import UIKit
import Flutter
import UnityFramework
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
// Unity
var unityFramework: UnityFramework?
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Flutter
GeneratedPluginRegistrant.register(with: self)
// Unity
let registry = self.registrar(forPlugin: "UnityPlugin")
UnityPlugin.register(with: registry)
// Unity
initUnityFramework()
// Unity
setupUnityMessageHandlers()
// Unity
registerUnityViewFactory()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
// Unity
func initUnityFramework() {
let unityLoader = UnityFrameworkLoader()
unityFramework = unityLoader.loadUnityFramework()
if unityFramework != nil {
// Unity
unityFramework?.setDataBundleId("com.unity3d.framework")
unityFramework?.register(self)
unityFramework?.runEmbedded(
withArgc: CommandLine.argc,
argv: CommandLine.unsafeArgv,
appLaunchOpts: nil
)
} else {
print("无法加载Unity框架")
}
}
// Unity
private func registerUnityViewFactory() {
let controller = self.window.rootViewController as? FlutterViewController
if let registrar = controller?.registrar(forPlugin: "UnityPlugin") {
let factory = UnityViewFactory(registrar: registrar, unityFramework: unityFramework)
registrar.register(factory, withId: "com.yourcompany.unity_flutter/unityview")
}
}
// Unity
private func setupUnityMessageHandlers() {
// Unity
let controller = self.window.rootViewController as? FlutterViewController
let channel = FlutterMethodChannel(name: "com.example.ar_tourism_flutter_unity.unity", binaryMessenger: controller!.binaryMessenger)
// Flutter->Unity
// UnityPlugin
}
// Unity
private func openUnityScene(sceneName: String) {
// Unity
sendMessageToUnity(gameObject: "GameManager", methodName: "LoadScene", message: sceneName)
}
// Unity
private func closeUnity() {
unityFramework?.pause(true)
// Unity
}
// Unity
private func sendMessageToUnity(gameObject: String, methodName: String, message: String) {
unityFramework?.sendMessageToGO(withName: gameObject, functionName: methodName, message: message)
}
}