Due to the similar demand recently, I have studied it in the past two days.

First, preparation

Unity export xcode project

Two, start to flip

1. Import the following files from Unity3D into the project directory

  • Data
  • Classes
  • MapFileParser.sh
  • Libraries
  • MapFileParser Executes the file

Note: The Data file import type is Creat folde References Classes mapFileParser. sh Libraries MapFileParser execute file import through Creat Groups

The following figure shows the import

2. Delete the reference

  • Delete the Libraries->libil2cpp reference option asRemove Refernces
  • Target -> Build Phases -> DynamicLibEngineAPI removed. Undefined symbols for Architecture arm64: Undefined symbols for Architecture arm64:… in … from:DynamicLibEngineAPI.o

3. Set classes->prefix. PCH to add the PCH path

4. The main.m operation

Copy the code from classes in main.mm into the project’s main.m and change the suffix to mm as well

UIApplicationMain(argc, argv, nil[NSString stringWithUTF8String: AppControllerClassName]);
Copy the code

Modified to

UIApplicationMain(argc, argv, nil[NSString stringWithUTF8String: @"AppDelegate"]);
Copy the code

May modify the AppDelegate

AppDelegate.h

@property (strong.nonatomic) UIWindow *window;
@property (strong.nonatomic) UIWindow *unityWindow;
@property (strong.nonatomic) UnityAppController *unityController; - (void)StartUnity; / / open the unity
- (void)hideUnityWindow; / / pause

Copy the code

AppDelegate.m

#import "AppDelegate.h"
#import "SRViewController.h"
#import "UnityAppController.h"

@interface AppDelegate(a)

@end

@implementation AppDelegate- (UIWindow *)unityWindow{
    UIWindow *window = UnityGetMainWindow();
    returnwindow; } - (void)showUnityWindow{
    UnityPause(false); } - (void)hideUnityWindow{
    UnityPause(true); } - (void)StartUnity {
    [_unityController applicationDidBecomeActive:[UIApplication sharedApplication]];
    [self showUnityWindow];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.unityController = [[UnityAppController alloc] init];
    [_unityController application:application didFinishLaunchingWithOptions:launchOptions];
    
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor redColor];
    SRViewController *vc = [[SRViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];

    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];

    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    [_unityController applicationWillResignActive:application];
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    [_unityController applicationDidEnterBackground:application];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    [_unityController applicationWillEnterForeground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [_unityController applicationDidBecomeActive:application];
}


- (void)applicationWillTerminate:(UIApplication *)application {
    [_unityController applicationWillTerminate:application];
}
Copy the code

6 UnityAppController.h

will

inline UnityAppController* GetAppController()
{
    return _UnityAppController;
}
Copy the code

Modified to

#import "AppDelegate.h"
inline UnityAppController*  GetAppController()
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    return delegate.unityController;
}
Copy the code

7 calls

@implementation SRViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @ "123";
    self.view.backgroundColor = [UIColor greenColor];
    
    
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(50.400.100.50)];
    [btn addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@ "start" forState:UIControlStateNormal];
    btn.tag = 1;
    [self.view addSubview:btn];
    
    
    UIButton * btn2 = [[UIButton alloc] initWithFrame:CGRectMake(50.450.100.50)];
    [btn2 addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
    btn2.tag = 2;
    [btn2 setTitle:@ "end" forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    
}

- (void)test:(UIButton *) btn{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    delegate.unityWindow.frame = CGRectMake(0.0.100.200);
    
    if (btn.tag == 1) {[self.view addSubview:delegate.unityWindow];
        delegate.unityWindow.hidden = NO;
        [delegate StartMyUnity];
    }else {
        [delegate hideUnityWindow];
        delegate.unityWindow.hidden = YES; }}Copy the code

Add Framework and Run Script

Add these two items in the same way as in the Unity project

  • Note whether Status is Required or Optional
  • Dylib is found by adding Other -> command+shift+G search for /usr/liblibiconv.2.dylibadd

Other important Settings

  • Enable BitCode = NO;
  • Header/Library Search Paths are consistent with Unity
  • Framework Search Paths is consistent with Unity
  • Other Linker Flags are consistent with Unity
    • All_load If the project has this remember to remove and unity is not compatible
  • Supported Platforms export projects consistent with Unity best true machine
  • Other C Flags, Other C++ Flags, and Untiy must be consistent
  • C Language Dialect Remain the same
  • Add user-defined (Target ->Build Settings -> Plus sign to the right of Leveles)
    • GCC_THUMB_SUPPORT = NO
    • PROVISIONING_PROFILE
    • UNITY_RUNTIME_VERSION = Unity version number
    • UNITY_SCRIPTING_BACKEND = il2cpp