コード例 #1
0
        private IEnumerator LoadUsers(string[] ids, LeaderboardPlayerModel[] players, Action <LeaderboardPlayerModel[]> result)
        {
            bool loaded = false;
            bool error  = false;

            IUserProfile[] profiles = null;
            platform.LoadUsers(ids, (_profiles) =>
            {
                if (_profiles != null || _profiles.Length == 0)
                {
                    for (int a = 0; a < _profiles.Length; ++a)
                    {
                        IUserProfile profile = _profiles[a];
                        players[a].name      = profile.userName;
                        players[a].avatar    = profile.image;

                        Debug.LogFormat("User {0}. Name: {1}; Score: {2}; Id: {3}; Avatar: {4}",
                                        a, players[a].name, players[a].score, ids[a], players[a].avatar == null);
                    }
                    profiles = _profiles;
                    loaded   = true;
                }
                else
                {
                    Debug.LogWarning("Leaderboard: LoadUsers returned null or no profiles.");
                    loaded = true;
                    error  = true;
                    result.Invoke(null);
                }
            });

            //Wait to load Users
            while (!loaded)
            {
                yield return(new WaitForSeconds(0.3f));
            }

            if (!error)
            {
                Debug.Log("Leaderboard: Waiting to load avatars started.");
                //Wait to load avatars
                float secondsOfTrying   = 15;
                float secondsPerAttempt = 0.3f;
                loaded = false;
                while (secondsOfTrying > 0)
                {
                    int  nrAvatarsLoaded = 0;
                    int  index           = 0;
                    bool allLoaded       = true;
                    foreach (var plr in profiles)
                    {
                        if (plr.image == null)
                        {
                            allLoaded = false;
                        }
                        else
                        {
                            players[index].avatar = plr.image;
                            nrAvatarsLoaded++;
                        }
                        ++index;
                    }

                    Debug.LogFormat("Leaderboard: Loaded {0}/{1} avatars. Time remained: {2}",
                                    nrAvatarsLoaded, players.Length, secondsOfTrying);

                    if (allLoaded)
                    {
                        loaded = true;
                        break;
                    }

                    secondsOfTrying -= secondsPerAttempt;
                    yield return(new WaitForSeconds(secondsPerAttempt));
                }

                Debug.Log("Leaderboard: Waiting to load avatars finished.");
                result.Invoke(loaded ? players : null);
            }

            yield return(null);
        }