搭配 Unity 使用 Google Play 免安裝

Unity 的 Google Play 免安裝外掛程式會設定 Unity 專案,以便建立遊戲的即時應用程式版本。本指南說明如何安裝及使用這個外掛程式。

下載並匯入外掛程式

這個外掛程式是 Unity 專用 Google Play 外掛程式的一部分。如要匯入外掛程式,請按照下列步驟操作:

  1. Unity 專用 Google Play 外掛程式下載最新版本。
  2. 依序選取「Assets」>「Import package」>「Custom Package」,並匯入所有項目,即可匯入 .unitypackage 檔案。

Unity 編輯器功能

匯入外掛程式,在 Unity 中新增「Google」>「Play Instant」子選單。這個子選單提供以下選項。

版本設定

會開啟一個視窗,可用於在「Installed」和「Instant」開發模式之間切換。切換至即時設定會產生下列影響:

  • 建立名為 PLAY_INSTANT 的指令碼定義符號,以便用於使用 #if PLAY_INSTANT#endif 編寫指令碼。
  • 管理 AndroidManifest.xml 的更新,以便因應某些必要變更,例如 android:targetSandboxVersion

播放器設定

圖 1 所示的「Player Settings」對話方塊會顯示建議,協助您針對 Google Play 免安裝功能進行最佳化調整、根據更相容的圖形 API 進行開發,以及縮減 APK 的大小。

具體建議包括僅使用 OpenGL ES 2.0,以及停用多執行緒轉譯功能。
圖 1. 「Player Settings」對話方塊

這些播放器設定分為「必要」和「建議」設定。如果設定有對應的「Update」按鈕,按一下該按鈕即可將設定變更為偏好的值。

如要進一步縮減 APK 大小,請開啟 Unity 套件管理員,並移除任何未使用的套件。

快速部署

快速部署功能可將部分資產封裝到 AssetBundle 中,藉此縮減 Unity 免安裝應用程式的大小。使用快速部署功能時,Unity 遊戲引擎和載入畫面會封裝到免安裝應用程式 APK 中,並在免安裝應用程式啟動後從伺服器擷取 AssetBundle。

支援安裝工作流程

許多免安裝應用程式的目標是讓使用者在安裝完整版本前體驗應用程式。適用於 Unity 的 Google Play 免安裝外掛程式提供的 API 可以顯示 Play 商店安裝對話方塊,以及將狀態從立即安裝到已安裝的應用程式。

顯示安裝提示

具有「Install」按鈕的免安裝應用程式,可以透過安裝按鈕點擊處理常式呼叫以下內容,顯示 Play 商店安裝對話方塊:

Google.Play.Instant.InstallLauncher.ShowInstallPrompt();

ShowInstallPrompt() 方法的超載允許以下一或多項情況:

  • 判斷使用者是否退出安裝程序。請在免安裝應用程式的主要活動中覆寫 onActivityResult(),並在指定的 requestCode 上檢查 RESULT_CANCELED
  • 透過 referrer 參數傳遞安裝參照網址字串。
  • 透過 PutPostInstallIntentStringExtra() 傳遞目前遊戲工作階段的狀態。

請參考以下範例:

using Google.Play.Instant;
...
const int requestCode = 123;
var sessionInfo = /* Object serialized as a string representing player's current location, etc. */;
using (var activity = UnityPlayerHelper.GetCurrentActivity())
using (var postInstallIntent = InstallLauncher.CreatePostInstallIntent(activity))
{
    InstallLauncher.PutPostInstallIntentStringExtra(postInstallIntent, "sessionInfo", sessionInfo);
    InstallLauncher.ShowInstallPrompt(activity, requestCode, postInstallIntent, "test-referrer");
}

如果使用者完成應用程式安裝程序,Play 商店會使用提供的 postInstallIntent 重新啟動應用程式。安裝版應用程式可使用以下程式碼擷取 postInstallIntent 中的值集:

var sessionInfo = InstallLauncher.GetPostInstallIntentStringExtra("sessionInfo");

附註:

  • 如果使用者安裝應用程式後選擇取消啟動作業,postInstallIntent 內含的額外項目可能無法存取已安裝的應用程式。相較於保留持續狀態,傳送意圖額外項目更適合用於保留使用中的工作階段狀態;後者是指 Cookie API。
  • 任何人都可以建構具有額外欄位的意圖來啟動已安裝的應用程式,因此,如果酬載授予有價值的項目,請設計酬載,使其只能使用一次、加密簽署,並驗證伺服器上的簽名。

Cookie API 提供各種方法,用於將免安裝應用程式中的 Cookie (例如玩家 ID 或關卡完成資料) 從免安裝應用程式傳遞至對應的安裝版應用程式。與 postInstallIntent 額外項目不同,Cookie 狀態即使使用者並未立即啟動已安裝的應用程式,還是可以查看 Cookie 狀態。舉例來說,免安裝應用程式可以從安裝按鈕點擊處理常式呼叫下列程式碼:

using Google.Play.Instant;
...
var playerInfo = /* Object serialized as a string representing game levels completed, etc. */;
var cookieBytes = System.Text.Encoding.UTF8.GetBytes(playerInfo);
try
{
    var maxCookieSize = CookieApi.GetInstantAppCookieMaxSize();
    if (cookieBytes.Length > maxCookieSize)
    {
        UnityEngine.Debug.LogErrorFormat("Cookie length {0} exceeds limit {1}.", cookieBytes.Length, maxCookieSize);
    }
    else if (CookieApi.SetInstantAppCookie(cookieBytes))
    {
        UnityEngine.Debug.Log("Successfully set cookie. Now display the app install dialog...");
        InstallLauncher.ShowInstallPrompt();
    }
    else
    {
        UnityEngine.Debug.LogError("Failed to set cookie.");
    }
}
catch (CookieApi.InstantAppCookieException ex)
{
    UnityEngine.Debug.LogErrorFormat("Failed to set cookie: {0}", ex);
}

如果使用者完成應用程式安裝,安裝的應用程式可使用以下程式碼擷取 Cookie 資料:

var cookieBytes = CookieApi.GetInstantAppCookie();
var playerInfoString = System.Text.Encoding.UTF8.GetString(cookieBytes);
if (!string.IsNullOrEmpty(playerInfoString))
{
    // Initialize game state based on the cookie, e.g. skip tutorial level completed in instant app.
}