老司機(jī)走開
網(wǎng)上有很多unity集成到Objective-C的教程, 出門左拐,度娘谷歌都有,這里就不重復(fù)介紹了
公司項(xiàng)目需求
現(xiàn)有項(xiàng)目swift 2.3 + unity
虐死人啊
網(wǎng)上教程有篇老外的
- 你項(xiàng)目不用cocoapods
- 你看得懂作者思路
just go ->
How to use Unity 3D within an iOS app
使用cocoapods,直接越坑
廢話不多說,下面步驟:
1. unity導(dǎo)出工程
沒啥難度,配置好就行, 注意 : 設(shè)備方向最好是Auto Rotation, 再由iOS端控制方向,會減少不少bug
2. 添加Unity.xcconfig 文件到項(xiàng)目路徑
按圖步驟,設(shè)置project的配置,Debug和Release改成Unity就行
添加UnityBridge.h, UnityUtils.h, UnityUtils.mm 到項(xiàng)目
//更改整個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,這三個
新建文件夾ios_ui_animation, 拷貝進(jìn)去
4. 更改工程配置
更改 UNITY_RUNTIME_VERSION 為你unity工程版本號,我的是 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
Data 設(shè)置 Create folder
7. 刪除引用
先刪除引用libraries里面的libil2cpp文件夾季春,然后再刪除Classes里面的Native文件夾里面的所有.h文件
8. 變更unity里方法,引用等
找到main.mm
注釋這個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啟動
//@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)
}
}
//添加一個新的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)路徑是否正確
添加依賴庫
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ā)評論我