#bevy-plugin #ios #bevy #mobile #swift #gamedev

bevy_ios_gamecenter

Bevy Plugin and Swift Package to provide access to iOS native GameKit (Gamecenter) from inside Bevy Apps

11 releases

0.3.0 Dec 2, 2024
0.2.0 Jul 11, 2024
0.1.8 May 11, 2024

#112 in Game dev

Download history 15/week @ 2024-08-26 72/week @ 2024-09-02 158/week @ 2024-09-09 47/week @ 2024-09-16 32/week @ 2024-09-23 49/week @ 2024-09-30 65/week @ 2024-10-07 47/week @ 2024-10-14 15/week @ 2024-10-21 39/week @ 2024-10-28 45/week @ 2024-11-04 50/week @ 2024-11-11 46/week @ 2024-11-18 30/week @ 2024-11-25 192/week @ 2024-12-02

319 downloads per month

MIT license

53KB
1K SLoC

bevy_ios_gamecenter

crates.io docs.rs discord

Bevy Plugin and Swift Package to provide access to iOS native GameKit (Gamecenter) from inside Bevy Apps It uses Swift-Bridge to auto-generate the glue code and transport data types.

demo

Demo from our game using this crate: zoolitaire.com

Features

  • authentication
  • save games (based on iCloud)
  • achievements
  • leaderboards

TODOs

  • challenges, matchmaking

Instructions

  1. Add to XCode: Add SPM (Swift Package Manager) dependency
  2. Add Rust dependency
  3. Setup Plugin

1. Add to XCode

  • Add GameKit framework: gamekit

  • Go to File -> Add Package Dependencies and paste https://github.com/rustunit/bevy_ios_gamecenter.git into the search bar on the top right: xcode

Note: The rust crate used must be exactly the same version as the Swift Package. I suggest using a specific version (like 0.2.0 in the screenshot) to make sure to always use binary matching versions!

2. Add Rust dependency

cargo add bevy_ios_gamecenter

or

# always pin to the same exact version you also of the Swift package
bevy_ios_gamecenter = { version = "=0.2.0" }

3. Setup Plugin

Initialize Bevy Plugin:

// request auth right on startup
app.add_plugins(IosGamecenterPlugin::new(true));
fn bevy_system() {
    bevy_ios_gamecenter::achievements_reset();
    
    // update achievement progress, 100 % will complete it
    bevy_ios_gamecenter::achievement_progress("id".into(),100.);

    bevy_ios_gamecenter::leaderboards_score(
        "raking id".into(),
        // score
        1,
        // context
        2,
    );

    // open gamecenter view (leaderboard)
    bevy_ios_gamecenter::trigger_view(view_states::LEADERBOARDS);

    // save arbitrary binary buffer as a savegame
    bevy_ios_gamecenter::save_game("test".into(), vec![1, 2, 3].as_slice());

    // request list of `IosGCSaveGame`
    bevy_ios_gamecenter::fetch_save_games();

    // based on result of above `fetch_save_games` request
    let save_game = IosGCSaveGame {..} 
    bevy_ios_gamecenter::load_game(save_game);
}

Alternatively you can use a convenient Observer based request/response approach:

fn bevy_system(mut gamecenter: BevyIosGamecenter) {
    gamecenter.fetch_save_games().on_response(|trigger:Trigger<IosGCSaveGamesResponse>|{
        // handle list of save games
    });
}

Process Response Events from iOS back to us in Rust:

fn process_gamecenter_events(
    mut events: EventReader<IosGamecenterEvents>,
) {
    for e in events.read() {
        match e {
            IosGamecenterEvents::SaveGames(response) => todo!(),
            IosGamecenterEvents::Player(player) => todo!(),
            IosGamecenterEvents::Authentication(response) => todo!(),
            IosGamecenterEvents::SavedGame(response) => todo!(),
            IosGamecenterEvents::LoadGame(response) => todo!(),
            IosGamecenterEvents::AchievementProgress(response) => todo!(),
            IosGamecenterEvents::AchievementsReset(response) => todo!(),
            IosGamecenterEvents::LeaderboardScoreSubmitted(response) => todo!(),
        }
    }
}

Our Other Crates

Bevy version support

bevy bevy_ios_gamecenter
0.15 0.3,main
0.14 0.2
0.13 0.1

License

All code in this repository is dual-licensed under either:

at your option. This means you can select the license you prefer.

Your contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~24–36MB
~556K SLoC