Cesium for unity源码编译以及我碰到的问题解决方法(我奶奶来了都能整出来)

发布于:2025-09-02 ⋅ 阅读:(18) ⋅ 点赞:(0)

0.准备工作

所有都是我使用的版本

0. unity 版本 官方推荐2022.3LTS(我是2022.3.50f1c1)

unity版本

1. nasm版本2.16.01

链接: nasm下载
nasm版本

2. cmake版本3.15以上(我是3.31.1)

cmake版本下载
cmake版本

3. NET SDK V6.0版本以上(我是9.0.101)

NET SDK下载
NET SDK版本

4. windows长路径支持

windows长路径支持教程

windows长路径支持

5. vs2022

VS2022下载
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目录下
Ctrl+A Ctrl+C

Ctrl+V复制过来

2. 用cmd 进入 com.cesium.unity 文件夹下

cd cesium-unity-samples\Packages\com.cesium.unity

3. 安装Reinterop

在cmd中输入

dotnet publish Reinterop~ -o .

直接就是一个执行完毕

4. 用unity打开工程

直接就是一个项目的打开
99.999%会提示这个
0.001%应该是你安装了这个版本
点这个
直接就是一个版本的更改
我选的是2022.3.50f1c1
直接就是一个版本的切换
点这个
改!
稍等片刻弹出个窗口,点如图所示
直接就是一个Conti的nue
经过一个漫长的等待

打开啦!!!
我们可以看到工具栏里已经出现了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. 打开工程

重新打开工程,我们可以看到成功了
报错没有啦,面板出来啦