0.准备工作
所有都是我使用的版本
0. unity 版本 官方推荐2022.3LTS(我是2022.3.50f1c1)
1. nasm版本2.16.01
链接: nasm下载
2. cmake版本3.15以上(我是3.31.1)
3. NET SDK V6.0版本以上(我是9.0.101)
4. windows长路径支持
5. vs2022
6. 区域设置勾选Beta版(这玩意不一定有用,我的电脑上改了就可以,但是同事的电脑上改了就不行)
我把教程先放在这 教程:感谢这位大佬
打开控制面板
打开时间和区域
打开区域
点击管理,然后点击更改系统区域设置
把这玩意勾上
1. 编译全过程
0. 先找一个文件夹,用cmd进去,这里我直接放在D盘CesiumCode下
1. 输入
git clone --recurse-submodules https://github.com/CesiumGS/cesium-unity-samples.git
(这里我在写文章的时候碰见了一个问题,下载的时候在Package目录下没有com.cesium.unity 这个文件夹)
那我们在Packages文件夹下新建一个 com.cesium.unity
再上cesium官方github下载对应的包
cesium官方包
解压出来
把里头的文件全部复制到 \cesium-unity-samples\Packages\com.cesium.unity目录下
2. 用cmd 进入 com.cesium.unity 文件夹下
cd cesium-unity-samples\Packages\com.cesium.unity
3. 安装Reinterop
在cmd中输入
dotnet publish Reinterop~ -o .
4. 用unity打开工程
99.999%会提示这个
点这个
我选的是2022.3.50f1c1
点这个
稍等片刻弹出个窗口,点如图所示
经过一个漫长的等待
我们可以看到工具栏里已经出现了cesium了,但是点开什么都没有,这也是正常的
关闭工程
如果不执行这一步c++工程就会报错
(这一步很重要,我来解释一下为什么要做这一步,这一步是让unity去编译一个dotnet的c++库,后续用cmake编译文件的时候编译的就是c++调用c#库的一个东西,不过我没有研究透彻,只是个人的一些浅显理解,没办法保证正确性,欢迎各位大佬在评论区指正)
5. cmake编译
回到cmd 然后进入native~文件夹内
cd native~
执行
cmake -B build -S . -G "Visual Studio 17 2022" -DCMAKE_BUILD_TYPE=Debug
我们可以看到是失败的
因为\native~\extern里头的几个文件夹是空的(存在直接成功的情况,如果成功直接跳转到步骤6
这时候就需要我们手动下载
手动下载地址
一次下载缺少的四个文件夹
依次复制到对于的文件
复制完成时候回到cmd 再次执行
cmake -B build -S . -G "Visual Studio 17 2022" -DCMAKE_BUILD_TYPE=Debug
看见这个就说明执行成功了
进入build目录应该长这样
乂↑ 如果这里报错是这种那就得执行前面的准备工作6 详细报错看一下这个
$ cmake -B build -S . -G "Visual Studio 17 2022"
-- VCPKG_TRIPLET x64-windows-static-md
-- VCPKG_OVERLAY_PORTS
-- VCPKG_OVERLAY_TRIPLETS
-- EZVCPKG v0.1 starting up
Website: https://github.com/jherico/ezvcpkg
-- EZVCPKG_BASEDIR envrionment variable not found and basedir not set, using default C:\Users\datameta/.ezvcpkg
-- EZVCPKG initializing
commit: 2024.11.16
repository: https://github.com/microsoft/vcpkg.git
local dir: C:/Users/datameta/.ezvcpkg/2024.11.16
-- EZVCPKG Building/Verifying package asyncplusplus using triplet x64-windows-static-md
-- EZVCPKG Building/Verifying package expected-lite using triplet x64-windows-static-md
-- EZVCPKG Building/Verifying package fmt using triplet x64-windows-static-md
-- EZVCPKG Building/Verifying package glm using triplet x64-windows-static-md
-- EZVCPKG Building/Verifying package rapidjson using triplet x64-windows-static-md
-- EZVCPKG Building/Verifying package spdlog using triplet x64-windows-static-md
-- EZVCPKG Building/Verifying package stb using triplet x64-windows-static-md
-- EZVCPKG Building/Verifying package uriparser using triplet x64-windows-static-md
-- EZVCPKG Building/Verifying package abseil using triplet x64-windows-static-md
-- EZVCPKG Building/Verifying package draco using triplet x64-windows-static-md
-- EZVCPKG Building/Verifying package ktx using triplet x64-windows-static-md
CMake Error at cmake/ezvcpkg/ezvcpkg.cmake:83 (message):
EZVCPKG failed with error 1
Call Stack (most recent call first):
cmake/ezvcpkg/ezvcpkg.cmake:184 (EZVCPKG_CHECK_RESULTS)
cmake/ezvcpkg/ezvcpkg.cmake:276 (EZVCPKG_BUILD)
cmake/ezvcpkg/ezvcpkg.cmake:290 (EZVCPKG_FETCH_IMPL)
CMakeLists.txt:66 (ezvcpkg_fetch)
*** The output from the command was:
Computing installation plan...
The following packages will be built and installed:
ktx:x64-windows-static-md@4.3.2
Detecting compiler hash for triplet x64-windows-static-md...
Compiler found: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.42.34433/bin/Hostx64/x64/cl.exe
Restored 0 package(s) from C:\Users\datameta\AppData\Local\vcpkg\archives in 143 us. Use --debug to see more details.
Installing 1/1 ktx:x64-windows-static-md@4.3.2...
Building ktx:x64-windows-static-md@4.3.2...
C:\Users\datameta\.ezvcpkg\2024.11.16\triplets\community\x64-windows-static-md.cmake: info: loaded community triplet from here. Community triplets are not built in the curated registry and are thus less likely to succeed.
-- Using cached KhronosGroup-KTX-Software-v4.3.2.tar.gz.
-- Extracting source C:/Users/datameta/.ezvcpkg/2024.11.16/downloads/KhronosGroup-KTX-Software-v4.3.2.tar.gz
CMake Error at scripts/cmake/vcpkg_execute_required_process.cmake:127 (message):
Command failed: "D:/Program Files/CMake/bin/cmake.exe" -E tar xjf C:/Users/datameta/.ezvcpkg/2024.11.16/downloads/KhronosGroup-KTX-Software-v4.3.2.tar.gz
Working Directory: C:/Users/datameta/.ezvcpkg/2024.11.16/buildtrees/ktx/src/v4.3.2-8d9e3937e6.clean.tmp
Error code: 1
See logs for more information:
C:\Users\datameta\.ezvcpkg\2024.11.16\buildtrees\ktx\extract-err.log
Call Stack (most recent call first):
scripts/cmake/vcpkg_extract_source_archive.cmake:120 (vcpkg_execute_required_process)
scripts/cmake/vcpkg_extract_source_archive_ex.cmake:8 (vcpkg_extract_source_archive)
scripts/cmake/vcpkg_from_github.cmake:113 (vcpkg_extract_source_archive_ex)
ports/ktx/portfile.cmake:1 (vcpkg_from_github)
scripts/ports.cmake:196 (include)
error: building ktx:x64-windows-static-md failed with: BUILD_FAILED
See https://learn.microsoft.com/vcpkg/troubleshoot/build-failures?WT.mc_id=vcpkg_inproduct_cli for more information.
Elapsed time to handle ktx:x64-windows-static-md: 4.1 s
Please ensure you're using the latest port files with `git pull` and `vcpkg update`.
Then check for known issues at:
https://github.com/microsoft/vcpkg/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+ktx
You can submit a new issue at:
https://github.com/microsoft/vcpkg/issues/new?title=[ktx]+Build+error+on+x64-windows-static-md&body=Copy+issue+body+from+C%3A%2FUsers%2Fdatameta%2F.ezvcpkg%2F2024.11.16%2Finstalled%2Fvcpkg%2Fissue_body.md
*** Content of log file: C:\Users\datameta\.ezvcpkg\2024.11.16\buildtrees\ktx\extract-err.log:
cmake -E tar: warning: skipping symbolic link "KTX-Software-4.3.2/NOTICE.md" -> "LICENSE.md".
CMake Error: Problem with archive_write_header(): Invalid empty pathname
CMake Error: Current file:
CMake Error: Problem extracting tar: C:/Users/datameta/.ezvcpkg/2024.11.16/downloads/KhronosGroup-KTX-Software-v4.3.2.tar.gz
CMake Error at cmake/ezvcpkg/ezvcpkg.cmake:102 (message):
Call Stack (most recent call first):
cmake/ezvcpkg/ezvcpkg.cmake:184 (EZVCPKG_CHECK_RESULTS)
cmake/ezvcpkg/ezvcpkg.cmake:276 (EZVCPKG_BUILD)
cmake/ezvcpkg/ezvcpkg.cmake:290 (EZVCPKG_FETCH_IMPL)
CMakeLists.txt:66 (ezvcpkg_fetch)
-- Configuring incomplete, errors occurred!
这个问题是在解压KhronosGroup-KTX-Software-v4.3.2.tar.gz的时候解压不出来,说是可以设置使用7z或者bandizip来解压,但是我后面没研究,欢迎知道的大佬在评论区里说一下
6. 编译c++项目
用vs2022打开CesiumForUnityNative.sln
ctrl+shift+b 全部编译
等待片刻后生成完毕
7. 复制Dll
1)在native~/build/Runtime/Debug 中,有动态库文件:CesiumForUnityNative-Runtime.dll
2)在native~/build/Editor/Debug 中,有动态库文件:CesiumForUnityNative-Editor.dll
3)将两个文件其拷贝至com.cesium.unity/Editor文件夹下
2. 打开工程
重新打开工程,我们可以看到成功了