首先看一下项目的目录结构,便于Unity项目更新。U3D项目更新时只需要把unity_ios目录中的文件替换即可
2357B9A7-ECE7-4CCA-A0F8-A1FD596FA0A3.png1、正式开始吧~第一步先创建Unity.xcconfig,添加到项目
7E01BFBD-4DEF-46AF-8B9A-9F439A643EFD.png文件内容为
//
// Unity.xcconfig
// ShareTravel
//
// Created by CC on 2018/6/11.
// Copyright © 2018年 CC. All rights reserved.
//
//
// Unity.xcconfig
UNITY_RUNTIME_VERSION = 5.6.1f1;//U3D版本
UNITY_SCRIPTING_BACKEND = il2cpp;
GCC_THUMB_SUPPORT = NO;
GCC_USE_INDIRECT_FUNCTION_CALLS = NO
//U3D存放目录 可根据实际情况修改
UNITY_IOS_EXPORT_PATH = $(PROJECT_DIR)/unity_ios;
GCC_PREFIX_HEADER = $(UNITY_IOS_EXPORT_PATH)/Classes/Prefix.pch;
//添加的动态库FLAGS
OTHER_LDFLAGS = -weak-lSystem -weak_framework CoreMotion -weak_framework GameKit -weak_framework iAd -framework CoreGraphics -framework AVFoundation -framework CoreVideo -framework CoreMedia -framework SystemConfiguration -framework CoreLocation -framework MediaPlayer -framework CFNetwork -framework AudioToolbox -framework OpenAL -framework QuartzCore -framework OpenGLES -framework UIKit -framework Foundation -liconv.2 -liPhone-lib;
HEADER_SEARCH_PATHS = $(inherited) $(UNITY_IOS_EXPORT_PATH) $(UNITY_IOS_EXPORT_PATH)/Classes $(UNITY_IOS_EXPORT_PATH)/Classes/Native $(UNITY_IOS_EXPORT_PATH)/Classes/UI $(UNITY_IOS_EXPORT_PATH)/Libraries $(UNITY_IOS_EXPORT_PATH)/Libraries/libil2cpp/include $(UNITY_IOS_EXPORT_PATH)/Libraries/bdwgc/include;
LIBRARY_SEARCH_PATHS = $(inherited) $(UNITY_IOS_EXPORT_PATH) $(UNITY_IOS_EXPORT_PATH)/Libraries $(UNITY_IOS_EXPORT_PATH)/Libraries/libil2cpp/include;
ENABLE_BITCODE = NO;
//桥接文件 根据实际情况修改
SWIFT_OBJC_BRIDGING_HEADER = $(PROJECT_DIR)/$(PRODUCT_NAME)/Support/ShareTravel-Bridging-Header.h;
OTHER_CFLAGS = -DINIT_SCRIPTING_BACKEND=1;
CLANG_CXX_LANGUAGE_STANDARD = compiler-default;
CLANG_CXX_LIBRARY = libc++;
CLANG_WARN_BOOL_CONVERSION = NO;
CLANG_WARN_CONSTANT_CONVERSION = NO;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES;
CLANG_WARN_EMPTY_BODY = NO;
CLANG_WARN_ENUM_CONVERSION = NO;
CLANG_WARN_INT_CONVERSION = NO;
CLANG_WARN_OBJC_ROOT_CLASS = YES;
CLANG_WARN_UNREACHABLE_CODE = NO;
CLANG_WARN__DUPLICATE_METHOD_MATCH = NO;
GCC_C_LANGUAGE_STANDARD = c99;
GCC_ENABLE_OBJC_EXCEPTIONS = NO;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_THUMB_SUPPORT = NO;
GCC_USE_INDIRECT_FUNCTION_CALLS = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION[arch=*64] = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = NO;
GCC_WARN_UNINITIALIZED_AUTOS = NO;
GCC_WARN_UNUSED_FUNCTION = NO;
GCC_NO_COMMON_BLOCKS = NO;
CLANG_ENABLE_MODULES = NO;
CLANG_WARN_DOCUMENTATION_COMMENTS = NO;
CLANG_WARN_EMPTY_BODY = NO;
CLANG_WARN_INFINITE_RECURSION = NO;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = NO;
CLANG_WARN_UNREACHABLE_CODE = NO;
GCC_WARN_UNUSED_FUNCTION = NO;
CLANG_WARN__DUPLICATE_METHOD_MATCH = NO;
2、添加完Unity.xcconfig后修改project配置如下
368B7BFE-B5DE-4A67-830A-6E760FC5C3BA.png3、添加 添加UnityUtils.h UnityUtils.mm 到项目
//
// UnityUtils.h
// ShareTravel
//
// Created by CC on 2018/6/11.
// Copyright © 2018年 CC. All rights reserved.
//
#ifndef UnityUtils_h
#define UnityUtils_h
void custom_unity_init(int argc, char* argv[]);
#endif /* UnityUtils_h */
//
// UnityUtils.m
// ShareTravel
//
// Created by CC on 2018/6/11.
// Copyright © 2018年 CC. All rights reserved.
//
#include "RegisterMonoModules.h"
#include "RegisterFeatures.h"
#include <csignal>
static const int constsection = 0;
void UnityInitTrampoline();
extern "C" void custom_unity_init(int argc, char* argv[])
{
@autoreleasepool
{
UnityInitTrampoline();
// UnityParseCommandLine(argc, argv);
UnityInitRuntime(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);
}
}
4、将u3d导出的文件加入到项目
54C171B9-6D46-44FF-8FCE-82B4909DAF64.png注意:文件夹按照下图对应的方式添加,由于文件较大,可能需要耐心等待几分钟
FA342463-2D48-42E8-9999-306D6E8E0330.png 6BCAAD31-36B0-468D-AEF4-27EBFEF70B21.png5、桥接文件添加
#import "UnityUtils.h"
#import "UnityAppController.h"
#import "Unity/UnityInterface.h"
6、添加脚本
FA116136-8A33-42EC-B099-40DEF1C965C6.png6、Build Settings修改如下配置
F1BBE0D0-1375-490F-BC13-A92910946FD4.png7、修改Unity里的方法
- unity_ios-->Classes 找到main.mm
//int main(int argc, char* argv[])
//{
// signed long long startTime = mach_absolute_time();
// @autoreleasepool
// {
// UnitySetStartupTime(startTime);
// UnityInitTrampoline();
// UnityInitRuntime(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;
//}
//替换为
int main_unity_default(int argc, char* argv[])
{
signed long long startTime = mach_absolute_time();
@autoreleasepool
{
UnitySetStartupTime(startTime);
UnityInitTrampoline();
// UnityParseCommandLine(argc, argv);
UnityInitRuntime(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;
}
- unity_ios-->Classes 找到UnityAppController.h
注意:引用
#import <UIKit/UIKit.h>
//inline UnityAppController* GetAppController()
//{
// return (UnityAppController*)[UIApplication sharedApplication].delegate;
//}
//替换为
NS_INLINE UnityAppController* GetAppController()
{
NSObject<UIApplicationDelegate>* delegate = [UIApplication sharedApplication].delegate;
UnityAppController* currentUnityController = (UnityAppController *)[delegate valueForKeyPath:[NSString stringWithFormat:@"currentUnityController"]];
return currentUnityController;
}
3)新建main.swift文件
//
// main.swift
// ShareTravel
//
// Created by CC on 2018/6/11.
// Copyright © 2018年 CC. All rights reserved.
//
import Foundation
import UIKit
custom_unity_init(CommandLine.argc, CommandLine.unsafeArgv)
UIApplicationMain(
CommandLine.argc,
UnsafeMutableRawPointer(CommandLine.unsafeArgv)
.bindMemory(
to: UnsafeMutablePointer<Int8>.self,
capacity: Int(CommandLine.argc)),
nil,
NSStringFromClass(AppDelegate.self)
)
4)修改AppDelegate.swift
//@UIApplicationMain 注意注释此项 让app从main.swift启动
class AppDelegate: UIResponder, UIApplicationDelegate {
var currentUnityController = UnityAppController()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window?.backgroundColor = UIColor.white;
window?.rootViewController = ViewController();
currentUnityController = UnityAppController()
currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)
window!.makeKeyAndVisible()
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)
}
}
8、现在可以尝试运行,显示u3d
let unityView = UnityGetGLView()
unityView?.frame = self.view.bounds
unityView?.center = self.view.center
self.view.addSubview(unityView!)
self.view.bringSubview(toFront: unityView!)
9、错误解决
image.png
网友评论