เมื่อผสานรวมการนำส่งเนื้อหา เกม Unity จะเข้าถึง Asset Pack ได้โดยใช้ Addressables หรือ AssetBundle Addressables เป็นโซลูชันการนำส่งชิ้นงานที่ใหม่และแนะนำสำหรับเกมที่สร้างด้วย Unity 2019.4 ขึ้นไป ส่วน AssetBundles รองรับแพ็กชิ้นงานใน Unity 2017.4 และ 2018.4
Unity Addressables
เกมที่สร้างด้วย Unity 2019.4 ขึ้นไปควรใช้ Addressables ในการนําส่งเนื้อหาใน Android Unity มี Play Asset Delivery (PAD) API สำหรับจัดการ Asset Pack ของ Android โดยใช้ Addressables ดูข้อมูลเกี่ยวกับการใช้รายการที่ระบุได้ที่หัวข้อต่อไปนี้
- ที่อยู่สำหรับแพ็กเกจ Android
- คู่มือ PAD สําหรับ Unity
- เอกสารอ้างอิง PAD API สำหรับ Unity
ใช้ไฟล์ AssetBundle
เกมที่สร้างด้วย Unity 2017.4 และ 2018.4 สามารถใช้ไฟล์ AssetBundle สำหรับการนำส่งเนื้อหาใน Android ได้ ไฟล์ AssetBundle ของ Unity มีชิ้นงานที่แปลงเป็นอนุกรม ซึ่งเครื่องมือของ Unity จะโหลดได้ขณะที่แอปทำงานอยู่ ไฟล์เหล่านี้มีไว้สำหรับแพลตฟอร์มหนึ่งๆ โดยเฉพาะ (เช่น สร้างขึ้นสำหรับ Android) และใช้ร่วมกับ Asset Pack ได้ โดยทั่วไปแล้ว ระบบจะแพ็กไฟล์ AssetBundle 1 ไฟล์เป็นแพ็กเกจชิ้นงานเดียว โดยแพ็กเกจจะใช้ชื่อเดียวกับ AssetBundle หากต้องการความยืดหยุ่นมากขึ้นในการสร้างแพ็กชิ้นงาน ให้กำหนดค่าแพ็กชิ้นงานโดยใช้ API
ขณะรันไทม์ ให้ใช้คลาส Play Asset Delivery สำหรับ Unity เพื่อเรียกข้อมูล AssetBundle ที่แพ็กไว้ใน Asset Pack
สิ่งที่ต้องมีก่อน
- ตั้งค่าสภาพแวดล้อมการพัฒนาซอฟต์แวร์
OpenUPM-CLI
หากติดตั้ง OpenUPM CLI แล้ว คุณจะติดตั้งรีจิสทรี OpenUPM ได้ด้วยคำสั่งต่อไปนี้
openupm add com.google.play.assetdelivery
OpenUPM
เปิดการตั้งค่าเครื่องมือจัดการแพ็กเกจโดยเลือกตัวเลือกเมนู Unity แก้ไข > การตั้งค่าโปรเจ็กต์ > เครื่องมือจัดการแพ็กเกจ
เพิ่ม OpenUPM เป็นรีจิสทรีที่มีขอบเขตไปยังหน้าต่างตัวจัดการแพ็กเกจ โดยทำดังนี้
Name: package.openupm.com URL: https://package.openupm.com Scopes: com.google.external-dependency-manager com.google.play.common com.google.play.core com.google.play.assetdelivery com.google.android.appbundle
เปิดเมนูเครื่องมือจัดการแพ็กเกจโดยเลือกตัวเลือกเมนู Unity หน้าต่าง > เครื่องมือจัดการแพ็กเกจ
ตั้งค่าเมนูแบบเลื่อนลงของขอบเขตบัญชีดูแลจัดการเพื่อเลือกรีจิสทรีของฉัน
เลือกแพ็กเกจ Google Play Integrity Plugin for Unity จากรายการแพ็กเกจ แล้วกดติดตั้ง
นำเข้าจาก GitHub
ดาวน์โหลด
.unitypackage
เวอร์ชันล่าสุดจาก GitHubนําเข้าไฟล์
.unitypackage
โดยเลือกตัวเลือกเมนู Unity ชิ้นงาน > นําเข้าแพ็กเกจ > แพ็กเกจที่กําหนดเอง และนําเข้ารายการทั้งหมด
กำหนดค่า AssetBundle โดยใช้ UI
กำหนดค่า AssetBundle แต่ละรายการในแพ็กเกจชิ้นงาน
- เลือก Google > Android App Bundle > การตั้งค่าการนำส่งเนื้อหา
- หากต้องการเลือกโฟลเดอร์ที่มีไฟล์ AssetBundle โดยตรง ให้คลิกเพิ่มโฟลเดอร์
สําหรับแต่ละแพ็กเกจ ให้เปลี่ยนโหมดการแสดงโฆษณาเป็นเวลาติดตั้ง รวดเร็ว ติดตาม หรือออนดีมานด์ แก้ไขข้อผิดพลาดหรือข้อกําหนดเบื้องต้น แล้วปิดหน้าต่าง
เลือก Google > สร้าง Android App Bundle เพื่อสร้าง App Bundle
(ไม่บังคับ) กำหนดค่า App Bundle ให้รองรับรูปแบบการบีบอัดพื้นผิวที่แตกต่างกัน
กำหนดค่าชุดชิ้นงานโดยใช้ API
คุณสามารถกําหนดค่าการส่งชิ้นงานผ่านสคริปต์เครื่องมือแก้ไขซึ่งสามารถเรียกใช้ได้เป็นส่วนหนึ่งของระบบบิลด์อัตโนมัติ
ใช้คลาส AssetPackConfig
เพื่อกำหนดชิ้นงานที่จะรวมไว้ในบิลด์ Android App Bundle รวมถึงโหมดการนำส่งของชิ้นงาน Asset Pack เหล่านี้ไม่จำเป็นต้องมี AssetBundle
public void ConfigureAssetPacks { // Creates an AssetPackConfig with a single asset pack, named // examplePackName, containing all the files in path/to/exampleFolder. var assetPackConfig = new AssetPackConfig(); assetPackConfig.AddAssetsFolder("examplePackName", "path/to/exampleFolder", AssetPackDeliveryMode.OnDemand); // Configures the build system to use the newly created assetPackConfig when // calling Google > Build and Run or Google > Build Android App Bundle. AssetPackConfigSerializer.SaveConfig(assetPackConfig); // Alternatively, use BundleTool.BuildBundle to build an App Bundle from script. BuildBundle(new buildPlayerOptions(), assetPackConfig); }
นอกจากนี้ คุณยังใช้เมธอดแบบคงที่ BuildBundle
ในคลาส Bundletool
เพื่อสร้าง App Bundle ของ Android ที่มีแพ็กเกจเนื้อหาได้ โดยให้ BuildPlayerOptions และ AssetPackConfig
ดูบทแนะนำแบบมีคำแนะนำได้ที่ การใช้ Play Asset Delivery ใน Codelab ของเกม Unity
ผสานรวมกับ Play Asset Delivery Unity API
Play Asset Delivery Unity API มีฟังก์ชันการทำงานสำหรับการขอ Asset Pack, จัดการการดาวน์โหลด และเข้าถึงเนื้อหา อย่าลืมเพิ่มปลั๊กอิน Unity ลงในโปรเจ็กต์ก่อน
ฟังก์ชันที่คุณใช้ใน API จะขึ้นอยู่กับวิธีสร้างชุดชิ้นงาน
หากคุณสร้างชุดชิ้นงานโดยใช้ UI ของปลั๊กอิน ให้เลือกชุดชิ้นงานที่กําหนดค่าด้วยปลั๊กอิน
หากคุณสร้างชุดชิ้นงานโดยใช้ API (หรือ UI ของปลั๊กอิน) ให้เลือกชุดชิ้นงานที่กําหนดค่า API
API นี้คล้ายกันไม่ว่าจะส่งแพ็กเนื้อหาประเภทใดก็ตามที่คุณต้องการเข้าถึง ขั้นตอนเหล่านี้แสดงอยู่ในแผนภาพต่อไปนี้
รูปที่ 1 แผนภาพขั้นตอนในการเข้าถึงชุดชิ้นงาน
เรียกข้อมูลชุดชิ้นงาน
นําเข้าคลังการส่งเนื้อหาของ Play และเรียกใช้เมธอด RetrieveAssetPackAsync()
เพื่อดาวน์โหลดแพ็กเนื้อหา หากแพ็กเวอร์ชันล่าสุดยังไม่มีอยู่ในดิสก์
using Google.Play.AssetDelivery; // After download, the assets and/or AssetBundles contained in the asset pack // are not loaded into memory. PlayAssetPackRequest request = PlayAssetDelivery.RetrieveAssetPackAsync(assetPackName);
การนำส่งเมื่อติดตั้ง
Asset Pack ที่กําหนดค่าเป็น install-time
จะพร้อมใช้งานทันทีที่เปิดแอป แต่คุณต้องโหลดชิ้นงานลงในหน่วยความจํา ดูหัวข้อโหลดชิ้นงานลงในหน่วยความจํา
การแสดงโฆษณาตามอย่างรวดเร็วและแบบออนดีมานด์
ส่วนเหล่านี้มีผลกับ Asset Pack ของ fast-follow
และ on-demand
ตรวจสอบสถานะ
ชุดชิ้นงานแต่ละชุดจะจัดเก็บไว้ในโฟลเดอร์แยกต่างหากในพื้นที่เก็บข้อมูลภายในของแอป
ใช้วิธี isDone()
เพื่อตรวจสอบว่าแพ็กเกจชิ้นงานได้รับการดาวน์โหลดแล้วและพร้อมใช้งานหรือไม่ หรือเกิดข้อผิดพลาดขึ้นหรือไม่
ตรวจสอบการดาวน์โหลด
ค้นหาออบเจ็กต์ PlayAssetPackRequest
เพื่อตรวจสอบสถานะของคำขอ
// Download progress of request, between 0.0f and 1.0f. The value will always be // 1.0 for assets delivered as install-time. // NOTE: A value of 1.0 does not mean that the request has completed, only that // the DOWNLOADING stage is finished. float progress = request.DownloadProgress; // Returns the status of the retrieval request. // If the request completed successfully, this value should be AssetDeliveryStatus.Available. // If an error occurred, this value should be AssetDeliveryStatus.Failed. AssetDelivery status = request.Status; switch(status) { case AssetDeliveryStatus.Pending: // Asset pack download is pending - N/A for install-time assets. case AssetDeliveryStatus.Retrieving: // Asset pack is being downloaded and transferred to app storage. // N/A for install-time assets. case AssetDeliveryStatus.Available: // Asset pack is downloaded on disk but NOT loaded into memory. // For PlayAssetPackRequest(), this indicates that the request is complete. case AssetDeliveryStatus.Failed: // Asset pack retrieval failed. case AssetDeliveryStatus.WaitingForWifi: // Asset pack retrieval paused until either the device connects via Wi-Fi, // or the user accepts the PlayAssetDelivery.ShowConfirmationDialog dialog. case AssetDeliveryStatus.RequiresUserConfirmation: // Asset pack retrieval paused until the user accepts the // PlayAssetDelivery.ShowConfirmationDialog dialog. default: break; } // Returns true if status is AssetDeliveryStatus.Available or AssetDeliveryStatus.Failed. bool done = request.IsDone; // If AssetDeliveryStatus.Failed, find more info about the error. AssetDeliveryErrorCode error = request.Error;
การดาวน์โหลดขนาดใหญ่
แพ็กชิ้นงานที่มีขนาดใหญ่กว่า 200 MB จะดาวน์โหลดโดยอัตโนมัติได้ก็ต่อเมื่ออุปกรณ์เชื่อมต่อกับ Wi-Fi เท่านั้น หากผู้ใช้ไม่ได้ใช้ Wi-Fi ระบบจะตั้งค่าสถานะ PlayAssetPackRequest
ให้AssetDeliveryStatus.WaitingForWifi
และหยุดการดาวน์โหลดชั่วคราว ในกรณีนี้ ให้รอจนกว่าอุปกรณ์จะเชื่อมต่อ Wi-Fi แล้วดาวน์โหลดต่อ หรือแจ้งให้ผู้ใช้อนุมัติการดาวน์โหลดแพ็กเกจผ่านการเชื่อมต่อมือถือ
การยืนยันผู้ใช้ที่จําเป็น
หากแพ็กมีสถานะ AssetDeliveryStatus.RequiresUserConfirmation
การดาวน์โหลดจะไม่ดำเนินการต่อจนกว่าผู้ใช้จะยอมรับกล่องโต้ตอบที่แสดงพร้อมกับ PlayAssetDelivery.ShowConfirmationDialog()
สถานะนี้อาจเกิดขึ้นหาก Play ไม่รู้จักแอป โปรดทราบว่าการเรียกใช้ PlayAssetDelivery.ShowConfirmationDialog()
ในกรณีนี้จะทำให้แอปอัปเดต หลังจากอัปเดตแล้ว ให้ขอชิ้นงานอีกครั้ง
if(request.Status == AssetDeliveryStatus.RequiresUserConfirmation || request.Status == AssetDeliveryStatus.WaitingForWifi) { var userConfirmationOperation = PlayAssetDelivery.ShowConfirmationDialog(); yield return userConfirmationOperation; switch(userConfirmationOperation.GetResult()) { case ConfirmationDialogResult.Unknown: // userConfirmationOperation finished with an error. Something went // wrong when displaying the prompt to the user, and they weren't // able to interact with the dialog. case ConfirmationDialogResult.Accepted: // User accepted the confirmation dialog--an update will start. case ConfirmationDialogResult.Declined: // User canceled or declined the dialog. It can be shown again. default: break; } }
ยกเลิกคำขอ (แบบออนดีมานด์เท่านั้น)
หากต้องการยกเลิกคำขอก่อนที่จะมีการดาวน์โหลด Asset Pack ให้เรียกใช้เมธอด AttemptCancel()
ในออบเจ็กต์ PlayAssetPackRequest
ดังนี้
// Will only attempt if the status is Pending, Retrieving, or Available; otherwise // it will be a no-op. request.AttemptCancel(); // Check to see if the request was successful by checking if the error code is Canceled. if(request.Error == AssetDeliveryErrorCode.Canceled) { // Request was successfully canceled. }
โหลดชิ้นงานลงในหน่วยความจํา
เมื่อคําขอเสร็จสมบูรณ์แล้ว ให้ใช้ฟังก์ชันใดฟังก์ชันหนึ่งต่อไปนี้เพื่อโหลดชิ้นงานลงในหน่วยความจํา
- ใช้
PlayAssetPackRequest.GetAssetLocation()
เพื่อรับออบเจ็กต์AssetLocation
ข้อมูลนี้ระบุเส้นทาง การออฟเซต และขนาดของชิ้นงานเพื่อให้โหลดจากดิสก์ได้ - หากชิ้นงานเป็น AssetBundle คุณจะใช้เมธอดที่สะดวกได้
PlayAssetPackRequest.LoadAssetBundleAsync(assetPath)
เส้นทางชิ้นงานที่ส่งควรสอดคล้องกับเส้นทางไปยัง AssetBundle จากภายในแพ็กชิ้นงาน ซึ่งจะแสดงผลเป็น AssetBundleCreateRequest
ขอ Asset Pack แบบอะซิงโครนัส
ในกรณีส่วนใหญ่ คุณควรใช้ Coroutines เพื่อขอ Asset Pack แบบไม่พร้อมกันและตรวจสอบความคืบหน้า ดังที่แสดงต่อไปนี้
private IEnumerator LoadAssetPackCoroutine(string assetPackName) { PlayAssetPackRequest request = PlayAssetDelivery.RetrieveAssetPackAsync(assetPackName); while (!request.IsDone) { if(request.Status == AssetDeliveryStatus.WaitingForWifi) { var userConfirmationOperation = PlayAssetDelivery.ShowConfirmationDialog(); // Wait for confirmation dialog action. yield return userConfirmationOperation; if((userConfirmationOperation.Error != AssetDeliveryErrorCode.NoError) || (userConfirmationOperation.GetResult() != ConfirmationDialogResult.Accepted)) { // The user did not accept the confirmation. Handle as needed. } // Wait for Wi-Fi connection OR confirmation dialog acceptance before moving on. yield return new WaitUntil(() => request.Status != AssetDeliveryStatus.WaitingForWifi); } // Use request.DownloadProgress to track download progress. // Use request.Status to track the status of request. yield return null; } if (request.Error != AssetDeliveryErrorCode.NoError) { // There was an error retrieving the pack. For error codes NetworkError // and InsufficientStorage, you may prompt the user to check their // connection settings or check their storage space, respectively, then // try again. yield return null; } // Request was successful. Load the asset pack into memory. AssetBundleCreateRequest assetBundleCreateRequest = request.LoadAssetBundleAsync(path/to/exampleBundle); yield return assetBundleCreateRequest; AssetBundle assetBundle = assetBundleCreateRequest.assetBundle;
ดูข้อมูลเพิ่มเติมเกี่ยวกับการจัดการข้อผิดพลาดได้ที่รายการรหัสข้อผิดพลาด
เมธอดอื่นๆ ของ Play Core API
ต่อไปนี้คือเมธอด API เพิ่มเติมที่คุณอาจต้องการใช้ในแอป
เรียกข้อมูล Asset Pack หลายรายการ
หากต้องการเรียกข้อมูลแพ็กเนื้อหาหลายรายการพร้อมกัน ให้ใช้ฟังก์ชันต่อไปนี้
// assetPackNames is an array of strings corresponding to asset packs. PlayAssetPackBatchRequest batchRequest = PlayAssetDelivery.RetrieveAssetPackBatchAsync(<IListstring> assetPackNames);
ติดตามสถานะของคําขอแต่ละรายการโดยตรวจสอบDictionary
สถานะต่อไปนี้
// Dictionary of AssetPackStates, with the asset pack name as the key. Dictionary<string, PlayAssetPackRequest> requests = batchRequest.Requests; // Returns true if all requests are complete. bool requestComplete = batchRequest.IsDone;
ตรวจสอบขนาดการดาวน์โหลด
ตรวจสอบขนาดของแพ็กเกจชิ้นงานโดยการเรียกใช้ Google Play แบบไม่พร้อมกันและตั้งค่าเมธอดการเรียกกลับเมื่อการดำเนินการเสร็จสมบูรณ์
public IEnumerator GetDownloadSize() { PlayAsyncOperation<long> getSizeOperation = PlayAssetDelivery.GetDownloadSize(assetPackName); yield return getSizeOperation; if(operation.Error != AssetDeliveryErrorCode.NoError) { // Error while retrieving download size. } else { // Download size is given in bytes. long downloadSize = operation.GetResult(); } }
นํา AssetBundles ออก
คุณสามารถนำชุดชิ้นงานติดตามอย่างรวดเร็วและแบบออนดีมานด์ออกได้หากไม่ได้โหลดไว้ในหน่วยความจำ ทำการเรียกแบบไม่พร้อมกันต่อไปนี้และตั้งค่าเมธอดการเรียกกลับเมื่อดำเนินการเสร็จสมบูรณ์
PlayAsyncOperation<string> removeOperation = PlayAssetDelivery.RemoveAssetPack(assetBundleName); removeOperation.Completed += (operation) => { if(operation.Error != AssetDeliveryErrorCode.NoError) { // Error while attempting to remove AssetBundles. } else { // Files were deleted OR files did not exist to begin with. } };
ขั้นตอนถัดไป
ทดสอบการนำส่งเนื้อหาในเครื่องและจาก Google Play