這個主要是記錄一下公司項目里面集成Unity時候遇到的一些問題。供后來的集成參考使用。
集成期間遇到了不少問題模板也換來換去主要是使用Cardboard 或者 GoogleVRForUnity
Section1 準備
1.swift 2.3 (swift 3.0 應該也沒有問題)
2.Xcode 8.2
3.Unity 5.6.0f1
Section2 開始集成
英文好的可以看看老外的集成文章可以說大部分人的參考步驟都是這個文章吧。
找了找中文的,步驟是參考 Nothing_lu的集成文章推薦先看看這里進行集成(我就是按照這個來的,證明能集成成功~,因為步驟都相同所以就不搬磚了,注意看一下下面的tip。)
tip:文章中有圖片不清晰的地方里面的代碼是這樣的:
var currentUnityController: UnityAppController!
currentUnityController = UnityAppController()
currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)
main.swift
里面的代碼我是使用的這個
// overriding @UIApplicationMain
custom_unity_init(Process.argc, Process.unsafeArgv)
UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(UIApplication), NSStringFromClass(AppDelegate))
ok,如果集成好了就開始填坑吧~~~。
Section3 開始天坑=填坑???
- diff: /../Podfile.lock: No such file or directory
diff: /Manifest.lock: No such file or directory
error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.
(Nothing同學的是新建一個工程,考慮大部分同學都會遇到這個問題,也能解決)
根據說明 執行 pod install
Pods/Target Support Files/Pods-ZMCalendar/Pods-ZMCalendar.debug.xcconfig
in your build configuration (UnityFix/Unity.xcconfig
).
這里看著應該是配置文件中缺少了什么,build工程以后發現錯誤依舊。。
到Unity.xcconfig下面添加
//添加代碼到Unity.xcconfig 里面
#include "Pods/Target Support Files/Pods-你的項目名/Pods-你的項目名.debug.xcconfig"
#include "Pods/Target Support Files/Pods-你的項目名/Pods-你的項目名.release.xcconfig"
Pod沒有報錯了。
Command + B. 如果還報錯,那么重啟一下 Xcode.
-
發現錯誤
swift:28:33: Use of undeclared type 'UnityAppController'
UnityAppController
配置正確的情況下這種錯誤一般是由于橋接文件沒弄好造成的。
所以查看橋接文件
添加上如圖所示:
#import "UnityUtils.h"
#import "UnityAppController.h"
#import "UnityInterface.h"
Command + B (swift 編譯一次可能需要 10分鐘,一點不夸張。)
-
發現錯誤 (我當前使用的是 swift 2.3 如果沒有報錯請忽略這個錯誤)
Use of unresolved identifier ''CommandLine“
CommandLine.png
后來發現 這個錯誤是main.swift 里面寫的好像是不對的。main.swift 里面寫成這樣
// main.swift
import Foundation
import UIKit
// overriding @UIApplicationMain
custom_unity_init(Process.argc, Process.unsafeArgv)
UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(UIApplication), NSStringFromClass(AppDelegate))
- 發現錯誤 'Unity/ObjCRuntime.h' file not found
嘗試添加路徑
這里面的路徑比較多 其實就是鏈接到 當前的一個 庫里面 如果出現這個錯誤 那么添加好對應的路徑。
- 發現錯誤 :/Users/zhangxianqiang/Desktop/ZMCalendarSwift副本/XMGWB/Classes/Tools/Libs/Masonry/MASConstraintMaker.m:55:5: Use of undeclared identifier 'NSAssert'
這個貌似是和 marsony 里面有所沖突
修改方法:
- 報錯
解決方法: (這種錯誤 一般就是 路徑問題 所以 圖中的 路徑請注意 你的可能不是叫 UnityFix!!!)
7錯誤 undeclared identifier 'UnityParseCommandLine'
解決方法:
8.報錯 GVROverlayView.h file not found
解決方法:
a. 當前我這個版本沒有找到對應的 sdk (如果你的小伙伴用了新版的 Unity 模板文件 可能沒有這個問題)
- 報錯
解決方法:
c c++ 里面都添加上這個 -DINIT_SCRIPTING_BACKEND=1
到這里 我的工程跑起來了 因為小伙伴的 Unity里面使用的庫文件經常變化 所以后面可能還遇到不同的坑。
Section 3 額外的一些坑記錄。
- "csingle" file not found
"csingle" file not found
解決說明: 這個問題好像是Xcdoe的問題 我編譯通過了也會報這個錯。
2.cannot use '@throw' with Objective-C exceptions disabled (masrony 集成到工程里面可能會有這個錯)
解決方法:
- gvr::creatMainApp_Expec...
(這個的解決方法不是很好, 如果有好方法請留言告訴我)
#include <vector>
namespace gvr {
class VrApp;
VrApp* CreateMainApp__EXPECTED_EXACTLY_ONE_VR_MAIN_APP_STATEMENT__(const std::vector<std::string>&) { return nullptr; }
}
- 更新版本遇到這個錯誤GoolekitCore.o:
這個是因為 庫沖突了 如果你的Untiy小伙伴用了最新的google cardboard庫 那么就 Podfile 里面去掉 GVRSDK。
-
另外 cocoapods 來回弄 遇到了這個問題 Attempt to read non existent folder!!
Attempt to read non existent folder
解決方法: 別的地方說卸載了cocoapod 重新安裝?? 我沒有試驗過,我只是 修改了文件夾的中文名字變成了英文,然后 把這個工程放到了另外一個文件夾里面再次運行pod install 就行了。
6.(2017.8.10)添加一個錯誤 這個錯誤是編譯已經通過了,跑在真機的時候直接崩潰的問題。
- 報錯 ---> (這里是unity 更換了 5.4.2f0)
build settings 里面:
Other C Flag: -DINIT_SCRIPTING_BACKEND=1
Other C++ Flag: -DINIT_SCRIPTING_BACKEND=1
8 . gfx device intialization failed
xcode 直接斷開和手機的鏈接了.. 這個問題糾纏了我好久了。google上面并沒有什么答案,大概靠譜一點的就是說Unity的配置為題,版本有的不兼容Metal。。 具體的配置可以參考我下面給出的Unity配置圖片,另外可以試試修改工程里面 Edit Schema -> Options -> GPU Frame Caputure 里面進行轉換對應的選項。
但是我遇到的都不是這個問題。
通過intialization failed 發現其實是有東西沒初始化,但是又不給任何提示。所以總結起來就是我們如果要保證一開始就要有初始化unity的步驟。注意 自己創建的 main.swift 文件。還有就是Appdelegate里面的@UIApplicationMain 需要注銷。具體的步驟參考的步驟Nothing_lu的集成文章集成步驟的第十步。
這里其實可以寫一個swift(iOS)工程的啟動解析了。
-
報錯:Bulk_Assembly-CSharp_ 文件報錯。
Bulk_Assembly-CSharp_
Tip:這個里面說明的是我們對一些函數并沒有定義,我個人對C C++ 并不是很擅長,看了幾篇文章,里面大概的意思是這個文件包裹了一些函數供后續的調用,如果出現了這個錯誤,仔細觀察一下是什么前綴,此處圖中是GCloudVioce,后來才分析到是Unity里面添加了一個三方庫,并沒有給到我。
解決:添加.a庫
- MTLDebugRenderCommandEncoder.mm:1331: failed assertion `(rect.y(0) + rect.height(720))(720) must be <= 687'
解決:這里是調整了這里,選擇不用Metal。。 不知道有沒有好的方案。
- NSUnknownKeyException: [<unitytoios.AppDelegate 0x1555ad30> valueForUndefinedKey:]: this class is not key value coding-compliant for the key currentUnityController
解決: 這個問題是一個小伙伴提出來的,是因為他用了 Xcode 9 swift4.0 導致的,可能是語言有升級的關系 appdelegate 里面 修改一下這個 @objc var currentUnityController: UnityAppController? 添加上objc!
Section 5 后續
1.如果你看到有問題或者有好的解決方法,請留言告訴我。
2.如果你遇到問題了,我盡量解答,因為這里面的坑太多了。
3.對你有幫助請留言告訴我。
4.沒有解決問題也請讓我知道你來過這里。
5.生活不容易, 就像每次編譯這個工程 都需要 10分鐘以上。所以 這個文章一邊上班一邊寫了好幾天。
參考文章
http://www.lxweimin.com/p/e8217896d6ff
http://www.cnblogs.com/fuunnyy/p/6227740.html
http://www.lxweimin.com/p/1a141e4fccb3
沒時間吐槽了,寫了這個文章。當做記錄。
=============================================
4.24 添加更新:主要是記錄一下 unity 小伙伴如果更新了 他們的版本然后我們這邊更換集成文件的主要步驟。
1.找到相應的文件夾 替換 classes, Data,Libraries文件夾。工程里面最好是先刪除引用,然后再次導入.(classes,Libraries create groups
data 選擇 create folder references)
2.修改 classes/main.mm 里面的 main -> main_unity_default
UnityParseCommandLine -> UnityInitRuntime(這個不一定要換。囧~)
3.Libraries/RegisterFeatures.mm 添加
#include <vector>
namespace gvr {
class VrApp;
VrApp* CreateMainApp__EXPECTED_EXACTLY_ONE_VR_MAIN_APP_STATEMENT__(const std::vector<std::string>&) { return nullptr; }
}
- 找到UnityAppController.h
添加:#import <UIKit/UIKit.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;
}
5.各種庫文件找不到。 這個可能是因為沒有響應的庫文件,檢查一下:
(如果你是用的pod 管理的 GVRSDK,那么這里應該沒有不然會報很多沖突的錯誤。)
8.記錄一下unity的配置情況
4.28 添加更新。(App里面 現在又添加了一個 百度語音的功能,我先暫時寫到這里面,有時間會整理成另外一篇文章的。)
首先導入的 3方庫 TTTAttributedLabel 這個要是用cocoapods 可以使用 1.6.1版本
appcontroller 里面添加一個接收方法 。目前不知道能不能成功調用,(下面的代碼直接使用是接收不到的!!!)
void Voice (char *buy_id)
{
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:@"1"];
[alert setMessage:[NSString stringWithUTF8String:buy_id]];
[alert addButtonWithTitle:@"確定"];
[alert show];
}
后來問了 Unity 的小伙伴得知應該添加如下的代碼:(這段代碼說是可以添加到工程的任何位置,iOS 的OC工程應該是可以的,我選擇這里是因為我的工程是 swift 的工程,這里面肯定是運行不了這個的具體的原因這里就不說了,感興趣的可以去研究一下,囧~~)
//代碼定義區域 A
#if defined(__cplusplus)
extern "C"{
#endif
// 個人資料聯系他
extern void _Voice( char* toPayStr);
#if defined(__cplusplus)
}
#endif
//代碼定義區域 B
#if defined(__cplusplus)
extern "C"{
#endif
extern void _Voice( char* toPayStr){
NSLog(@"調用了_Voice--%s",toPayStr);
NSString *str = [NSString stringWithFormat:@"%s",toPayStr];
[[NSNotificationCenter defaultCenter] postNotificationName:@"unitySendMessageNoti" object:nil userInfo:@{@"message":str}];
}
#if defined(__cplusplus)
}
#endif
另外這里我用OC 的方式 發送了一條通知,讓Unity的消息傳到swift的代碼中,如果哪個小伙伴有好的方式進行交互可以留言告訴我~。