Skip to content

Commit 487c702

Browse files
author
Martin Straub
committed
got rid of main.swift file => just call unity_init from AppDelegate, minor refactorings, tested with unity 5.5.2 on Xcode 8.3.2
1 parent 2ba080f commit 487c702

File tree

5 files changed

+7
-63
lines changed

5 files changed

+7
-63
lines changed

README.md

+3-44
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ script and the project import are directly derieved from his work. The video
1111
he made in the provided link is worth watching.
1212

1313
This covers Unity 5+. At the time of this writing this has been
14-
successfully used with Unity `5.2.2f1` and `Swift 3.0` under `Xcode 8.0`.
14+
successfully used with Unity `5.5.2f1` and `Swift 3.1` under `Xcode 8.3.2`.
1515

1616
This works with storyboards.
1717

@@ -91,7 +91,6 @@ which is not diffiucilt, it's just time consuming given the number of files.
9191
- Clean up your unity project
9292
- Add the `objc` folder in this repo with the new custom unity init and obj-c bridging header
9393
- Rename `main` in `main.mm` to anything else
94-
- Alter the application delegate and create a main.swift file.
9594
- Wrap the UnityAppController into your application delegate
9695
- Adjust the `GetAppController` function in `UnityAppController.h`
9796
- Go bananas, you did it! Add the unity view wherever you want!
@@ -125,7 +124,7 @@ You can also adjust your
125124
UNITY_RUNTIME_VERSION
126125
```
127126

128-
If you are not using `5.2.2f1`.
127+
If you are not using `5.5.2f1`.
129128

130129

131130
#### Add a new `run script` build phase
@@ -202,47 +201,6 @@ Anyway, we need to rename this function to anything but `main`:
202201
int main_unity_default(int argc, char* argv[])
203202
```
204203

205-
#### Alter the swift application delegate and create a main.swift file
206-
207-
We have to get our initialization point done however, so we need some small additions/changes.
208-
209-
Open your `AppDelegate.swift` you will see this at the top of the file:
210-
211-
```swift
212-
@UIApplicationMain
213-
class AppDelegate: UIResponder, UIApplicationDelegate {
214-
```
215-
216-
All we are going to do is remove `@UIApplicationMain` so we
217-
are left with the following after we are done:
218-
219-
```swift
220-
class AppDelegate: UIResponder, UIApplicationDelegate {
221-
```
222-
223-
Now we need to let xcode know where our new main is. Go ahead and create
224-
a new swift file called `main.swift`. Paste this into it:
225-
226-
```swift
227-
import Foundation
228-
import UIKit
229-
230-
// overriding @UIApplicationMain
231-
// http://stackoverflow.com/a/24021180/1060314
232-
233-
custom_unity_init(CommandLine.argc, CommandLine.unsafeArgv)
234-
let newUnsafeArgv = UnsafeMutableRawPointer( CommandLine.unsafeArgv ).bindMemory( to: UnsafeMutablePointer<Int8>.self, capacity: Int( CommandLine.argc ) )
235-
UIApplicationMain( CommandLine.argc, newUnsafeArgv , NSStringFromClass( UIApplication.self ), NSStringFromClass( AppDelegate.self ) )
236-
```
237-
238-
Assuming your bridging header is properly registered, xcode will NOT be
239-
complaining about `custom_unity_init`. If it is, something is wrong with the
240-
bridging header registration. Go check that out.
241-
242-
Note that if your `AppDelegate` is NOT called `AppDelegate` you will need to update
243-
the last argument above in `UIApplicationMain(<argc>, <argv>, <UIApplication>, <here>)`
244-
to be whatever yours is called.
245-
246204
#### Wrap the UnityAppController into your application delegate
247205

248206
We are taking away control from the unity generated application delegate, we
@@ -278,6 +236,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
278236

279237
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
280238
self.application = application
239+
unity_init(CommandLine.argc, CommandLine.unsafeArgv)
281240
currentUnityController = UnityAppController()
282241
currentUnityController!.application(application, didFinishLaunchingWithOptions: launchOptions)
283242

Unity.xcconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Created by Adam Venturella on 10/28/15.
55
// Settings from http://www.the-nerd.be/2015/08/20/a-better-way-to-integrate-unity3d-within-a-native-ios-application/
66

7-
UNITY_RUNTIME_VERSION = 5.5.0f3;
7+
UNITY_RUNTIME_VERSION = 5.5.2f1;
88
UNITY_SCRIPTING_BACKEND = il2cpp;
99
UNITY_IOS_EXPORT_PATH = /Path/To/Your/Unity/iOS/Build;
1010
GCC_PREFIX_HEADER = $(UNITY_IOS_EXPORT_PATH)/Classes/Prefix.pch;

objc/UnityBridge.h

+1-16
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,5 @@
1010
#import <UIKit/UIKit.h>
1111
#import "UnityUtils.h"
1212
#import "UnityAppController.h"
13-
#import "Unity/UnityInterface.h"
13+
#import "UnityInterface.h"
1414
#endif /* UnityBridge_h */
15-
16-
17-
18-
/**
19-
* Replacement Function for UnityAppController.h
20-
*
21-
*/
22-
/*
23-
NS_INLINE UnityAppController* GetAppController(){
24-
25-
NSObject<UIApplicationDelegate>* delegate = [UIApplication sharedApplication].delegate;
26-
UnityAppController* currentUnityController = (UnityAppController *)[delegate valueForKey:@"currentUnityController"];
27-
return currentUnityController;
28-
}
29-
*/

objc/UnityUtils.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
#define UnityUtils_h
99

1010

11-
void custom_unity_init(int argc, char* argv[]);
11+
void unity_init(int argc, char* argv[]);
1212

1313
#endif /* UnityUtils_h */

objc/UnityUtils.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
void UnityInitTrampoline();
2626

2727

28-
extern "C" void custom_unity_init(int argc, char* argv[])
28+
extern "C" void unity_init(int argc, char* argv[])
2929
{
3030
@autoreleasepool
3131
{

0 commit comments

Comments
 (0)