在集成之前确保demo项目可以运行 (导出demo项目是最好用mac版Unity)
1.新建立文件 Unity.xconfig
// 写入自己的untiy对应的版本号
UNITY_RUNTIME_VERSION = 5.5.2f1;
UNITY_SCRIPTING_BACKEND = il2cpp;
UNITY_IOS_EXPORT_PATH = /Path/To/Your/Unity/iOS/Build;
GCC_PREFIX_HEADER = $(UNITY_IOS_EXPORT_PATH)/Classes/Prefix.pch;
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 = "$(UNITY_IOS_EXPORT_PATH)/Classes" "$(UNITY_IOS_EXPORT_PATH)/Classes/Native" "$(UNITY_IOS_EXPORT_PATH)/Libraries/libil2cpp/include";
LIBRARY_SEARCH_PATHS = "$(UNITY_IOS_EXPORT_PATH)/Libraries" "$(UNITY_IOS_EXPORT_PATH)/Libraries/libil2cpp/include";
ENABLE_BITCODE = NO;
SWIFT_OBJC_BRIDGING_HEADER = UnityBridge.h;
OTHER_CFLAGS = -DINIT_SCRIPTING_BACKEND=1;
CLANG_CXX_LANGUAGE_STANDARD = c++11;
CLANG_CXX_LIBRARY = libc++;
CLANG_ENABLE_MODULES = NO;
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_ENABLE_CPP_RTTI = 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;
2. project配置, 如下图
3.新建文件夹unity_ios, 并且将Classes Data Libraries 三个文件拷贝到该文件夹下
添加完成之后, 应该显示这样
4.接下来下载三个文件, 参考链接
新建一个文件夹 objc, 将三个文件拷贝到此文件夹下
UnityBridge.h 文件内容
#ifndef UnityBridge_h
#define UnityBridge_h
#import
#import "UnityUtils.h"
#import "UnityAppController.h"
#import "UnityInterface.h"
#import "ReceiveUnitySystemEvent.h"
#endif /* UnityBridge_h */
UnityUtils.h文件内容
#ifndef UnityUtils_h
#define UnityUtils_h
void custom_unity_init(int argc, char* argv[]);
#endif /* UnityUtils_h */
UnityUtils.mm文件内容
#include "RegisterMonoModules.h"
#include "RegisterFeatures.h"
#include
// Hack to work around iOS SDK 4.3 linker problem
// we need at least one __TEXT, __const section entry in main application .o files
// to get this section emitted at right time and so avoid LC_ENCRYPTION_INFO size miscalculation
static const int constsection = 0;
void UnityInitTrampoline();
extern "C" void custom_unity_init(int argc, char* argv[])
{
@autoreleasepool
{
UnityInitTrampoline();
// UnityParseCommandLine(argc, argv); //Unity 5.3+
UnityInitRuntime(argc, argv); //Unity 5.6+,5.4和5.5用哪个我没试过,可以根据报错情况选择。
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);
}
}
注意三个文件都是用 create groups (不要创建桥接文件 don't create)
5.模型文件的添加
6. 修改这两个地方(build setting 最末端)
7. 修改桥接文件路径
8. 添加依赖库文件
9. main.mm修改
// WARNING: this MUST be c decl (NSString ctor will be called after +load, so we cant really change its value)
const char* AppControllerClassName = "UnityAppController";
int main_unity_default(int argc, char* argv[])
{
@autoreleasepool
{
UnityInitTrampoline();
// UnityParseCommandLine(argc, argv); //Unity 5.3+
UnityInitRuntime(argc, argv); //Unity 5.6+,5.4和5.5用哪个我没试过,可以根据报错情况选择。
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;
}
10. UnityAppController.h文件修改
extern UnityAppController* _UnityAppController;
NS_INLINE UnityAppController* GetAppController()
{
NSObject* delegate = [UIApplication sharedApplication].delegate;
UnityAppController* currentUnityController = (UnityAppController *)[delegate valueForKey:@"currentUnityController"];
return currentUnityController;
}
11. 修改 Appdelegate.swift 文件
import UIKit
//注释@UIApplicationMain, 让swift从main.swift启动
//@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var currentUnityController: UnityAppController!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
currentUnityController = UnityAppController()
currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)
window = UIWindow.init(frame: UIScreen.main.bounds)
let sb = UIStoryboard.init(name: "Main", bundle: nil)
let vc = sb.instantiateInitialViewController()
window?.rootViewController = vc!;
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)
}
}
12.添加main.swift文件
import Foundation
import UIKit
custom_unity_init(CommandLine.argc, CommandLine.unsafeArgv)
UIApplicationMain(
CommandLine.argc,
UnsafeMutableRawPointer(CommandLine.unsafeArgv)
.bindMemory(
to: UnsafeMutablePointer.self,
capacity: Int(CommandLine.argc)),
nil,
NSStringFromClass(AppDelegate.self)
)
13.修改ViewController.swift 文件使用AR
@IBAction func loadUnity(sender: UIButton) {
let unityview = UnityGetGLView()
unityview?.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(unityview!)
let views = ["view": unityview]
let w = NSLayoutConstraint.constraints(withVisualFormat: "|[view]|", options: [], metrics: nil, views: views)
let h = NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options: [], metrics: nil, views: views)
view.addConstraints(w + h)
}
14. 停止AR识别
https://github.com/blitzagency/ios-unity5
15.easyAR 需要修改的地方 (相机黑屏没有画面)
在 UnityAppController.mm 中添加
extern "C" void ezarUnitySetGraphicsDevice(void* device, int deviceType, int eventType);
extern "C" void ezarUnityRenderEvent(int marker);
- (void)shouldAttachRenderDelegate {
//新增
UnityRegisterRenderingPlugin(&ezarUnitySetGraphicsDevice, &ezarUnityRenderEvent);
//新增
}
遇到的一些问题
问题一:
shellScript = ""$PROJECT_DIR/MapFileParser.sh""\nrm -rf
将多了的引号删除
问题二:
_DefaultTrackableEventHandler_OnTrackingFound_m4202593607 in Bulk_Assembly-CSharp_0.o
iOS中自定义函数未定义
问题三
dataSet 加载失败, 是由于没找到相关路径.
直接在项目路径下拷贝一份资源文件
问题四:
相机6s 黑屏问题
用 Mac unity 2018 导出包
问题五: 打开 和 暂停
https://github.com/blitzagency/ios-unity5
问题六: Objective-C不能用@try*
今天遇到Cannot use ‘@try’ with Objective-C exceptions disable,解决方法:将LLVM 6.1 - Language - Objective C -> Enable Objective-C Exceptions改为YES。
问题七: “_AppControllerClassName”, referenced from:
+[VuforiaNativeRendererController(OverrideAppDelegate) load] in VuforiaNativeRendererController.o
修改 main.mm 中的方法
const char* AppControllerClassName = "UnityAppController";
int main_unity_default(int argc, char* argv[])
{
@autoreleasepool
{
UnityInitTrampoline();
// UnityParseCommandLine(argc, argv); //Unity 5.3+
UnityInitRuntime(argc, argv); //Unity 5.6+,5.4和5.5用哪个我没试过,可以根据报错情况选择。
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;
}
问题八: Library not loaded: @rpath/Vuforia.framework/Vuforia
Image not load
问题九: Xcode release下调试报错[Project Name] was compiled with optimization - stepping may behave oddly; variables may not be available
- 解决:Build Setting中的optimization level release改为与debug一样的模式。-
- updated your OTHER_CFLAGS in .xcconfig to
OTHER_CFLAGS = $(inherited) -DINIT_SCRIPTING_BACKEND=1 -fno-strict-overflow -DRUNTIME_IL2CPP=1;
added the @objc in front of var currentUnityController: UnityAppController!
问题十: 打包时报错
no match func objc_msgsend
解决方法:
[image:BC6992C7-CA27-4EE3-84F1-B046DD9CF584-3925-00006B91ED0379F1/07DB4B63-738D-446B-AE92-A0ECC8373448.png]
设置为 NO
问题十一: 打包时 Swift build error_if_any_output_files_are_specified_they_all_must_be
修改
[image:54368292-E997-4C27-B26F-EC5A19BC9F11-3925-00006CBDC9844CEC/4E5A069D-1DC0-4C9B-9FF7-F20E138AFBBC.png]
debug 和 release 修改为increasemental
问题十二: error: unexpected input file: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk
other swift flag 配置错误了
参考链接
参考1
参考2