Work in progress

The content of this page was not yet updated for Godot 4.2 and may be outdated. If you know how to improve this page or you can confirm that it's up to date, feel free to open a pull request.

為 iOS 進行編譯

Godot 提供了 StoreKit、GameCenter、iCloud 服務等外掛程式。它們使用下面解釋的相同非同步呼叫模型。

ARKit和Camera存取也作為外掛程式提供.

若想參與貢獻說明文件,則可在 此處 找到其儲存庫。

存取場景節點

要存取外掛程式功能, 首先需要通過呼叫 Engine.has_singleton() 函式來檢查外掛程式是否匯出並可用, 該函式會返回一個註冊的單例.

下列為有兩個骨頭的骨骼節點範例:

var in_app_store
var game_center

func _ready():
    if Engine.has_singleton("InAppStore"):
        in_app_store = Engine.get_singleton("InAppStore")
    else:
        print("iOS IAP plugin is not available on this platform.")

    if Engine.has_singleton("GameCenter"):
        game_center = Engine.get_singleton("GameCenter")
    else:
        print("iOS Game Center plugin is not available on this platform.")

非同步方法

請求非同步作業時, 方法如下所示:

Error purchase(Variant params);

參數通常是一個字典,包含發出請求所需的資訊,並且呼叫將有兩個階段。首先,該方法將立即返回 Error 值。如果這個 Error 不是“OK”,則呼叫操作完成,可能在本地引起錯誤(沒有網路連接、API 配置不正確等)。如果錯誤值是“OK”,則會生成回應事件並將其新增到“掛起事件”佇列中。例如:

func on_purchase_pressed():
    var result = in_app_store.purchase({ "product_id": "my_product" })
    if result == OK:
        animation.play("busy") # show the "waiting for response" animation
    else:
        show_error()

# put this on a 1 second timer or something
func check_events():
    while in_app_store.get_pending_event_count() > 0:
        var event = in_app_store.pop_pending_event()
        if event.type == "purchase":
            if event.result == "ok":
                show_success(event.product_id)
            else:
                show_error()

請記住,當一個呼叫返回 OK 時,API 將*始終*通過 pending_event 介面產生一個事件,即使它是一個錯誤,或網路超時等。您應該能夠,例如,安全地阻止等待的介面來自伺服器的回復。如果任何 API 不以這種方式運作,則應將其視為錯誤。

掛起事件介面包含兩個方法:

  • get_pending_event_count() 返回佇列中掛起事件的數量。

  • Variant pop_pending_event() 彈出佇列中的第一個事件並返回它。

Store Kit

Implemented in Godot iOS InAppStore plugin.

Store Kit API 可通過 InAppStore 單例存取。它是自動初始化的。

提供了以下方法,文件如下:

   Error purchase(Variant params)
   Error request_product_info(Variant params)
   Error restore_purchases()
   void set_auto_finish_transaction(bool enable)
   void finish_transaction(String product_id)

and the pending events interface:

::

   int get_pending_event_count()
   Variant pop_pending_event()

purchase

通過 Store Kit API 購買一個產品 ID。你需要在收到成功回應後呼叫 finish_transaction(product_id),或者在呼叫 purchase() 之前呼叫 set_auto_finish_transaction(true)。這兩個方法確保事務完成。

參數

參數是一個字典,包含一個 product_id 欄位,是你的商品的字串 ID。例子:

var result = in_app_store.purchase({ "product_id": "my_product" })

回應事件

回應事件將是包含以下欄位的字典:

出錯時:

{
  "type": "purchase",
  "result": "error",
  "product_id": "the product ID requested",
}

成功時:

{
  "type": "purchase",
  "result": "ok",
  "product_id": "the product ID requested",
}

request_product_info

在產品ID列表中請求產品資訊.

參數

參數為字典,只有一個欄位 product_ids,是產品 ID 字串的陣列。例如:

var result = in_app_store.request_product_info({ "product_ids": ["my_product1", "my_product2"] })

回應事件

回應事件將是包含以下欄位的字典:

{
  "type": "product_info",
  "result": "ok",
  "invalid_ids": [ list of requested IDs that were invalid ],
  "ids": [ list of IDs that were valid ],
  "titles": [ list of valid product titles (corresponds with list of valid IDs) ],
  "descriptions": [ list of valid product descriptions ],
  "prices": [ list of valid product prices ],
  "localized_prices": [ list of valid product localized prices ],
}

restore_purchases

恢復使用者帳戶之前完成的購買。會為每一個之前購買過的產品 ID 建立響應事件。

回應事件

回應事件將是包含以下欄位的字典:

{
  "type": "restore",
  "result": "ok",
  "product_id": "product ID of restored purchase",
}

set_auto_finish_transaction

設為 true 時,一旦購買成功,就會自動結束購買。請在呼叫 purchase() 前呼叫本方法。

參數

參數為布林值,表示購買是否應該自動結束。例如:

in_app_store.set_auto_finish_transaction(true)

