text stringlengths 1 474 |
|---|
<topic_start> |
1. Enable platform services |
To enable games services, set up Game Center on iOS and |
Google Play Games Services on Android.<topic_end> |
<topic_start> |
iOS |
To enable Game Center (GameKit) on iOS:Open your Flutter project in Xcode. |
Open ios/Runner.xcworkspaceSelect the root Runner project.Go to the Signing & Capabilities tab.Click the + button to add Game Center as a capability.Close Xcode.If you haven’t already, |
register your game in App Store Connect |
and from the My App section press the + icon.Still in App Store Connect, look for the Game Center section. You |
can find it in Services as of this writing. On the Game |
Center page, you might want to set up a leaderboard and several |
achievements, depending on your game. Take note of the IDs of the |
leaderboards and achievements you create.<topic_end> |
<topic_start> |
Android |
To enable Play Games Services on Android:If you haven’t already, go to Google Play Console |
and register your game there.Still in Google Play Console, select Play Games Services → Setup |
and management → Configuration from the navigation menu and |
follow their instructions.This takes a significant amount of time and patience. |
Among other things, you’ll need to set up an |
OAuth consent screen in Google Cloud Console. |
If at any point you feel lost, consult the |
official Play Games Services guide.When done, you can start adding leaderboards and achievements in |
Play Games Services → Setup and management. Create the exact |
same set as you did on the iOS side. Make note of IDs.Go to Play Games Services → Setup and management → Publishing.Click Publish. Don’t worry, this doesn’t actually publish your |
game. It only publishes the achievements and leaderboard. Once a |
leaderboard, for example, is published this way, it cannot be |
unpublished.Go to Play Games Services → Setup and management → |
Configuration → Credentials.Find the Get resources button. |
It returns an XML file with the Play Games Services IDs.Add a file at android/app/src/main/res/values/games-ids.xml |
containing the XML you received in the previous step.<topic_end> |
<topic_start> |
2. Sign in to the game service |
Now that you have set up Game Center and Play Games Services, and |
have your achievement & leaderboard IDs ready, it’s finally Dart time.Add a dependency on the games_services package.Before you can do anything else, you have to sign the player into |
the game service. |
<code_start>try { |
await GamesServices.signIn(); |
} on PlatformException catch (e) { |
// ... deal with failures ... |
}<code_end> |
The sign in happens in the background. It takes several seconds, so |
don’t call signIn() before runApp() or the players will be forced to |
stare at a blank screen every time they start your game.The API calls to the games_services API can fail for a multitude of |
reasons. Therefore, every call should be wrapped in a try-catch block as |
in the previous example. The rest of this recipe omits exception |
handling for clarity.lightbulb Tip |
It’s a good practice to create a controller. This would be a |
ChangeNotifier, a bloc, or some other piece of logic that wraps around |
the raw functionality of the games_services plugin.<topic_end> |
<topic_start> |
3. Unlock achievements |
Register achievements in Google Play Console and App Store Connect, |
and take note of their IDs. Now you can award any of those |
achievements from your Dart code: |
<code_start>await GamesServices.unlock( |
achievement: Achievement( |
androidID: 'your android id', |
iOSID: 'your ios id', |
), |
);<code_end> |
The player’s account on Google Play Games or Apple Game Center now |
lists the achievement.To display the achievements UI from your game, call the |
games_services API: |
<code_start>await GamesServices.showAchievements();<code_end> |
This displays the platform achievements UI as an overlay on your game.To display the achievements in your own UI, use |
GamesServices.loadAchievements().<topic_end> |
<topic_start> |
4. Submit scores |
When the player finishes a play-through, your game can submit the result |
of that play session into one or more leaderboards.For example, a platformer game like Super Mario can submit both the |
final score and the time taken to complete the level, to two separate |
leaderboards.In the first step, you registered a leaderboard in Google Play |
Console and App Store Connect, and took note of its ID. Using this |
ID, you can submit new scores for the player: |
<code_start>await GamesServices.submitScore( |
score: Score( |
iOSLeaderboardID: 'some_id_from_app_store', |
androidLeaderboardID: 'sOmE_iD_fRoM_gPlAy', |
value: 100, |
), |
);<code_end> |
You don’t need to check whether the new score is the player’s |
highest. The platform game services handle that for you.To display the leaderboard as an overlay over your game, make the |
following call: |
<code_start>await GamesServices.showLeaderboards( |
iOSLeaderboardID: 'some_id_from_app_store', |
androidLeaderboardID: 'sOmE_iD_fRoM_gPlAy', |
);<code_end> |
If you want to display the leaderboard scores in your own UI, you |
can fetch them with GamesServices.loadLeaderboardScores().<topic_end> |
<topic_start> |
5. Next steps |
There’s more to the games_services plugin. With this plugin, you can:Some achievements can be incremental. For example: “You have collected |
all 10 pieces of the McGuffin.”Each game has different needs from game services.To start, you might want to create this controller |
in order to keep all achievements & leaderboards logic in one place: |
<code_start>import 'dart:async'; |
import 'package:games_services/games_services.dart'; |
import 'package:logging/logging.dart'; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.