0

目前我正在使用来自 EvilMindDev 的华为插件。

下面是 AccountManager 脚本。

using HuaweiMobileServices.Id;
using HuaweiMobileServices.Utils;
using System;
using UnityEngine;

namespace HmsPlugin
{
    public class AccountManager : MonoBehaviour
    {

    public static AccountManager GetInstance(string name = "AccountManager") => GameObject.Find(name).GetComponent<AccountManager>();

    private static HuaweiIdAuthService DefaultAuthService
    {
        get
        {
            Debug.Log("[HMS]: GET AUTH");
            var authParams = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM).SetIdToken().CreateParams();
            Debug.Log("[HMS]: AUTHPARAMS AUTHSERVICE" + authParams);
            var result = HuaweiIdAuthManager.GetService(authParams);
            Debug.Log("[HMS]: RESULT AUTHSERVICE"+ result);
            return result;
        }
    }

    public AuthHuaweiId HuaweiId { get; private set; }
    public Action<AuthHuaweiId> OnSignInSuccess { get; set; }
    public Action<HMSException> OnSignInFailed { get; set; }

    private HuaweiIdAuthService authService;

    // Start is called before the first frame update
    void Awake()
    {
        Debug.Log("[HMS]: AWAKE AUTHSERVICE");
        authService = DefaultAuthService;
        Debug.Log("DefaultAuthService : "+DefaultAuthService);
        Debug.Log("authService : "+authService);
    }

    public void SignIn()
    {
        Debug.Log("[HMS]: Sign in " + authService);
        authService.StartSignIn((authId) =>
        {
            HuaweiId = authId;
            Debug.Log("HuaweiId : "+HuaweiId);
            OnSignInSuccess?.Invoke(authId);
        }, (error) =>
        {
            HuaweiId = null;
            OnSignInFailed?.Invoke(error);
        });
    }

    public void SignOut()
    {
        Debug.Log("authService.SignOut");
        authService.SignOut();
        HuaweiId = null;
    }
}
}

下面是 AccountSignIn 脚本。

using HuaweiMobileServices.Id;
using HuaweiMobileServices.Utils;
using UnityEngine;
using UnityEngine.UI;
using HmsPlugin;
public class AccountSignIn : MonoBehaviour
{

private const string NOT_LOGGED_IN = "No user logged in";
private const string LOGGED_IN = "{0} is logged in";
private const string LOGIN_ERROR = "Error or cancelled login";

private Text loggedInUser;
private AccountManager accountManager;

// Start is called before the first frame update
void Start()
{
    loggedInUser = GameObject.Find("LoggedUserText").GetComponent<Text>();
    loggedInUser.text = NOT_LOGGED_IN;

    //accountManager = AccountManager.GetInstance();
    accountManager = GetComponent<AccountManager>();
    accountManager.OnSignInSuccess = OnLoginSuccess;
    accountManager.OnSignInFailed = OnLoginFailure;
    LogIn();
}

public void LogIn()
{
    accountManager.SignIn();
}

public void LogOut()
{
    accountManager.SignOut();
    loggedInUser.text = NOT_LOGGED_IN;
}

public void OnLoginSuccess(AuthHuaweiId authHuaweiId)
{
    loggedInUser.text = string.Format(LOGGED_IN, authHuaweiId.DisplayName);
}

public void OnLoginFailure(HMSException error)
{
    loggedInUser.text = LOGIN_ERROR;
}
}

每次我尝试登录它都会给我这个错误。 是HuaweiIdAuthService。

即使我尝试提供的演示也会给我同样的错误。如果我尝试使用 Android Studio 进行调试,它仍然会给我同样的错误。

public void SignIn()
{
    Debug.Log("[HMS]: Sign in " + authService);
    authService.StartSignIn((authId) =>
    {
        HuaweiId = authId;
        Debug.Log("HuaweiId : "+HuaweiId);
        OnSignInSuccess?.Invoke(authId);
    }, (error) =>
    {
        HuaweiId = null;
        OnSignInFailed?.Invoke(error);
    });
}

authService 不返回任何内容。我可以从哪里得到它?

4

2 回答 2

0

它是一个空指针。请检查未分配的对象。如果您找不到任何东西,请删除项目并重新安装,因为有时会发生此类事情。

于 2020-09-25T06:13:47.070 回答
0

这个插件有 Unity 2019 和 Unity 2018 的 2 个分支。

  1. 您应该在华为应用市场中激活 Account Kit API
  2. 检查配置 AndroidManifest 文件
  3. 检查 Agconnect-service.json 文件
于 2020-09-30T06:34:07.870 回答