文档

​
​

Development

User Acquisition

Monetization

工业

Cloud Code

Open Unity Dashboard

Cloud Code

LiveOps
​
​
Cloud Code
  • Overview
  • Get started
  • Server authority
  • Cloud Code C# modules
    • Overview
    • Get started
    • Concepts
    • Tutorials
      • Run modules
      • Write modules
      • Development essentials
      • Automate deployment
      • Integrate services
        • Integrate with other Unity Services
        • Interact with cross-player data
        • Integrate with external services
        • Use Access Control
        • Send push messages
      • Advanced configuration
      • Use Cases
    • Reference
  • Cloud Code JavaScript scripts
  • Logging
  • Use-case samples
  • Privacy and consent
  1. Cloud Code

与外部服务集成

Extend Cloud Code modules with external services using NuGet packages or HTTP clients.
阅读时间3 分钟
最后更新于 9 个月前

Cloud Code 模块可以与外部服务集成。
要处理自定义数据类型,您可以实现自定义 JSON 序列化。

使用 NuGet 包

Cloud Code 模块是 .NET 生态系统中的 C# 库项目,因此它们使用 NuGet Package Manager 来安装依赖项。这意味着您可以在 Cloud Code 模块中使用任何外部 NuGet 包。
要查看可用的 NuGet 包,请参阅 NuGet 包库。

调用外部 API

您还可以调用任何外部 API。以下示例调用 Cat Facts API 来获取随机的 cat fact。
  1. 声明一个
    CatFactsApi
    类来处理 API 调用。
    using Newtonsoft.Json;using RestSharp;using Unity.Services.CloudCode.Core;using Unity.Services.CloudCode.Shared;namespace CatFacts;public interface ICatFactsApi{ public Task<CatFactsApiResponse?> GetCatFact();}public class CatFactsApiResponse{ public string fact { get; set; } public int length { get; set; }}public class CatFactsApi : ICatFactsApi{ private readonly RestClient _httpClient; public CatFactsApi() { // It's important to only create one RestClient per service in order to avoid creating and destroying too many network connections which could hurt performance _httpClient = new RestClient("https://catfact.ninja/fact"); } public async Task<CatFactsApiResponse?> GetCatFact() { var request = new RestRequest(); try { var res = await _httpClient.ExecuteGetAsync(request); return JsonConvert.DeserializeObject<CatFactsApiResponse?>(res.Content); } catch (ApiException e) { throw new Exception($"Failed to get cat fact: {e.Message}"); } }}
  2. 将这个
    CatFactsApi
    类声明为
    ModuleConfig
    类中的单例。
    public class ModuleConfig : ICloudCodeSetup { public void Setup(ICloudCodeConfig config) { config.Dependencies.AddSingleton<ICatFactsApi>(new CatFactsApi()); } }
  3. 声明一个
    GetCatFact
    函数来调用
    CatFactsApi
    类的
    GetCatFact
    方法:
    using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Logging;using Unity.Services.CloudCode.Core;using Unity.Services.CloudCode.Shared;namespace CatFacts;public class CatFactsSample{ private static ILogger<CatFactsSample> _logger; public CatFactsSample(ILogger<CatFactsSample> logger) { _logger = logger; } [CloudCodeFunction("GetCatFact")] public async Task<CatFactsApiResponse?> GetCatFact(ICatFactsApi catFactsApi) { try { var fact = await catFactsApi.GetCatFact(); _logger.LogInformation("Cat fact: {fact}", fact?.fact); return fact; } catch (ApiException e) { _logger.LogError("Failed to get cat fact. Error: {Error}", e.Message); throw new Exception($"Failed to get cat fact: {e.Message}"); } } public class ModuleConfig : ICloudCodeSetup { public void Setup(ICloudCodeConfig config) { config.Dependencies.AddSingleton<ICatFactsApi>(new CatFactsApi()); } }}
  4. 从游戏客户端调用
    GetCatFact
    函数。此函数返回一个随机的 cat fact:
    { "output": { "fact": "The Amur leopard is one of the most endangered animals in the world.", "length": 68 }}
如需有关如何与 Unity 服务集成的信息,请参阅与其他 Unity 服务集成。

Copyright © 2026 Unity Technologies
法律信息隐私政策CookiesDocumentation Terms of Use请勿出售或分享我的个人信息您的隐私选择(Cookie 设置)

“Unity”、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属公司在美国和其他地方的商标或注册商标(此处查看更多信息)。其他名称或品牌是其各自所有者的商标。

为方便起见,一些页面是机器翻译的,可能包含不准确的内容。如有信息不一致的情况,以英文版本为准。

  • 在本页上
    • 使用 NuGet 包

    • 调用外部 API


报告此页面的问题