iOS的插件

Godot provides StoreKit, GameCenter, iCloud services and other plugins. They are using same model of asynchronous calls explained below.

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

Latest updates, documentation and source code can be found at Godot iOS plugins repository

访问插件单例

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

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

var in_app_store

func _ready():
    if Engine.has_singleton("InAppStore"):
        in_app_store = Engine.get_singleton("InAppStore")

    else:
        print("iOS IAP plugin is not exported.")

异步方法

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

Error purchase(Variant p_params);

参数通常是一个字典, 包含发出请求所需的信息, 并且调用将有两个阶段. 首先, 该方法将立即返回Error值. 如果错误不是 OK, 则调用操作完成, 可能在本地引起错误(没有Internet连接,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()``弹出队列中的第一个事件并返回它.

商店套件

实现在 Godot iOS InAppStore 插件 .

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

  • Error purchase(Variant p_params);

  • Error request_product_info(Variant p_params);

  • Error restore_purchases();

和pending_event接口

int get_pending_event_count();
Variant pop_pending_event();

采购

通过Store Kit API购买产品ID.

参数

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

var result = InAppStore.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列表中请求产品信息.

参数

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

var result = InAppStore.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"
}

游戏中心

实现在 Godot iOS GameCenter 插件 .

游戏中心的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();

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

认证

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

响应事件

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

出错:

{
  "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

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

参数

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

  • 得分 浮点数

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

示例:

var result = GameCenter.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``(浮动)成就进度从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 ]
}

show_game_center

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

参数

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

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

  • leaderboard_name (字符串)(可选)要显示的排行榜的名称. 仅在 视图排行榜 (或 默认 配置为显示排行榜)时使用. 如果未指定,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",
}