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设置
- 打开
Edit > Project Settings > Player - 在
Other Settings中,将Scripting Runtime Version设置为.NET 4.x Equivalent - 确保
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
相关产品推荐
相关产品推荐

