unity 整合到原生iOS項(xiàng)目(swift 2.3)

老司機(jī)走開

網(wǎng)上有很多unity集成到Objective-C的教程, 出門左拐,度娘谷歌都有,這里就不重復(fù)介紹了

公司項(xiàng)目需求

現(xiàn)有項(xiàng)目swift 2.3 + unity
虐死人啊

網(wǎng)上教程有篇老外的

  1. 你項(xiàng)目不用cocoapods
  2. 你看得懂作者思路

just go ->
How to use Unity 3D within an iOS app

使用cocoapods,直接越坑

廢話不多說,下面步驟:

1. unity導(dǎo)出工程

沒啥難度,配置好就行, 注意 : 設(shè)備方向最好是Auto Rotation, 再由iOS端控制方向,會(huì)減少不少bug
Auto Rotation

2. 添加Unity.xcconfig 文件到項(xiàng)目路徑

文件地址, blitzagency提供

Unity.xcconfig

按圖步驟,設(shè)置project的配置,Debug和Release改成Unity就行

添加UnityBridge.h, UnityUtils.h, UnityUtils.mm 到項(xiàng)目

//更改整個(gè)extern "C" int custom_unity_init 里面代碼為:
extern "C" int custom_unity_init(int argc, char* argv[])
{
    @autoreleasepool
    {
        UnityInitTrampoline();
        UnityParseCommandLine(argc, argv);
        
        RegisterMonoModules();
        NSLog(@"-> registered mono modules %p\n", &constsection);
        RegisterFeatures();
        
        // iOS terminates open sockets when an application enters background mode.
        // The next write to any of such socket causes SIGPIPE signal being raised,
        // even if the request has been done from scripting side. This disables the
        // signal and allows Mono to throw a proper C# exception.
        std::signal(SIGPIPE, SIG_IGN);
        
        //        UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:"AppControllerClassName"]);
    }
    
    return 0;
}

3. 將unity導(dǎo)出文件添加到項(xiàng)目中

第一步的導(dǎo)出工程只需要Classes,Data,Libraries,這三個(gè)
新建文件夾ios_ui_animation, 拷貝進(jìn)去


新建文件夾

4. 更改工程配置

配置如圖

更改 UNITY_RUNTIME_VERSION 為你unity工程版本號(hào),我的是 5.3.1f1

5. 添加run script 到 build phase

添加:

rm -rf "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Data";
cp -Rf "$UNITY_IOS_EXPORT_PATH/Data" "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Data";

添加步驟

6. 清理unity文件

把unity導(dǎo)出項(xiàng)目Classes,Data,Libraries文件拷貝到ios_ui_animation


拷貝

Classes, Libraries 設(shè)置 Create groups


設(shè)置

Data 設(shè)置 Create folder
設(shè)置

目錄結(jié)構(gòu)

7. 刪除引用

先刪除引用libraries里面的libil2cpp文件夾,然后再刪除Classes里面的Native文件夾里面的所有.h文件

都是刪除引用
刪除

8. 變更unity里方法,引用等

找到main.mm

注釋這個(gè)import
//#import "UnitySubAppDelegate.h"
//方法替換一下
int main(int argc, char* argv[])
{
    @autoreleasepool
    {
        UnityInitTrampoline();
        UnityParseCommandLine(argc, argv);

        RegisterMonoModules();
        NSLog(@"-> registered mono modules %p\n", &constsection);
        RegisterFeatures();

        // iOS terminates open sockets when an application enters background mode.
        // The next write to any of such socket causes SIGPIPE signal being raised,
        // even if the request has been done from scripting side. This disables the
        // signal and allows Mono to throw a proper C# exception.
        std::signal(SIGPIPE, SIG_IGN);

        //UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:AppControllerClassName]);
        UIApplicationMain(argc, argv, nil, NSStringFromClass([UnitySubAppDelegate class]));
    }

    return 0;
}
//替換為
int main_unity_default(int argc, char* argv[])
{
    @autoreleasepool
    {
        UnityInitTrampoline();
        UnityParseCommandLine(argc, argv);
        
        RegisterMonoModules();
        NSLog(@"-> registered mono modules %p\n", &constsection);
        RegisterFeatures();
        
        // iOS terminates open sockets when an application enters background mode.
        // The next write to any of such socket causes SIGPIPE signal being raised,
        // even if the request has been done from scripting side. This disables the
        // signal and allows Mono to throw a proper C# exception.
        std::signal(SIGPIPE, SIG_IGN);
        
        //UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:AppControllerClassName]);
        //        UIApplicationMain(argc, argv, nil, NSStringFromClass([UnitySubAppDelegate class]));
        UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:AppControllerClassName]);
    }
    
    return 0;
}
//找到UnityAppController.h
//最上面添加
@class UnityViewControllerBase;

//注釋此方法
//inline UnityAppController*    GetAppController()
//{
//  return (UnityAppController*)[UIApplication sharedApplication].delegate;
//}

//替換為此方法
NS_INLINE UnityAppController* GetAppController()
{
    NSObject<UIApplicationDelegate>* delegate = [UIApplication sharedApplication].delegate;
    UnityAppController* currentUnityController = (UnityAppController *)[delegate valueForKey:@"currentUnityController"];
    return currentUnityController;
}

9. 自己swift項(xiàng)目

//打開AppDelegate.swift

//注釋@UIApplicationMain, 讓swift從main.swift啟動(dòng)
//@UIApplicationMain

//項(xiàng)目添加如下代碼
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var currentUnityController: UnityAppController!


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        currentUnityController = UnityAppController()
        currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        currentUnityController.applicationWillResignActive(application)
    }

    func applicationDidEnterBackground(application: UIApplication) {
        currentUnityController.applicationDidEnterBackground(application)
    }

    func applicationWillEnterForeground(application: UIApplication) {
        currentUnityController.applicationWillEnterForeground(application)
    }

    func applicationDidBecomeActive(application: UIApplication) {
        currentUnityController.applicationDidBecomeActive(application)
    }

    func applicationWillTerminate(application: UIApplication) {
        currentUnityController.applicationWillTerminate(application)
    }
}
//添加一個(gè)新的main.swift文件到工程里
//添加如下代碼
import Foundation
import UIKit
// overriding @UIApplicationMain
custom_unity_init(Process.argc, Process.unsafeArgv)
UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(UIApplication), NSStringFromClass(AppDelegate))

10. pod install 之后

//添加代碼到Unity.xcconfig 里面
#include "Pods/Target Support Files/Pods-你的項(xiàng)目名/Pods-你的項(xiàng)目名.debug.xcconfig"
#include "Pods/Target Support Files/Pods-你的項(xiàng)目名/Pods-你的項(xiàng)目名.release.xcconfig"

Project -> Build Settings -> User-Defined
查看PODS_ROOT等相關(guān)路徑是否正確


設(shè)置

添加依賴庫


添加依賴庫

11. 加載unity view

func loadUnity(sender: UIButton) {
        let unityview = UnityGetGLView()
        unityview.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(unityview)
        let views = ["view": unityview]
        let w = NSLayoutConstraint.constraintsWithVisualFormat("|[view]|", options: [], metrics: nil, views: views)
        let h = NSLayoutConstraint.constraintsWithVisualFormat("V:|[view]|", options: [], metrics: nil, views: views)
        
        view.addConstraints(w + h)
    }

跑起來試一下
集成只支持unity+swift2.3!!!!
集成只支持unity+swift2.3!!!!
集成只支持unity+swift2.3!!!!
有任何問題,發(fā)評(píng)論我

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容