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

Unity中解决‘AmazonCognitoIdentityProviderClient.SignUp’保护级别不可访问错误

解决Unity中Cognito SignUp方法保护级别无法访问的问题

错误原因

AmazonCognitoIdentityProviderClient的同步SignUp方法为内部受保护权限,AWS SDK for .NET(含Unity适配版本)仅对外公开异步API,同步方法仅用于SDK内部实现,因此外部代码无法直接调用。

解决方案

改用异步SignUpAsync方法,并结合Unity的异步支持处理流程:

  • 将UI点击方法改为异步形式(Unity 2017及以上版本支持async/await)
  • 替换同步SignUp调用为await client.SignUpAsync(signup_request)
  • 保留异常捕获逻辑处理注册错误

修改后的完整代码

using System;
using UnityEngine;
using Amazon.CognitoIdentityProvider;
using Amazon.CognitoIdentityProvider.Model;
using TMPro;

public class Signup : MonoBehaviour
{
    private string clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";

    public TMP_InputField usernameField;
    public TMP_InputField passwordField;

    // 改为async void适配Unity UI事件回调
    public async void OnClick()
    {
        var client = new AmazonCognitoIdentityProviderClient(null, Amazon.RegionEndpoint.APNortheast1);
        var signup_request = new SignUpRequest
        {
            ClientId = clientId,
            Username = usernameField.text,
            Password = passwordField.text,
        };

        try
        {
            // 调用异步方法并等待结果
            var result = await client.SignUpAsync(signup_request);
            Debug.Log($"注册状态:用户已确认={result.UserConfirmed}");
        }
        catch (Exception ex)
        {
            Debug.LogError($"注册失败:{ex.Message}");
        }
    }
}

额外注意事项

  • 必须使用AWS SDK for Unity专用包,而非普通.NET SDK,Unity版已适配引擎线程模型
  • 若用户池客户端配置了客户端密钥,则不能传入null作为凭证提供者,需配置包含密钥的凭证信息
  • 异步操作中若需修改UI元素,需确保代码运行在Unity主线程(Debug.Log自动适配主线程,直接操作UI可借助Invoke或UnityMainThreadDispatcher)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 13:35:20