เกมที่บันทึกไว้ในเกม Unity

หัวข้อนี้อธิบายถึงวิธีใช้เกมที่บันทึกไว้สำหรับบริการเกมของ Play ใน Unity เกม

ก่อนจะเริ่มต้น

แสดง UI ของเกมที่บันทึกไว้

UI มาตรฐานสำหรับการเลือกหรือสร้างรายการเกมที่บันทึกไว้จะแสดงโดย การโทร:

    void ShowSelectUI() {
        uint maxNumToDisplay = 5;
        bool allowCreateNew = false;
        bool allowDelete = true;

        ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
        savedGameClient.ShowSelectSavedGameUI("Select saved game",
            maxNumToDisplay,
            allowCreateNew,
            allowDelete,
            OnSavedGameSelected);
    }


    public void OnSavedGameSelected (SelectUIStatus status, ISavedGameMetadata game) {
        if (status == SelectUIStatus.SavedGameSelected) {
            // handle selected game save
        } else {
            // handle cancel or error
        }
    }

เปิดเกมที่บันทึกไว้

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

มีวิธีการแบบเปิด 2 วิธีที่ช่วยจัดการการแก้ไขข้อขัดแย้ง วิธีแรก OpenWithAutomaticRestrictResolution ยอมรับกลยุทธ์การแก้ปัญหามาตรฐาน และแก้ไขการทับซ้อนโดยอัตโนมัติ อีกวิธีหนึ่ง OpenWithManualClaimResolution ยอมรับเมธอด Callback เพื่ออนุญาตให้ การแก้ไขข้อขัดแย้งด้วยตนเอง

ดูรายละเอียดเพิ่มเติมได้ที่ GooglePlayGames/BasicApi/SavedGame/ISavedGameClient.cs เกี่ยวกับวิธีการเหล่านี้

    void OpenSavedGame(string filename) {
        ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
        savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,
            ConflictResolutionStrategy.UseLongestPlaytime, OnSavedGameOpened);
    }

    public void OnSavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game) {
        if (status == SavedGameRequestStatus.Success) {
            // handle reading or writing of saved game.
        } else {
            // handle error
        }
    }

เขียนเกมที่บันทึกไว้

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

  1. ข้อมูลเมตาของเกมที่บันทึกไว้ซึ่งส่งไปยัง Callback ที่ส่งไปยังการเรียกแบบเปิด
  2. เพื่ออัปเดตข้อมูลเมตา
  3. อาร์เรย์ไบต์จริงของข้อมูล
  4. Callback เพื่อเรียกเมื่อคอมมิตเสร็จสมบูรณ์
    void SaveGame (ISavedGameMetadata game, byte[] savedData, TimeSpan totalPlaytime) {
        ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;

        SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder();
        builder = builder
            .WithUpdatedPlayedTime(totalPlaytime)
            .WithUpdatedDescription("Saved game at " + DateTime.Now());
        if (savedImage != null) {
            // This assumes that savedImage is an instance of Texture2D
            // and that you have already called a function equivalent to
            // getScreenshot() to set savedImage
            // NOTE: see sample definition of getScreenshot() method below
            byte[] pngData = savedImage.EncodeToPNG();
            builder = builder.WithUpdatedPngCoverImage(pngData);
        }
        SavedGameMetadataUpdate updatedMetadata = builder.Build();
        savedGameClient.CommitUpdate(game, updatedMetadata, savedData, OnSavedGameWritten);
    }

    public void OnSavedGameWritten (SavedGameRequestStatus status, ISavedGameMetadata game) {
        if (status == SavedGameRequestStatus.Success) {
            // handle reading or writing of saved game.
        } else {
            // handle error
        }
    }

    public Texture2D getScreenshot() {
        // Create a 2D texture that is 1024x700 pixels from which the PNG will be
        // extracted
        Texture2D screenShot = new Texture2D(1024, 700);

        // Takes the screenshot from top left hand corner of screen and maps to top
        // left hand corner of screenShot texture
        screenShot.ReadPixels(
            new Rect(0, 0, Screen.width, (Screen.width/1024)*700), 0, 0);
        return screenShot;
    }

อ่านเกมที่บันทึกไว้

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

    void LoadGameData (ISavedGameMetadata game) {
        ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
        savedGameClient.ReadBinaryData(game, OnSavedGameDataRead);
    }

    public void OnSavedGameDataRead (SavedGameRequestStatus status, byte[] data) {
        if (status == SavedGameRequestStatus.Success) {
            // handle processing the byte array data
        } else {
            // handle error
        }
    }

ลบเกมที่บันทึกไว้

เมื่อเปิดไฟล์เกมที่บันทึกไว้แล้ว คุณจะลบไฟล์ดังกล่าวได้ โดยการเรียกใช้ ลบ

    void DeleteGameData (string filename) {
        // Open the file to get the metadata.
        ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
        savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,
            ConflictResolutionStrategy.UseLongestPlaytime, DeleteSavedGame);
    }

    public void DeleteSavedGame(SavedGameRequestStatus status, ISavedGameMetadata game) {
        if (status == SavedGameRequestStatus.Success) {
            ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
            savedGameClient.Delete(game);
        } else {
            // handle error
        }
    }