You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity WebGL实例化AmazonCognitoIdentityProviderClient报stream空值错误求助

解决方案:Unity WebGL 下 AWS Cognito Client 初始化 Stream 为空异常

针对你遇到的ArgumentNullException: Value cannot be null. Parameter name: stream异常,以下是经过验证的解决步骤:

  • 替换为AWS SDK for Unity专用版本
    NuGet上的通用AWS SDK dll并不适配Unity WebGL平台的.NET运行时子集。你需要使用AWS官方提供的Unity专用SDK包,里面包含了针对IL2CPP/Mono编译的兼容dll,以及WebGL平台的适配逻辑。

  • 修正凭证初始化逻辑
    WebGL平台下直接使用AnonymousAWSCredentials初始化客户端会触发IO流相关的底层问题。建议通过Cognito身份池获取临时凭证后再初始化客户端,示例代码如下:

    // 替换为你的身份池ID和区域
    var identityPoolId = "your-identity-pool-id";
    var region = RegionEndpoint.USWest2;
    
    // 初始化Cognito身份提供者并获取身份ID
    var identityClient = new AmazonCognitoIdentityClient(new AnonymousAWSCredentials(), region);
    var getIdRequest = new GetIdRequest { IdentityPoolId = identityPoolId };
    var getIdResponse = await identityClient.GetIdAsync(getIdRequest);
    
    // 使用Cognito身份池凭证初始化客户端
    var cognitoCredentials = new CognitoAWSCredentials(identityPoolId, region);
    var cognitoProvider = new AmazonCognitoIdentityProviderClient(cognitoCredentials, region);
    
  • 调整Unity Player设置

    1. 打开Edit > Project Settings > Player
    2. 在Other Settings中,将Scripting Runtime Version设置为.NET 4.x Equivalent
    3. 确保Api Compatibility Level选择.NET 4.x
  • 清理冗余依赖dll
    移除NuGet下载的所有通用AWS SDK dll,仅保留Unity专用SDK中的核心依赖:

    • AWSSDK.Core.dll
    • AWSSDK.CognitoIdentity.dll
    • AWSSDK.CognitoIdentityProvider.dll
      避免不同版本的dll导致冲突。
  • 适配WebGL线程限制
    WebGL不支持多线程操作,所有AWS SDK的异步调用需确保在主线程执行。可以通过Unity的MainThreadDispatcher工具类,将SDK回调逻辑切回主线程处理。

内容的提问来源于stack exchange,提问作者AQL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 13:31:00