iOS的插件

目前,Godot提供了StoreKit、GameCenter、iCloud服务插件.它们使用相同的异步调用模型,解释如下.

ARKit和Camera访问也作为插件提供.

访问插件单例

要访问插件功能,首先需要通过调用 Engine.has_singleton() 函数来检查插件是否导出并可用,该函数会返回一个注册的单例.

下面是一个如何在GDScript中做到这一点的例子:

  1. var in_app_store
  2. func _ready():
  3. if Engine.has_singleton("InAppStore"):
  4. in_app_store = Engine.get_singleton("InAppStore")
  5. else:
  6. print("iOS IAP plugin is not exported.")

异步方法

请求异步操作时,方法如下所示:

  1. Error purchase(Variant p_params);

参数通常是一个字典,包含发出请求所需的信息,并且调用将有两个阶段. 首先,该方法将立即返回Error值. 如果错误不是``OK``,则调用操作完成,可能在本地引起错误(没有Internet连接,API配置不正确等). 如果错误值为``OK``,则会生成响应事件并将其添加到``挂起事件``队列中. 例:

  1. func on_purchase_pressed():
  2. var result = in_app_store.purchase({ "product_id": "my_product" })
  3. if result == OK:
  4. animation.play("busy") # show the "waiting for response" animation
  5. else:
  6. show_error()
  7. # put this on a 1 second timer or something
  8. func check_events():
  9. while in_app_store.get_pending_event_count() > 0:
  10. var event = in_app_store.pop_pending_event()
  11. if event.type == "purchase":
  12. if event.result == "ok":
  13. show_success(event.product_id)
  14. else:
  15. show_error()

请记住,当一个调用返回OK时,API将 始终 通过pending_event接口产生一个事件,即使它是一个错误,或网络超时等.您应该能够,例如,安全地阻止等待的接口 来自服务器的回复. 如果任何API不以这种方式运行,则应将其视为错误.

挂起事件接口包含两个方法:

  • ``get_pending_event_count()``返回队列中挂起事件的数量.

  • ``Variant pop_pending_event()``弹出队列中的第一个事件并返回它.

商店套件

实现在 `Godot iOS InAppStore插件<https://github.com/godotengine/godot-ios-plugins/blob/master/plugins/inappstore/in_app_store.mm>`_ .

Store Kit API可通过 InAppStore 单例访问.它是自动初始化的.

  • Error purchase(Variant p_params);

  • Error request_product_info(Variant p_params);

  • Error restore_purchases();

和pending_event接口

  1. int get_pending_event_count();
  2. Variant pop_pending_event();

采购

通过Store Kit API购买产品ID.

参数

将Dictionary作为参数,使用一个字段 product_id ,一个包含产品ID的字符串. 例:

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

响应事件

响应事件将是包含以下字段的字典:

出错:

  1. {
  2. "type": "purchase",
  3. "result": "error",
  4. "product_id": "the product id requested"
  5. }

成功时:

  1. {
  2. "type": "purchase",
  3. "result": "ok",
  4. "product_id": "the product id requested"
  5. }

request_product_info

在产品ID列表中请求产品信息.

参数

将Dictionary作为参数,使用一个字段 product_ids ,一个带有产品ID列表的字符串数组. 例:

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

响应事件

响应事件将是包含以下字段的字典:

  1. {
  2. "type": "product_info",
  3. "result": "ok",
  4. "invalid_ids": [ list of requested ids that were invalid ],
  5. "ids": [ list of ids that were valid ],
  6. "titles": [ list of valid product titles (corresponds with list of valid ids) ],
  7. "descriptions": [ list of valid product descriptions ] ,
  8. "prices": [ list of valid product prices ],
  9. "localized_prices": [ list of valid product localized prices ],
  10. }

restore_purchases

恢复以前在用户帐户上进行的购买. 这将为每个先前购买的产品ID创建响应事件.

响应事件

响应事件将是包含以下字段的字典:

  1. {
  2. "type": "restore",
  3. "result": "ok",
  4. "product id": "product id of restored purchase"
  5. }

游戏中心

实现在 `Godot iOS GameCenter插件<https://github.com/godotengine/godot-ios-plugins/blob/master/plugins/gamecenter/game_center.mm>`_ .

