unity与原生iOS项目的整合(一)

      目录介绍

1、软件环境

      unity3D:5.4.1f1

      xcode:8.3.3

2、准备工作

     2.1、新建一个iOS工程,简称iOS原生工程

     2.2、unity导出的iOS工程,简称Unity工程 

             Unity工程使用之前一定要清楚导出的时候的设备选择,注意Unity导出工程时设置的参数项注意项

  Target  SDK 的设置关系到iOS工程测试和编译的环境

3、集成步骤

3.1  添加Unity工程中的文件到iOS工程中

(1)将Unity工程下的3个文件夹Classes、Libraries、Data ,两个文件 MapFileParser 、 MapFileParser.sh添加至iOS工程中,

添加时注意选择Copy items if needed选项,Classes和Libraries文件夹选择 Create groups,而Data文件夹选择Create folder references选项。

       添加完Unity文件夹之后的工程结构

添加完毕以后可以把Unity文件夹中的一些无关文件清理掉,它们会影响编译速度,删除时选择Remove Reference

  • Classes->Native文件夹下的*.h文件
  • Libraries文件夹下的libil2cpp文件夹

  3.2添加相关框架

   需要添加哪些framework可以参见Unity工程的TARGETS -> General -> Linked Frameworks and Libraries,如图所示:

   

        

3.3. 配置Build Settings相关选项
(1) Enable Bitcode -> NO
(2) Other Linker Flags
添加-weak_framework CoreMotion -weak-lSystem
 
 3.4    Header Search Paths
  • "$(SRCROOT)"
  • "$(SRCROOT)/Classes"
  • $(SRCROOT)/Classes/Native
  • $(SRCROOT)/Libraries/libil2cpp/include
  • $(SRCROOT)/Libraries
注意:根据工程实际路径来配置,$(SRCROOT)是指.xcproj文件所在的路径,如果此处路径配置不正确,可能会出现诸如il2cpp-config.h file not found的编译错误。

 3.5    Library Search Paths

  • "$(SRCROOT)"
  • "$(SRCROOT)/Libraries"

3.6 Custom Compiler Flags -> Other C Flags    添加    -DINIT_SCRIPTING_BACKEND=1

3.7 C Language Dialect选择C99 [-std=c99] 

3.8  新建pch文件:PrefixHeader.pch, 设置Prefix Header的路径(注意文件导入的位置)

3.9Precompile Prefix Header选择YES

3.10 C++ Language Dialect选择C++11 [-std=c++11]

3.11Enable C++ Runtime Types选择NO

3.12Warnings-Objective C->Overriding Deprecated Objective-C Methods选择YES

3.13Warnings-Objective C->Unintentional Root Classes选择YES

3.14 User-Defined

添加如下选项:

  • GCC_THUMB_SUPPORT->NO
  • GCC_USE_INDIRECT_FUNCTION_CALLS->NO
  • UNITY_RUNTIME_VERSION->5.4.0f3
  • UNITY_SCRIPTING_BACKEND->il2cpp

4、pch文件内容替换

Classes文件夹下的Prefix.pch文件中的内容全部复制粘贴到PrefixHeader.pch文件的

#define PrefixHeader_pch
#endif /* PrefixHeader_pch */
之间,并且引入UnityAppController.h文件如图所示:



5. Supporting Files下面的main.m

  • 重命名成main.mm
  • Classes文件夹下的main.mm文件中的全部内容复制到Supporting Files文件夹下的main.mm文件中,并进行修改:

    //const char* AppControllerClassName = @”UnityAppController”;
    const char* AppControllerClassName = “AppDelegate”;

  • Build Phases中移除Classes下main.mm

 

6. UnityAppController.h
   做如下修改

//inline UnityAppController*GetAppController()

//{

// return (UnityAppController*)[UIApplication sharedApplication].delegate;

//}

#import "AppDelegate.h"

inlineUnityAppController* GetAppController()

{

    AppDelegate *delegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;

    return delegate.unityController;

}


7、 AppDelegate.hAppDelegate.m


(1)AppDelegate.h文件的修改

#import <UIKit/UIKit.h>


@interface AppDelegate :UIResponder <UIApplicationDelegate>

