Created
February 23, 2022 08:42
-
-
Save eunjin3786/3dcb2216a7174da4acbda19cc7331a9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UnityManager { | |
static let shared = UnityManager() | |
private let dataBundleId: String = "com.unity3d.framework" | |
private let frameworkPath: String = "/Frameworks/UnityFramework.framework" | |
private var ufw: UnityFramework? | |
private init() {} | |
func launchUnity() { | |
let isInitialized = self.ufw?.appController() != nil | |
if isInitialized { | |
self.ufw?.showUnityWindow() | |
} else { | |
guard let ufw = self.loadUnityFramework() else { return } | |
self.ufw = ufw | |
ufw.setDataBundleId(dataBundleId) | |
ufw.runEmbedded( | |
withArgc: CommandLine.argc, | |
argv: CommandLine.unsafeArgv, | |
appLaunchOpts: nil | |
) | |
} | |
} | |
private func loadUnityFramework() -> UnityFramework? { | |
let bundlePath: String = Bundle.main.bundlePath + frameworkPath | |
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment