Back to Puerts

Nuget

unity/nuget/README.md

1.0.173.0 KB
Original Source

Nuget

使用

可用的平台 .net8.0, win-x64, linux-x64, osx (universal binary)

Python 的 osx 版本目前仅支持 osx-arm64

目前不支持AOT

引入Puerts ( Puerts.Core 包),并根据需求引入需要运行的平台的 NativeAssets (.dll, .so ...), 没有 NativeAssets 则无法在对应的平台运行

例如

  • Puerts.Core.NativeAssets.Win32 对应 Windows
  • Puerts.Core.NativeAssets.Linux 对应 Linux
  • Puerts.Core.NativeAssets.macOS 对应 macOS

引入需要的扩展,以下是支持的扩展,同样的,也需要引入对应的 NativeAssets

  • Puerts.Lua
  • Puerts.NodeJS
  • Puerts.Python
  • Puerts.QuickJS
  • Puerts.V8

简单用例,已引入 Puerts.CorePuerts.V8 以及对应的 NativeAssets

csharp
using Puerts;

var jsEnv = new ScriptEnv(new BackendV8()); 
jsEnv.Eval(@"CS.System.Console.WriteLine('hello world');");
jsEnv.Dispose();
Console.WriteLine("Press any key to exit...");

包版本定义

Directory.Build.props

构建包的版本可以通过设置 Directory.Build.props 中的各个版本号来定义

版本是从各个upm包的 package.json 中读取的

xml
<PuertsCoreVersion></PuertsCoreVersion>
<PuertsLuaVersion></PuertsLuaVersion>
<PuertsNodeJsVersion></PuertsNodeJsVersion>
<PuertsPythonVersion></PuertsPythonVersion>
<PuertsQuickJsVersion></PuertsQuickJsVersion>
<PuertsV8Version></PuertsV8Version>

设置目标的框架

xml
<PuertsCurrentTargetFrameworks>net8.0</PuertsCurrentTargetFrameworks>
<PuertsNativeAssetsLinuxTargetFrameworks>net8.0</PuertsNativeAssetsLinuxTargetFrameworks>
<PuertsNativeAssetsWin32TargetFrameworks>net8.0</PuertsNativeAssetsWin32TargetFrameworks>
<PuertsNativeAssetsmacOSTargetFramework>net8.0</PuertsNativeAssetsmacOSTargetFramework>

构建发布

在Windows下构建、发布

powershell
# 构建
.\build.ps1 --NativeAssetsDirectory "xxx\puerts\unity\downloaded_natives" --ProjectsRoot "xxx\puerts\unity\nuget"
# 构建并发布
.\build.ps1 --NativeAssetsDirectory "xxx\puerts\unity\downloaded_natives" --ProjectsRoot "xxx\puerts\unity\nuget" --Source "https://api.nuget.org/v3/index.json" --ApiKey "your-nuget-api-key"

在Linux/macOS下构建、发布

shell
# 构建
./build.sh --NativeAssetsDirectory "xxx/puerts/unity/downloaded_natives" --ProjectsRoot "xxx/puerts/unity/nuget"
# 构建并发布
/build.sh --NativeAssetsDirectory "xxx/puerts/unity/downloaded_natives" --ProjectsRoot "xxx/puerts/unity/nuget" --Source "https://api.nuget.org/v3/index.json" --ApiKey "your-nuget-api-key"

构建参数

  • NativeAssetsDirectory 原生文件目录,放置构建完成的 native 项目产物
  • ProjectsRoot nuget项目目录

发布参数

  • Source 打包发布的目标源
  • ApiKey 目标源的密钥 注意密钥安全

定义构建

构建脚本 Program.cs