@property (strong,nonatomic)UIWindow *window;

@property (strong,nonatomic)UIWindow *unityWindow;


@property (nonatomic,strong)UnityAppController *unityController;


- (void)showUnityWindow;

- (void)hideUnityWindow;

@end

(2)AppDelegate.m 文件的修改

#import "AppDelegate.h"


@interfaceAppDelegate ()


@end


@implementation AppDelegate


- (UIWindow *)unityWindow {

    returnUnityGetMainWindow();

}


- (void)showUnityWindow {

    [self.unityWindowmakeKeyAndVisible];

}


- (void)hideUnityWindow {

    [self.windowmakeKeyAndVisible];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

    self.window.backgroundColor = [UIColorredColor];

    

    self.unityController = [[UnityAppControlleralloc]init];

    

    [self.unityControllerapplication:applicationdidFinishLaunchingWithOptions:launchOptions];

    

    returnYES;

}


- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

    [self.unityControllerapplicationWillResignActive:application];

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    

    [self.unityControllerapplicationDidEnterBackground:application];

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

    [self.unityControllerapplicationWillEnterForeground:application];

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    [self.unityControllerapplicationDidBecomeActive:application];

}

- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    

    [self.unityControllerapplicationWillTerminate:application];

}

@end

8、ViewController.m

#import "ViewController.h"

#import "AppDelegate.h"

staticid object;

@interfaceViewController ()<UIAlertViewDelegate>

@property (nonatomic,strong)UIButton *showUnityButton;

@end

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    object =self;

    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColorblueColor];

    self.showUnityButton = [UIButtonbuttonWithType:UIButtonTypeSystem];

    [self.showUnityButtonsetTitle:@"Show Unity"forState:UIControlStateNormal];

    self.showUnityButton.frame =CGRectMake(0,20,100,44);

    self.showUnityButton.center = self.view.center;

    [self.showUnityButtonaddTarget:selfaction:@selector(showUnityView:)forControlEvents:UIControlEventTouchUpInside];

   [self.viewaddSubview:self.showUnityButton];

}

- (void)showUnityView:(id)sender {

    [(AppDelegate *)[UIApplicationsharedApplication].delegateshowUnityWindow];

    UnitySendMessage("Main Camera","OnClick","交互");

}


@end


文章参考主要网址:http://qingqinghebiancao.github.io/2016/09/07/Unity集成到iOS本地工程中/

      



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UnityiOS的集成中,互斥锁问题通常是由于使用了多线程导致的。iOS中的UI操作必须在主线程(也称为UI线程)中执行,而Unity中的游戏逻辑通常是在其他线程中执行的。因此,在UnityiOS的集成中,需要使用互斥锁来确保UI操作和游戏逻辑的同步执行。 具体来说,可以使用iOS中的NSLock类来实现互斥锁。在Unity中,可以通过以下方式将NSLock类导入到Unity项目中: 1. 在Unity项目中创建一个Objective-C类,并将其命名为“iOSBridge”。 2. 在iOSBridge.h文件中添加以下代码: ``` #import <Foundation/Foundation.h> @interface iOSBridge : NSObject - (void)lock; - (void)unlock; @end ``` 3. 在iOSBridge.m文件中添加以下代码: ``` #import "iOSBridge.h" @implementation iOSBridge { NSLock *_lock; } - (id)init { if (self = [super init]) { _lock = [[NSLock alloc] init]; } return self; } - (void)lock { [_lock lock]; } - (void)unlock { [_lock unlock]; } @end ``` 4. 在Unity中创建一个C#类,并将其命名为“iOSBridge”。 5. 在iOSBridge.cs文件中添加以下代码: ``` using System.Runtime.InteropServices; public class iOSBridge { [DllImport("__Internal")] private static extern void lock(); [DllImport("__Internal")] private static extern void unlock(); public static void Lock() { lock(); } public static void Unlock() { unlock(); } } ``` 6. 在需要使用互斥锁的地方,可以通过调用iOSBridge类的Lock和Unlock方法来实现互斥锁的功能。 需要注意的是,在使用互斥锁的时候,要确保锁的范围不要太大,否则可能会导致UI卡顿。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值