實驗性 YouTube 遊戲角落 Godot 包裝函式

有了這個包裝函式,您就可以使用提供的 .gd,在 Godot 中存取 YouTube Playables SDK。這個包裝函式包含 .js SDK 連接器和 .gd 檔案,可協助加快開發速度。我們也提供範例專案,說明如何在專案中使用這些指令碼。

您可以從 Playables 範例存放區下載 Godot zip 套件。

用量

  1. 在您要放置這些指令碼的專案資料夾中,擷取 GoogleYTGameWrapper-godot.zip。這個 ZIP 檔案包含兩個檔案:
    • YTGameWrapper.gd:用於連結至 YT Playables Javascript SDK,讓您在遊戲中呼叫 SDK。
    • YTGameSDK.js:用於連線至 YT Playables SDK。在本例中,我們會將這個檔案放在專案的根目錄中。您可以變更這個值,但必須更新 Head Include 位置。
    • 您也可以從我們的 Playables sample repo 下載完整專案範例版本。針對這個版本,請使用名為 GoogleYTGameWrapper-godot-with-sample.zip 的 ZIP 檔案。
  2. 為已解壓縮的 YTGameWrapper.gd 指令碼新增 Autoload Global。您可以依序前往 Project -> Project Settings... -> Globals 進行設定。在 Path 輸入框中選取 YTGameWrapper.gd 檔案,並在 Node Name 輸入框中新增 YTGameWrapper。最後,選取 + Add
    • 這樣一來,指令碼就會在程式碼中全域可用,讓您視需要呼叫 YTGameWrapper
  3. 確認 Godot 專案的平台設定為匯出 Web。這項設定位於「Export」分頁,請依序選取 ProjectExport...
  4. 在「匯出」面板中新增預設設定,方法是依序選取 Add...Web。建立後,請將這段程式碼新增至 Head Include 設定方塊。
<script src="https://www.youtube.com/game_api/v1">// Load YT Game API code</script>
<script src="YTGameSDK.js"></script>
  1. 選取 Export Project... 並選取建構位置,即可在 Export 面板中匯出 Web 專案。
  2. 建構專案後,請前往建構資料夾,並將 YTGameSDK.js 檔案複製到這個資料夾中,並確保在提交建構作業時納入該檔案。
  3. 請確認您遵循專案的技術規定,並務必使用 Playables 測試套件

範例

本節提供一些 .gd 包裝函式的使用範例,但並非可用 API 的完整清單。如需可用 API 的完整清單,請參閱 YouTube Playables SDK。這也假設您的專案已按照「用法」一節所述設定。

sendScore

以下範例說明如何在 .gd 中實作 sendScore(int points)

var current_game_score: int = 0

...

func add_score(scoreAdded: int):
    current_game_score += scoreAdded
    YTGameWrapper.send_score(current_game_score)

onPause

以下範例說明遊戲如何監聽來自 YT Playables 的 Pause 事件,以便在需要時暫停引擎:

func _ready() -> void:
    # hook up on game paused
    YTGameWrapper.game_paused.connect(self._on_game_paused)
    # hook up on game resumed and game save data received events
    YTGameWrapper.game_resumed.connect(self._on_game_resumed)

func _on_game_paused():
    # Pause your game logic, audio, etc.
    get_tree().paused = true
    
func _on_game_resumed():
    # Resume your game logic, audio, etc.
    get_tree().paused = false

saveData

以下範例說明如何使用 saveData,並將其傳送至 YT Playables SDK:

var current_game_score: int = 0
var time_elapsed: float = 0.0

...

func save_game_data():
    YTGameWrapper.save_data('{"BestScore": "' + str(current_game_score) + '","BestTime": "' +str(time_elapsed)+ '"}')

loadData

以下範例說明如何使用 loadData,並將其傳送至 YT Playables SDK:

func _ready() -> void:
    # hook up game load data received event
    YTGameWrapper.load_data_received.connect(self._on_load_data)
    # Request to load data when the game starts
    YTGameWrapper.load_data()

func _on_load_data(save_data_string: String):
    if not save_data_string.is_empty():
        # Add logic to parse the save_data_string and restore game state
        print("Loaded data: ", save_data_string)

requestInterstitialAd

以下範例說明如何使用 requestInterstitialAd,指出目前是顯示插頁式廣告 (如有) 的好時機。為獲得最佳結果,請在遊戲過程中休息時發出這項呼叫,例如在關卡結束時。

func set_game_over(didWin: bool):
    YTGameWrapper.request_interstitial_ad()

    # logic for game over

如何在 Godot 專案中分割素材資源 (延後載入)

開發人員在使用 Godot 時,最常遇到的問題之一,就是如何在個別檔案大小規定和總套件大小規定的限制內。

素材資源的延遲載入功能是您可為專案進行的最佳化最佳做法,因為您可以視需要載入素材資源、層級和資料。如果您能正確執行這項操作,我們的認證團隊可能會豁免整體檔案大小限制,因為完整遊戲不會在前端載入,而是在使用者瀏覽產品時載入。

為協助您正確載入,Godot 提供匯出包、修補程式和模組 (英文連結)。建議您產生可視需要載入的 .pck 檔案。

請參閱完整的 YT Playables API 參考資料