为方便 Unity 开发者调试和接入腾讯云游戏多媒体引擎产品 API,这里向您介绍适用于 Unity 开发的工程导出配置。
导出 iOS 平台
从 Unity 工程导出成 Xcode 工程时,需要对 GME 动态库进行处理,处理手段根据 Unity 版本的区别而有所不同。
1. 动态库处理(Unity 2019及以上版本)
配置原理
新建一个 Editor OnPostprocessBuild 脚本,利用UnityEditor.iOS.Xcode.Extensions.PBXProjectExtensions.AddFileToEmbedFrameworks,这个 API 会自动将动态库拷贝到最终出包 Bundle 的 framework 目录下,并为其签名。
string[] framework_names = {"libgme_fdkaac.framework","libgme_lamemp3.framework","libgme_ogg.framework","libgme_soundtouch.framework"};
示例代码
可参考 Demo 工程中的 add_dylib.cs 脚本文件,根据自己工程需求将此部分代码放在工程中 Editor 文件夹下。
[UnityEditor.Callbacks.PostProcessBuild(1002)]public static void OnPostprocessBuild (UnityEditor.BuildTarget BuildTarget, string path){if (BuildTarget == UnityEditor.BuildTarget.iOS) {UnityEngine.Debug.Log ("OnPostprocessBuild add_dylib:" + path);#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX{string projPath = UnityEditor.iOS.Xcode.PBXProject.GetPBXProjectPath (path);UnityEditor.iOS.Xcode.PBXProject proj = new UnityEditor.iOS.Xcode.PBXProject ();proj.ReadFromString (System.IO.File.ReadAllText (projPath));// string targetGuid = proj.TargetGuidByName (UnityEditor.iOS.Xcode.PBXProject.GetUnityTargetName ()); // 2018string targetGuid = proj.GetUnityMainTargetGuid(); // 2019// 根据导入的 framework 进行删减string[] framework_names = {"libgme_fdkaac.framework","libgme_lamemp3.framework","libgme_ogg.framework","libgme_soundtouch.framework"};for (int i = 0; i < framework_names.Length; i++){string framework_name = framework_names[i];string dylibGuid = null;dylibGuid = proj.FindFileGuidByProjectPath("Frameworks/Plugins/iOS/" + framework_name);if (dylibGuid == null) {UnityEngine.Debug.LogWarning (framework_name + " guid not found");} else {UnityEngine.Debug.LogWarning (framework_name + " guid:" + dylibGuid);// proj.AddDynamicFramework (targetGuid, dylibGuid);UnityEditor.iOS.Xcode.Extensions.PBXProjectExtensions.AddFileToEmbedFrameworks(proj, targetGuid, dylibGuid);proj.AddBuildProperty(targetGuid, "LD_RUNPATH_SEARCH_PATHS", "@executable_path/Frameworks");System.IO.File.WriteAllText (projPath, proj.WriteToString ());}}}#endif}}
2. 动态库处理(Unity 版本低于2019)
目前只有 Unity 2019 及以后版本可以使用 UnityEditor.iOS.Xcode.Extensions,如果是早期 Unity 版本,可以从高版本 Unity 导出 UnityEditor.iOS.Xcode 包给低版本 Unity 使用,或者直接参考附件 UnityEditorAV.iOS.XCode.zip 将此文件解压后放置于工程目录 Editor 文件夹下。
![](https://qcloudimg.tencent-cloud.cn/image/document/641ee6ddb925408ae898b13881b9d105.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/641ee6ddb925408ae898b13881b9d105.png)
3. 导出 Xcode 工程
确保 Xcode 版本在10.0以上,在 Unity 编辑器中导出 Xcode 工程。
4. 关闭 BitCode
如果编译过程中提示下图错误,请关闭 Bitcode。关闭此设置,只需在 Targets > Build Settings 中搜索 Bitcode 即可,找到相应选项,设置为 NO。
![](https://qcloudimg.tencent-cloud.cn/image/document/52c1d2cbf517295eaf0ab3cced5a9112.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/52c1d2cbf517295eaf0ab3cced5a9112.png)
5. 添加 iOS 平台权限
Required background modes:允许后台运行(可选)。
Microphone Usage Description:允许麦克风权限。
6. 补充库文件
在编译过程中出现如下图错误,请将库文件补全。
![](https://qcloudimg.tencent-cloud.cn/image/document/85b38583fec06a4f5c067605cf9e7747.png)
库文件列表如下:
![](https://qcloudimg.tencent-cloud.cn/image/document/85b38583fec06a4f5c067605cf9e7747.png)
libc++.tbdCoreMedia.frameworklibresolv.tbdAVFoundation.frameworkSecurity.frameworkCoreAudio.frameworkAudioToolbox.frameworklibiconv.tbdlibz.tbdSystemConfiguration.frameworkOpenAL.framework
7. 添加 libresolv9.tbd
出现如下图错误:
![](https://qcloudimg.tencent-cloud.cn/image/document/11d381a64467626365e9e7215a0942d0.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/11d381a64467626365e9e7215a0942d0.png)
请将 libresolv9.tbd 添加到 UnityFramework 中。
![](https://qcloudimg.tencent-cloud.cn/image/document/605cc6d0efa71ff0526c010b4d9e638c.jpeg)
![](https://qcloudimg.tencent-cloud.cn/image/document/605cc6d0efa71ff0526c010b4d9e638c.jpeg)
8. Xcode 15支持
如果在使用xcode 15编译Unity工程时出现:
error: unable to execute command: Abort trap: 6 clang: error: linker command failed due to signal (use -v to see invocation)
请添加 -ld64 连接选项
![](https://qcloudimg.tencent-cloud.cn/image/document/58f04d134cbe66be3a4fe73ba8cd15df.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/58f04d134cbe66be3a4fe73ba8cd15df.png)
9. 导出问题
导出 Android 平台
1. 删减 lib 文件
GME Unity SDK 默认提供 arm64-v8a、armeabi-v7a 及 x86 的 lib 文件,请根据工程项目所需进行删减。
架构缺失
如果导出 Android 可执行文件,缺失指定架构,则会导致 Crash。
如果导出可执行文件 apk 文件后,打开后黑屏闪退,一般为缺少相应架构的 lib 文件,请根据工程进行添加或者删减。
2. 权限配置
2.1 必要权限
请务必在工程 AndroidManifest.xml 文件中添加以下权限:
<uses-permission android:name="android.permission.RECORD_AUDIO" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
2.2 根据需求添加权限
请根据需求在工程 AndroidManifest.xml 文件中添加以下权限:
读写权限不是必须添加的,请根据以下规则进行判断是否添加:
如果使用的是默认的日志路径(/sdcard/Android/data/xxx.xxx.xxx/files),即表示未对 SetLogPath 进行调用,则不需要 WRITE_EXTERNAL_STORAGE 权限。
如果调用 SetLogPath 接口将日志路径放在外部存储设备,以及使用语音消息功能在录制时的存储路径是在外部存储设备,则需要向用户申请 WRITE_EXTERNAL_STORAGE 权限,并得到用户明确批准。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
需要根据以下规则判断如何添加蓝牙权限:
如果工程中的 targetSDKVersion 为 30 或者以下:
<uses-permission android:name="android.permission.BLUETOOTH"/>
如果工程中的 targetSDKVersion 为 31 或者以上:
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" /><uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
3. 导出问题
导出 Windows 平台
导出 WebGL 平台
1. 配置WebGL下的plugins
设置 Windows 平台下 gmesdk.dll 的适用范围,避免它和 WebGL平台下的 gmesdk 冲突:
![](https://qcloudimg.tencent-cloud.cn/image/document/bce11acbbab6ca45cfe9d9024a26f5ab.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/bce11acbbab6ca45cfe9d9024a26f5ab.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/47991d66a5b88a0602087e2c23712811.jpeg)
![](https://qcloudimg.tencent-cloud.cn/image/document/47991d66a5b88a0602087e2c23712811.jpeg)
2. 取消Flare Layer(Unity 2018及以上版本)
![](https://qcloudimg.tencent-cloud.cn/image/document/b630bb9d48dfe35c6c3af31c0241c081.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/b630bb9d48dfe35c6c3af31c0241c081.png)
由于部分 Unity 版本不再支持 MainCamera 中的 Flare Layer 模式,因此需要在要打包的 Scene 中将 Flare Layer 取消勾选,否则会报以下错误:
![](https://qcloudimg.tencent-cloud.cn/image/document/76d8f8a98df3a040b47fca15efb183dd.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/76d8f8a98df3a040b47fca15efb183dd.png)
3. 选择模板
导出 WebGL 平台时,选择 GME 的 WebGL 模板,这样才能保证打包成功的产物正确地导入了相关依赖库。工程默认会使用 GMEWebGLTemplatesUnity2018 模板,这个模板支持 Unity2018 和 Unity2019 版本。对于 Unity2020 和 Unity2021 版本,打包时需更改使用模板,使用 GMEWebGLTemplatesUnity2021 进行打包。
![](https://qcloudimg.tencent-cloud.cn/image/document/8450220cfb5fe789b5332b5deb96a68c.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/8450220cfb5fe789b5332b5deb96a68c.png)
4. 引入前端库
在将 GME-WebGL 导入到自己的工程中,在使用 Unity 生成好对应的网页时,需要手动引入前端库并将前端库文件放在相应的引用位置,并添加 Audio 标签(如下图所示)。如希望每次打包 Unity 产物的时候就自动完成上述工作,可以参考 GME-WebGL demo中的做法,为自己的工程添加相应的模板。
![](https://qcloudimg.tencent-cloud.cn/image/document/8257375bf83f0fcd31bf405e3aca0666.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/8257375bf83f0fcd31bf405e3aca0666.png)