microsoft / xbox-live-unity-plugin

The Xbox Live Unity Plugin provides a way for developers in the Xbox Live Creators Program to quickly and easily integrate Xbox Live functionality into their Unity based game. For ID@Xbox developers, this Xbox Live Unity Plugin does not yet support all the features you will need. Instead, contact your Microsoft representative.
MIT License
167 stars 62 forks source link

SignInManager WindowsUser NULL in Release Builds - No WindowsUser #310

Closed NoelStephensNILC closed 2 years ago

NoelStephensNILC commented 3 years ago

Using Unity 2019.4.6f1, if you build either debug or release from within Unity editor for UWP and then build a RELEASE x64 version using the resulting solution from the editor's build then upon invoking the SignInPlayer Coroutine on Windows 10 the following call: Windows.System.User.FindAllAsync().AsTask();

Will always return a userTask status of "TaskStatus.RanToCompletion" but will return 0 (zero) results. This is problematic for the GameSaveHelper as it requires the WindowsUser in order to successfully call GameSaveProvider.GetForUserAsync during the GameSaveHelper's Initialize Coroutine.

If you run the same build generated from the editor in Debug mode, then it always returns a valid WindowsUser.

Any advice on how to get this functionality working in release?

Snigros commented 3 years ago

I think this is probably related to: https://forum.unity.com/threads/solved-uwp-xbox-live-creators-cloud-save-connected-storage-issue.655441/.

Using the information provided for that issue I modified the SignInPlayer couroutine (in SigninManager.cs) slightly to read as follows - hope this helps. Cheers, S.

    /// <summary>
    /// Adds and signs in a new Xbox Live User and assigns it a player number.
    /// Note: Different platforms support a different number of users. 
    /// AddUser might fail if the player number is not within the range of supported users.
    /// </summary>
    /// <param name="playerNumber">The Player Number that should be assigned to the Xbox Live User</param>
    public IEnumerator SignInPlayer(int playerNumber)
    {
        yield return null;

        if (!CurrentPlayers.ContainsKey(playerNumber))
        {
            XboxLivePlayerInfo newPlayerInfo = new XboxLivePlayerInfo()
            {
                SignInCallbacks = new List<UnityAction<XboxLiveUser, XboxLiveAuthStatus, string>>(),
                SignOutCallbacks = new List<UnityAction<XboxLiveUser, XboxLiveAuthStatus, string>>()
            };
            CurrentPlayers.Add(playerNumber, newPlayerInfo);
        }

if ENABLE_WINMD_SUPPORT

        // Modified from: https://forum.unity.com/threads/solved-uwp-xbox-live-creators-cloud-save-connected-storage-issue.655441/

        if (SystemInfo.deviceType == DeviceType.Console)
        {
            var autoPicker = new Windows.System.UserPicker { AllowGuestAccounts = false };
            var usersTask = autoPicker.PickSingleUserAsync().AsTask();
            CompleteXboxSignIn(playerNumber, usersTask);
        }
        else
        {
            var usersTask = Windows.System.User.FindAllAsync().AsTask();
            CompleteSignIn(playerNumber, usersTask);
        }

else

        CurrentPlayers[playerNumber].XboxLiveUser = new XboxLiveUser();
        XboxLiveUser.SignOutCompleted += XboxLiveUserSignOutCompleted;
        StartCoroutine(SignInAsync(playerNumber, CurrentPlayers[playerNumber]));

endif

    }

if ENABLE_WINMD_SUPPORT

    // Xbox console sign in...
    private async void CompleteXboxSignIn(int playerNumber, Task<User> usersTask)
    {
        var result = await usersTask;
        if (result != null)
        {
            CurrentPlayers[playerNumber].WindowsUser = usersTask.Result;
            CurrentPlayers[playerNumber].XboxLiveUser = new XboxLiveUser(CurrentPlayers[playerNumber].WindowsUser);
            XboxLiveUser.SignOutCompleted += XboxLiveUserSignOutCompleted;
            StartCoroutine(SignInAsync(playerNumber, CurrentPlayers[playerNumber]));
        }
        else
        {
            if (usersTask.Status == TaskStatus.Canceled)
            {
                NotifyAllCallbacks(playerNumber, null, XboxLiveAuthStatus.Canceled, "Xbox Sign In Failed: Player " + playerNumber + " failed. Status: Canceled by the User.");
            }
            else if (usersTask.Status == TaskStatus.Faulted)
            {
                ExceptionManager.Instance.ThrowException(
                    ExceptionSource.SignInManager,
                    ExceptionType.SignInFailed,
                    usersTask.Exception);
                NotifyAllCallbacks(playerNumber, null, XboxLiveAuthStatus.Failed, "Xbox Sign In Failed: Player " + playerNumber + " failed.");
            }
        }
    }

    // Windows 10 sign in
    private async void CompleteSignIn(int playerNumber, Task<IReadOnlyList<User>> usersTask)
    {
       var result = await usersTask;
       if (result != null && result.Count > 0)
       {
           var windowsUser = result[0];
           CurrentPlayers[playerNumber].WindowsUser = windowsUser;
           CurrentPlayers[playerNumber].XboxLiveUser = new XboxLiveUser(windowsUser);
           StartCoroutine(SignInAsync(playerNumber, CurrentPlayers[playerNumber]));
       }
       else
       {
           CurrentPlayers[playerNumber].XboxLiveUser = new XboxLiveUser();
           XboxLiveUser.SignOutCompleted += XboxLiveUserSignOutCompleted;
           StartCoroutine(SignInAsync(playerNumber, CurrentPlayers[playerNumber]));

           ExceptionManager.Instance.ThrowException(
                ExceptionSource.SignInManager,
                ExceptionType.SignInFailed,
                usersTask.Exception);
           NotifyAllCallbacks(playerNumber, null, XboxLiveAuthStatus.Failed, "Sign In Failed: Player " + playerNumber + " failed.");
       }
    }

endif`

MSFT-Heba commented 2 years ago

Hey everyone, Just for awareness in case this is still an issue, we've posted an announcement that integrating Xbox Live is no longer a requirement for UWP games to be published on Xbox as part of the Creators Program.

For more details: https://github.com/microsoft/xbox-live-unity-plugin#2021-update-more-accessible-creators-program-no-longer-requires-xbox-live