Skip to content

Commit a40493c

Browse files
author
Adam Venturella
committed
updates
1 parent 354503a commit a40493c

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ which is not diffiucilt, it's just time consuming given the number of files.
5050
- Add the `objc` folder in this repo with the new custom unity init and obj-c bridging header
5151
- Rename `main` in `main.mm` to anything else
5252
- Alter the application delegate and cerate a main.swift file.
53+
- Wrap the UnityAppController into your application delegate
5354
- Adjust the `GetAppController` function in `UnityAppController.h`
5455

5556

@@ -191,6 +192,63 @@ Note that if your `AppDelegate` is NOT called `AppDelegate` you will need to upd
191192
the last argument above in `UIApplicationMain(<argc>, <argv>, <UIApplication>, <here>)`
192193
to be whatever yours is called.
193194

195+
#### Wrap the UnityAppController into your application delegate
196+
197+
We are taking away control from the unity generated application delegate, we
198+
need to act as a proxy for it in our `AppDelegate`.
199+
200+
First add the following variable to your `AppDelegate`
201+
202+
```swift
203+
var currentUnityController: UnityAppController!
204+
```
205+
Now we need to initialize and proxy through the calls to the `UnityAppController`.
206+
All said and done you will be left with the following:
207+
208+
```swift
209+
//
210+
// AppDelegate.swift
211+
//
212+
// Created by Adam Venturella on 10/28/15
213+
//
214+
215+
import UIKit
216+
217+
class AppDelegate: UIResponder, UIApplicationDelegate {
218+
219+
var window: UIWindow?
220+
var currentUnityController: UnityAppController!
221+
222+
223+
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
224+
currentUnityController = UnityAppController()
225+
currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)
226+
return true
227+
}
228+
229+
func applicationWillResignActive(application: UIApplication) {
230+
currentUnityController.applicationWillResignActive(application)
231+
}
232+
233+
func applicationDidEnterBackground(application: UIApplication) {
234+
currentUnityController.applicationDidEnterBackground(application)
235+
}
236+
237+
func applicationWillEnterForeground(application: UIApplication) {
238+
currentUnityController.applicationWillEnterForeground(application)
239+
}
240+
241+
func applicationDidBecomeActive(application: UIApplication) {
242+
currentUnityController.applicationDidBecomeActive(application)
243+
}
244+
245+
func applicationWillTerminate(application: UIApplication) {
246+
currentUnityController.applicationWillTerminate(application)
247+
}
248+
}
249+
250+
```
251+
194252
#### Adjust the `GetAppController` function in `UnityAppController.h`
195253

196254
Locate the file `UnityAppController.h` in the xcode group `Unity/Classes/`
@@ -213,4 +271,16 @@ Comment that out. You will end up with this:
213271
//}
214272
```
215273

274+
Now we need to add a new version of this function:
275+
276+
```objc
277+
NS_INLINE UnityAppController* GetAppController()
278+
{
279+
280+
NSObject<UIApplicationDelegate>* delegate = [UIApplication sharedApplication].delegate;
281+
UnityAppController* currentUnityController = (UnityAppController *)[delegate valueForKey:@"currentUnityController"];
282+
return currentUnityController;
283+
}
284+
```
285+
216286
[www.the-nerd.be]: http://www.the-nerd.be/2015/08/20/a-better-way-to-integrate-unity3d-within-a-native-ios-application/ "The Nerd"

objc/UnityBridge.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
#define UnityBridge_h
1010

1111
#import "UnityUtils.h"
12+
#import "UnityAppController.h"
1213
#import "Unity/UnityInterface.h"
1314
#endif /* UnityBridge_h */
1415

1516

1617

18+
/**
19+
* Replacement Function for UnityAppController.h
20+
*
21+
*/
1722
/*
1823
NS_INLINE UnityAppController* GetAppController(){
1924

0 commit comments

Comments
 (0)