finish_transaction

如果你不希望自動結束交易事務,請在接收到成功購買回應後呼叫本方法。

參數

參數 product_id 是一個字串。product_id 表示需要結束購買的產品。例如:

in_app_store.finish_transaction("my_product1")

遊戲中心

Implemented in Godot iOS GameCenter plugin.

Game Center API 可以通過“GameCenter”單例使用。它有以下方法:

Error authenticate()
bool is_authenticated()
Error post_score(Variant score)
Error award_achievement(Variant params)
void reset_achievements()
void request_achievements()
void request_achievement_descriptions()
Error show_game_center(Variant params)
Error request_identity_verification_signature()

以及未決事件介面:

int get_pending_event_count()
Variant pop_pending_event()

x.attribute

在遊戲中心對使用者進行身份驗證.

回應事件

回應事件將是包含以下欄位的字典:

出錯時:

{
  "type": "authentication",
  "result": "error",
  "error_code": the value from NSError::code,
  "error_description": the value from NSError::localizedDescription,
}

成功時:

{
  "type": "authentication",
  "result": "ok",
  "player_id": the value from GKLocalPlayer::playerID,
}

post_score

將分數發行到遊戲中心排行榜.

參數

參數為一個字典,有兩個欄位:

  • score 浮點數

  • category 表示類別名稱的字串

範例:

var result = game_center.post_score({ "score": 100, "category": "my_leaderboard", })

回應事件

回應事件將是包含以下欄位的字典:

出錯時:

{
  "type": "post_score",
  "result": "error",
  "error_code": the value from NSError::code,
  "error_description": the value from NSError::localizedDescription,
}

成功時:

{
  "type": "post_score",
  "result": "ok",
}

award_achievement

修改遊戲中心成就的進度.

參數

將Dictionary作為參數, 包含3個欄位:

  • ``name``(字串)成就名稱

  • progress``(float)成就進度從 0.0 100.0(傳遞給 ``GKAchievement :: percentComplete

  • ``show_completion_banner``(bool)遊戲中心是否應該在螢幕頂部顯示成就橫幅

範例:

var result = award_achievement({ "name": "hard_mode_completed", "progress": 6.1 })

回應事件

回應事件將是包含以下欄位的字典:

出錯時:

{
  "type": "award_achievement",
  "result": "error",
  "error_code": the error code taken from NSError::code,
}

成功時:

{
  "type": "award_achievement",
  "result": "ok",
}

reset_achievements

清除所有 Game Center 成就。該函式不帶參數。

回應事件

回應事件將是包含以下欄位的字典:

出錯時:

{
  "type": "reset_achievements",
  "result": "error",
  "error_code": the value from NSError::code,
}

成功時:

{
  "type": "reset_achievements",
  "result": "ok",
}

request_achievements

請求遊戲角色取得進步的所有遊戲中心成就. 該函式不帶參數.

回應事件

回應事件將是包含以下欄位的字典:

出錯時:

{
  "type": "achievements",
  "result": "error",
  "error_code": the value from NSError::code,
}

成功時:

{
  "type": "achievements",
  "result": "ok",
  "names": [ list of the name of each achievement ],
  "progress": [ list of the progress made on each achievement ],
}

request_achievement_descriptions

無論進度如何, 都要求描述所有現有的Game Center成就. 該函式不帶參數.

回應事件

回應事件將是包含以下欄位的字典:

出錯時:

{
  "type": "achievement_descriptions",
  "result": "error",
  "error_code": the value from NSError::code,
}

成功時:

{
  "type": "achievement_descriptions",
  "result": "ok",
  "names": [ list of the name of each achievement ],
  "titles": [ list of the title of each achievement ],
  "unachieved_descriptions": [ list of the description of each achievement when it is unachieved ],
  "achieved_descriptions": [ list of the description of each achievement when it is achieved ],
  "maximum_points": [ list of the points earned by completing each achievement ],
  "hidden": [ list of booleans indicating whether each achievement is initially visible ],
  "replayable": [ list of booleans indicating whether each achievement can be earned more than once ],
}

center()

顯示內建的遊戲中心疊加層, 顯示排行榜, 成就和挑戰.

參數

將Dictionary作為參數, 包含兩個欄位:

  • ``view``(字串)(可選)要呈現的視圖的名稱。接受“default”(預設)“leaderboards”(排行榜)“achievements”(成就)“challenges”(挑戰)。預設為“default”。

  • ``leaderboard_name``(字串)(可選)要顯示的排行榜的名稱。僅在“view”為“leaderboards”(或“default”被配置為顯示排行榜)時使用。如果未指定,Game Center 將顯示聚合排行榜。

範例:

var result = show_game_center({ "view": "leaderboards", "leaderboard_name": "best_time_leaderboard" })
var result = show_game_center({ "view": "achievements" })

回應事件

回應事件將是包含以下欄位的字典:

關閉時:

{
  "type": "show_game_center",
  "result": "ok",
}