游戏中心的API是通过 GameCenter 单例来实现的.它有以下方法:

  • Error authenticate();

  • bool is_authenticated();

  • Error post_score(Variant p_score);

  • Error award_achievement(Variant p_params);

  • void reset_achievements();

  • void request_achievements();

  • void request_achievement_descriptions();

  • Error show_game_center(Variant p_params);

  • Error request_identity_verification_signature();

加上标准的待处理事件接口.

认证

在游戏中心对用户进行身份验证.

响应事件

响应事件将是包含以下字段的字典:

出错:

  1. {
  2. "type": "authentication",
  3. "result": "error",
  4. "error_code": the value from NSError::code,
  5. "error_description": the value from NSError::localizedDescription,
  6. }

成功时:

  1. {
  2. "type": "authentication",
  3. "result": "ok",
  4. "player_id": the value from GKLocalPlayer::playerID,
  5. }

post_score

将分数发布到游戏中心排行榜.

参数

将Dictionary作为参数,包含两个字段:

  • ``得分``浮点数

  • ``category``一个带有类别名称的字符串

示例:

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

响应事件

响应事件将是包含以下字段的字典:

出错:

  1. {
  2. "type": "post_score",
  3. "result": "error",
  4. "error_code": the value from NSError::code,
  5. "error_description": the value from NSError::localizedDescription,
  6. }

成功时:

  1. {
  2. "type": "post_score",
  3. "result": "ok",
  4. }

award_achievement

修改游戏中心成就的进度.

参数

将Dictionary作为参数,包含3个字段:

  • ``name``(字符串)成就名称

  • progress``(浮动)成就进度从0.0到100.0(传递给``GKAchievement :: percentComplete)

  • ``show_completion_banner``(bool)游戏中心是否应该在屏幕顶部显示成就横幅

示例:

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

响应事件

响应事件将是包含以下字段的字典:

出错:

  1. {
  2. "type": "award_achievement",
  3. "result": "error",
  4. "error_code": the error code taken from NSError::code,
  5. }

成功时:

  1. {
  2. "type": "award_achievement",
  3. "result": "ok",
  4. }

reset_achievements

清除所有Game Center成就. 该函数不带参数.

响应事件

响应事件将是包含以下字段的字典:

出错:

  1. {
  2. "type": "reset_achievements",
  3. "result": "error",
  4. "error_code": the value from NSError::code
  5. }

成功时:

  1. {
  2. "type": "reset_achievements",
  3. "result": "ok",
  4. }

request_achievements

请求游戏角色取得进步的所有游戏中心成就. 该函数不带参数.

响应事件

响应事件将是包含以下字段的字典:

出错:

  1. {
  2. "type": "achievements",
  3. "result": "error",
  4. "error_code": the value from NSError::code
  5. }

成功时:

  1. {
  2. "type": "achievements",
  3. "result": "ok",
  4. "names": [ list of the name of each achievement ],
  5. "progress": [ list of the progress made on each achievement ]
  6. }

request_achievement_descriptions

无论进度如何,都要求描述所有现有的Game Center成就. 该函数不带参数.

响应事件

响应事件将是包含以下字段的字典:

出错:

  1. {
  2. "type": "achievement_descriptions",
  3. "result": "error",
  4. "error_code": the value from NSError::code
  5. }

成功时:

  1. {
  2. "type": "achievement_descriptions",
  3. "result": "ok",
  4. "names": [ list of the name of each achievement ],
  5. "titles": [ list of the title of each achievement ]
  6. "unachieved_descriptions": [ list of the description of each achievement when it is unachieved ]
  7. "achieved_descriptions": [ list of the description of each achievement when it is achieved ]
  8. "maximum_points": [ list of the points earned by completing each achievement ]
  9. "hidden": [ list of booleans indicating whether each achievement is initially visible ]
  10. "replayable": [ list of booleans indicating whether each achievement can be earned more than once ]
  11. }

show_game_center

显示内置的游戏中心叠加层,显示排行榜,成就和挑战.

参数

将Dictionary作为参数,包含两个字段:

  • view``(string)(可选)要呈现的视图的名称. 接受``默认,``排行榜``,``成就``或``挑战``. 默认为``默认``.

  • ``leaderboard_name``(字符串)(可选)要显示的排行榜的名称. 仅在``视图``为``排行榜``(或``默认``配置为显示排行榜)时使用. 如果未指定,Game Center将显示聚合排行榜.

示例:

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

响应事件

响应事件将是包含以下字段的字典:

关闭时:

  1. {
  2. "type": "show_game_center",
  3. "result": "ok",
  4. }