Last active
October 23, 2020 05:28
-
-
Save n0mimono/e5e3aee4440a46b483a3a522e486c770 to your computer and use it in GitHub Desktop.
Unity-as-a-Library Sample Code
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
import UIKit | |
import UnityFramework | |
@main | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
func unityFramewokLoad() -> UnityFramework? { | |
let bundlePath = "\(Bundle.main.bundlePath)/Frameworks/UnityFramework.framework" | |
let bundle = Bundle(path: bundlePath) | |
if let bundle = bundle, !bundle.isLoaded { | |
bundle.load() | |
} | |
let ufw = bundle?.principalClass?.getInstance() | |
if ufw?.appController() == nil { | |
var header = _mh_execute_header | |
ufw?.setExecuteHeader(&header) | |
} | |
return ufw | |
} | |
let ufw = unityFramewokLoad() | |
ufw?.setDataBundleId("com.unity3d.framework") | |
ufw?.runEmbedded(withArgc: CommandLine.argc, argv: CommandLine.unsafeArgv, appLaunchOpts: launchOptions) | |
return true | |
} | |
} |
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
#if UNITY_IOS | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.Build; | |
using UnityEditor.Build.Reporting; | |
using UnityEditor.Callbacks; | |
using UnityEditor.iOS.Xcode; | |
public class IOSPostProcessor : IPostprocessBuildWithReport | |
{ | |
public void OnPostprocessBuild(BuildReport report) | |
{ | |
var summary = report.summary; | |
var outputPath = summary.outputPath; | |
var projectPath = outputPath + "/Unity-iPhone.xcodeproj/project.pbxproj"; | |
var pbx = new PBXProject(); | |
pbx.ReadFromFile(projectPath); | |
var guidTarget = pbx.GetUnityFrameworkTargetGuid(); | |
var guidHeader = pbx.FindFileGuidByProjectPath("Libraries/Plugins/iOS/UnityPlayerToIOS.h"); | |
pbx.AddPublicHeaderToBuild(guidTarget, guidHeader); | |
var guidData = pbx.FindFileGuidByProjectPath("Data"); | |
var guidResPhase = pbx.GetResourcesBuildPhaseByTarget(guidTarget); | |
pbx.AddFileToBuildSection(guidTarget, guidResPhase, guidData); | |
pbx.SetBuildProperty(guidTarget, "BITCODE_GENERATION_MODE", "bitcode"); | |
pbx.WriteToFile(projectPath); | |
} | |
public int callbackOrder => 0; | |
} | |
#endif |
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
#if !UNITY_EDITOR && UNITY_IOS | |
using System.Runtime.InteropServices; | |
#endif | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class NewBehaviourScript : MonoBehaviour | |
{ | |
#if !UNITY_EDITOR && UNITY_IOS | |
[DllImport("__Internal")] | |
static extern void sendMessage(string msg); | |
public void NativeMessage(string msg) | |
{ | |
sendMessage(msg); | |
} | |
#else | |
public void NativeMessage(string msg) | |
{ | |
Debug.Log(msg); | |
} | |
#endif | |
} |
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
#ifndef UnityPlayerToIOS_h | |
#define UnityPlayerToIOS_h | |
@protocol UnityCallback <NSObject> | |
@optional | |
@required | |
- (void)receiveMessage: (NSString *)msg; | |
@end | |
@interface UnityPlayerToIOS : NSObject | |
@property (class, nonatomic) id <UnityCallback> receiver; | |
+ (void)sendMessage: (NSString *)msg; | |
@end | |
#endif |
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
#import "UnityPlayerToIOS.h" | |
static id <UnityCallback> _receiver = nil; | |
@implementation UnityPlayerToIOS | |
+ (id <UnityCallback>) receiver { | |
return _receiver; | |
} | |
+ (void)setReceiver: (id <UnityCallback>)receiver { | |
_receiver = receiver; | |
} | |
+ (void)sendMessage: (NSString *)msg { | |
if (_receiver) { | |
[_receiver receiveMessage:msg]; | |
} | |
} | |
@end | |
extern "C" { | |
void sendMessage(const char *msg) { | |
[UnityPlayerToIOS sendMessage:[NSString stringWithCString: msg encoding:NSUTF8StringEncoding]]; | |
} | |
} |
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
import UIKit | |
import UnityFramework | |
import RxSwift | |
import RxCocoa | |
class ViewController: UIViewController { | |
let disposeBag = DisposeBag() | |
@IBOutlet weak var unityParentView: UIView! | |
@IBOutlet weak var button: UIButton! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
guard let ufw = UnityFramework.getInstance() else { return } | |
guard let unityView = ufw.appController()?.rootView else { return } | |
guard let unityParentView = unityParentView else { return } | |
unityParentView.addSubview(unityView) | |
unityView.bounds = unityParentView.bounds | |
ufw.setCallback(self) | |
button.rx.tap | |
.bind { [weak self] _ in | |
// something | |
} | |
.disposed(by: disposeBag) | |
} | |
} | |
extension ViewController : UnityCallback { | |
func receiveMessage(_ msg: String!) { | |
// something | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment