ลีดเดอร์บอร์ดในเกม Unity

หัวข้อนี้อธิบายวิธีใช้ลีดเดอร์บอร์ดของบริการเกมของ Play ในเกม Unity

ก่อนเริ่มต้นใช้งาน

ตั้งค่าโปรเจ็กต์ Unity และปลั๊กอิน Google Play Games สำหรับ Unity โปรดดูรายละเอียดในคู่มือเริ่มต้นใช้งานสำหรับ รายละเอียด

สร้างกิจกรรม

คุณสามารถสร้างลีดเดอร์บอร์ดใน Google Play Console โปรดดูรายละเอียดใน คู่มือลีดเดอร์บอร์ดสำหรับ บริการเกมของ Play หลังจากสร้างลีดเดอร์บอร์ดแล้ว ให้เพิ่มทรัพยากร Android ของลีดเดอร์บอร์ด ลงในปลั๊กอินตามที่อธิบายไว้ใน คู่มือเริ่มต้นใช้งาน

โพสต์คะแนนลงในลีดเดอร์บอร์ด

หากต้องการโพสต์คะแนนลงในลีดเดอร์บอร์ด ให้เรียก Social.ReportScore

    using GooglePlayGames;
    using UnityEngine.SocialPlatforms;
    ...
    // Post score 12345 to leaderboard ID "Cfji293fjsie_QA")
    Social.ReportScore(12345, "Cfji293fjsie_QA", (bool success) => {
        // Handle success or failure
    });

หากต้องการโพสต์คะแนนและใส่แท็กข้อมูลเมตา ให้ใช้PlayGamesPlatform อินสแตนซ์โดยตรง:

    using GooglePlayGames;
    using UnityEngine.SocialPlatforms;
    ...
    // Post score 12345 to leaderboard ID "Cfji293fjsie_QA" and tag "FirstDaily")
    PlayGamesPlatform.Instance.ReportScore(12345, "Cfji293fjsie_QA", "FirstDaily", (bool success) => {
        // Handle success or failure
    });

โปรดทราบว่าแพลตฟอร์มและเซิร์ฟเวอร์จะทิ้งคะแนนที่ต่ำกว่าคะแนนสูงสุดที่มีอยู่ของผู้เล่นโดยอัตโนมัติ คุณจึงส่งคะแนนได้อย่างอิสระโดยไม่ต้องตรวจสอบเพื่อทดสอบว่าคะแนนนั้นสูงกว่าคะแนนที่มีอยู่ของผู้เล่นหรือไม่

แสดง UI ของลีดเดอร์บอร์ด

หากต้องการแสดง UI ในตัวสำหรับลีดเดอร์บอร์ดทั้งหมด ให้เรียก Social.ShowLeaderboardUI

    using GooglePlayGames;
    using UnityEngine.SocialPlatforms;
    ...
    // Show leaderboard UI
    Social.ShowLeaderboardUI();

หากต้องการแสดงลีดเดอร์บอร์ดที่เฉพาะเจาะจงแทนลีดเดอร์บอร์ดทั้งหมด คุณสามารถส่งรหัสลีดเดอร์บอร์ดไปยังเมธอดได้ อย่างไรก็ตาม ฟังก์ชันนี้เป็นส่วนขยายของ Play Games ดังนั้นจึงต้องแคสต์ออบเจ็กต์ Social.Active เป็นออบเจ็กต์ PlayGamesPlatform ก่อน

    using GooglePlayGames;
    using UnityEngine.SocialPlatforms;
    ...
    // Show leaderboard UI
    PlayGamesPlatform.Instance.ShowLeaderboardUI("Cfji293fjsie_QA");

เข้าถึงข้อมูลลีดเดอร์บอร์ด

การดึงข้อมูลคะแนนลีดเดอร์บอร์ดทำได้ 2 วิธี

ใช้ Social.ILeaderboard

เมธอดนี้ใช้อินเทอร์เฟซ ILeaderboard เพื่อกำหนดขอบเขตและตัวกรองสำหรับการรับข้อมูล วิธีนี้ช่วยให้คุณกำหนดค่าสิ่งต่อไปนี้ได้ 1. รหัสลีดเดอร์บอร์ด 2. คอลเล็กชัน (โซเชียลหรือสาธารณะ) 3. กรอบเวลา (รายวัน รายสัปดาห์ ตลอดเวลา) 4. ตำแหน่งอันดับที่จะเริ่มดึงคะแนน 5. จำนวนคะแนน (ค่าเริ่มต้นคือ 25) 6. กรองตามรหัสผู้ใช้

หากพารามิเตอร์ from เป็นค่าที่ไม่เป็นบวก ระบบจะแสดงผลลัพธ์โดยเน้นที่ผู้เล่น ซึ่งหมายความว่าจะแสดงคะแนนรอบๆ คะแนนปัจจุบันของผู้เล่น

    ILeaderboard lb = PlayGamesPlatform.Instance.CreateLeaderboard();
    lb.id = "MY_LEADERBOARD_ID";
    lb.LoadScores(ok =>
        {
            if (ok) {
                LoadUsersAndDisplay(lb);
            }
            else {
                Debug.Log("Error retrieving leaderboardi");
            }
        });

ใช้ PlayGamesPlatform.LoadScores()

เมธอดนี้ใช้ PlayGamesPlatform โดยตรง ซึ่งให้ความยืดหยุ่นและข้อมูลเพิ่มเติมเมื่อเข้าถึงข้อมูลลีดเดอร์บอร์ด

    PlayGamesPlatform.Instance.LoadScores(
            GPGSIds.leaderboard_leaders_in_smoketesting,
            LeaderboardStart.PlayerCentered,
            100,
            LeaderboardCollection.Public,
            LeaderboardTimeSpan.AllTime,
            (data) =>
            {
                mStatus = "Leaderboard data valid: " + data.Valid;
                mStatus += "\n approx:" +data.ApproximateCount + " have " + data.Scores.Length;
            });

พารามิเตอร์สำหรับ LoadScores() มีดังนี้

  1. leaderboardId
  2. ตำแหน่งเริ่มต้น (คะแนนสูงสุดหรือเน้นที่ผู้เล่น)
  3. จำนวนแถว
  4. คอลเล็กชันลีดเดอร์บอร์ด (โซเชียลหรือสาธารณะ)
  5. ช่วงเวลา (รายวัน รายสัปดาห์ ตลอดเวลา)
  6. การเรียกกลับที่ยอมรับออบเจ็กต์ LeaderboardScoreData

คลาส LeaderboardScoreData ใช้เพื่อแสดงข้อมูลกลับไปยัง ผู้เรียกเมื่อโหลดคะแนน สมาชิกมีดังนี้

1. Id - the leaderboard id
2. Valid - true if the returned data is valid (the call was successful)
3. Status - the ResponseStatus of the call
4. ApproximateCount - the approximate number of scores in the leaderboard
5. Title - the title of the leaderboard
6. PlayerScore - the score of the current player
7. Scores - the list of scores
8. PrevPageToken - a token that can be used to call `LoadMoreScores()` to
    get the previous page of scores.
9. NextPageToken - a token that can be used to call `LoadMoreScores()` to
    get the next page of scores.
    void GetNextPage(LeaderboardScoreData data)
    {
        PlayGamesPlatform.Instance.LoadMoreScores(data.NextPageToken, 10,
            (results) =>
            {
                mStatus = "Leaderboard data valid: " + data.Valid;
                mStatus += "\n approx:" +data.ApproximateCount + " have " + data.Scores.Length;
            });
    }

การเรียกนี้อาจล้มเหลวเมื่อพยายามโหลดเพื่อนด้วย ResponseCode.ResolutionRequired หากผู้ใช้ไม่ได้แชร์รายชื่อเพื่อนกับเกม ในกรณีนี้ ให้ใช้ AskForLoadFriendsResolution เพื่อขอสิทธิ์เข้าถึง

รับชื่อผู้เล่น

คะแนนแต่ละรายการจะมี userId ของผู้เล่นที่ทำคะแนน คุณสามารถใช้ Social.LoadUsers() เพื่อโหลดโปรไฟล์ผู้เล่น โปรดทราบว่าเนื้อหาของโปรไฟล์ผู้เล่นขึ้นอยู่กับการตั้งค่าความเป็นส่วนตัวของผู้เล่น

    internal void LoadUsersAndDisplay(ILeaderboard lb)
    {
        // Get the user ids
        List<string> userIds = new List<string>();

        foreach(IScore score in lb.scores) {
            userIds.Add(score.userID);
        }
        // Load the profiles and display (or in this case, log)
        Social.LoadUsers(userIds.ToArray(), (users) =>
            {
                string status = "Leaderboard loading: " + lb.title + " count = " +
                    lb.scores.Length;
                foreach(IScore score in lb.scores) {
                    IUserProfile user = FindUser(users, score.userID);
                    status += "\n" + score.formattedValue + " by " +
                        (string)(
                            (user != null) ? user.userName : "**unk_" + score.userID + "**");
                }
                Debug.log(status);
            });
    }