เกมที่บันทึกไว้ในเกม 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 วิธีที่เปิดให้ใช้ในการจัดการการแก้ไขความขัดแย้ง วิธีแรกคือ OpenWithAutomaticConflictResolution ซึ่งยอมรับกลยุทธ์การแก้ไขมาตรฐาน ประเภทและแก้ไขความขัดแย้งโดยอัตโนมัติ ส่วนอีกวิธีหนึ่งคือ OpenWithManualConflictResolution ซึ่งยอมรับวิธีการเรียกกลับเพื่อให้ แก้ไขความขัดแย้งด้วยตนเองได้

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

    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 พารามิเตอร์สำหรับ CommitUpdate มี 4 รายการดังนี้

  1. ข้อมูลเมตาของเกมที่บันทึกไว้ซึ่งส่งไปยังการเรียกกลับที่ส่งไปยังการเรียก Open รายการใดรายการหนึ่ง
  2. การอัปเดตที่จะดำเนินการกับข้อมูลเมตา
  3. อาร์เรย์ไบต์จริงของข้อมูล
  4. การเรียกกลับเพื่อโทรเมื่อการคอมมิตเสร็จสมบูรณ์
    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
        }
    }

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

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

    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
        }
    }