Get started with the Google Play Games plugin for Unity | Android game development | Android Developers
Mục Chính
Before you begin
- In Google Play Console, set up Play Console for your game .
- In Google Play Console, create an achievement .
Plugin Installation
-
Get the plugin using one of these options:
Bạn đang đọc: Get started with the Google Play Games plugin for Unity | Android game development | Android Developers
- Clone the GitHub repo into your file system .
- Download the GitHub repo as a ZIP file, and then unpack it .
-
Find the
unitypackage
file in thecurrent-build
directory. This file
is the plugin. For example, it should look similar to the following:current-build/GooglePlayGamesPluginForUnity-X.YY.ZZ.unitypackage
- Open your game project in Unity .
-
Import the
unitypackage
file into your project’s assets by clicking the
Assets > Import Package > Custom Package menu item. -
Ensure that your current build platform is set to Android.
-
In the main menu, click File > Build Settings….
-
Select Android and click Switch Platform.
-
There should be a new menu item under Window > Google Play Games. If
there isn’t, refresh the assets by clicking Assets > Refresh and
then try setting the build platform again.
-
Get Android resources from Play Console
Each achievement, leaderboard, and sự kiện you create in Play Console includes an Android resouce that you use when you set up your Unity project .
- Get the Android resources for your game .
- In Play Console, open the Play Games Services project for your game .
-
Click the Achievements tab, and then click Get resources.
-
In the Export resources window, click the Android tab.
- Select and copy the XML content .
Set up your Unity project
- Add the Android resources to your Unity project .
- In Unity, click * * Window > Google Play Games > Setup … > Android Setup * * .
- Complete the these items in the setup windows :
-
Directory to save constants: The folder for the constants file.
-
Constants class name: The name of the C# class to create, including the namespace.
-
Resources Definition: Paste the Android resources data from Play Console here.
-
Web client ID: The client ID of the linked web app. This is only
needed if you have a web-based backend
for your game and need a server auth code to exchange for an access token by
the backend server, or if you need an ID token for the player to make other
non-game API calls. -
Click Setup. This configures your game with the client ID and
generates a C# class that contains constants for each of your Android
resources.
Android Setup
Set up the path to your Android SDK installation in Unity. This is located in
the preferences menu, under the External Tools section.
To configure your Unity game to run with Google Play Games on Android, first open the Android SDK manager and verify that you have downloaded the following packages. Depending on if you are using the SDK manager from Android Studio, or using the standalone SDK manager, the name of the components may be different. – Google Play Services – Android Support Library – Local Maven repository for Support Libraries ( Also known as Android Support Repository ) – Google Repository – Android 6.0 ( API 23 ) ( this does not affect the min SDK version ) .
Next, configure your game’s package name. To do this, click File > Build
Settings, select the Android platform and click Player Settings to
show Unity’s Player Settings window. In that window, look for the Bundle
Identifier setting under Other Settings. Enter your package name there
(for example com.example.my.awesome.game).
In order to sign in to Play Game Services, you need to sign your APK file, make sure that you are signing it with the correct certificate, that is, the one that corresponds to the SHA1 certificate fingerprint you entered in the Developer Console during the setup .
Next, click the Window |Google Play Games|Setup – Android setup menu item.
This will display the Android setup screen.
Enter the Constants class name. This is the name of the fully qualified class
that will be updated (or created) which contains the IDs of the game resources.
The format of the name is
. For example,
AwesomeGame.GPGSIds
Paste the resource definition data. This is the XML data from the Google Play
Developer Console which contains the resource IDs as well as the Application ID
for Android.
This data is found in the Google Play Developer Console by clicking ” Get resources ” on any of the resource pages ( e. g. Achievements or Leaderboards ), then clicking Android .
After pasting the data into the text area, click the Setup button.
Note: If you are using a web application or backend server with your game,
you can link the web application to the game to enable getting the player’s
id token and/or email address. To do this, link a web application to the
game in the Google Play Developer Console, and enter the client id for
the web application into the setup dialog.
Additional instructions on building for Android on Windows
If you are using Windows, you must make sure that your Java SDK installation can be accessed by Unity. To do this :
- Set the JAVA_HOME environment variable to your Java SDK installation path
(for example,C:\Program Files\Java\jdk1.7.0_45
). - Add the Java SDK’s
bin
folder to yourPATH
environment variable
(for example,C:\Program Files\Java\jdk1.7.0_45\bin
) - Reboot.
How to edit environment variables: In Windows 2000/XP/Vista/7, right-click
My Computer, then Properties, then go to Advanced System Properties
(or System Properties and then click the Advanced tab), then click
Environment Variables. On Windows 8, press Windows Key + W and search
for environment variables For more information, consult the documentation
for your version of Windows.
Run the project
If you are working with the Smoketest sample, you should be able to build
and run the project at this point. You will see the automatic sign-in attempt,
when Smoketest starts.
To build and run on Android, click
File > Build Settings, select the Android platform, then
Switch to Platform, then Build and Run.
The Google Play Games plugin implements Unity’s social interface, for compatibility with games that already use that interface when integrating with other platforms. However, some features are unique to Play Games and are offered as extensions to the standard social interface provided by Unity .
The standard API calls can be accessed through the Social.Active object,
which is a reference to an ISocialPlatform interface. The non-standard
Google Play Games extensions can be accessed by casting the Social.Active
object to the PlayGamesPlatform class, where the additional methods are
available.
When you call PlayGamesPlatform.Activate
, Google Play Games becomes your
default social platform implementation, which means that static calls to methods
in Social
and Social.Active
will be carried out by the Google Play Games
plugin. This is the desired behavior for most games using the plugin.
However, if for some reason you wish to keep the default implementation accessible ( for example, to use it to submit achievements and leaderboards to a different social platform ), you can use the Google Play Games plugin without overriding the default one. To do this :
- Do not call
PlayGamesPlatform.Activate
- If
Xyz
is the name of a method you wish to call on theSocial
class, do
not callSocial.Xyz
. Instead, callPlayGamesPlatform.Instance.Xyz
- Do not use
Social.Active
when interacting with Google Play Games. Instead,
usePlayGamesPlatform.Instance
.
That way, you can even submit scores and achievements simultaneously to two or more social platforms :
// Submit achievement to original default social platform
Social.ReportProgress("MyAchievementIdHere", 100.0f, callback);
// Submit achievement to Google Play
PlayGamesPlatform.Instance.ReportProgress("MyGooglePlayAchievementIdHere", 100.0f, callback);
Verify the sign-in service
A connection to Play Games Services is automatically attempted using the Sign-in service when your game is opened. If the connection succeeds, your game displays a sign-in prompt and is ready to use the Google Play Games plugin for Unity .
If a user has never used Google Play Games on their device, they are automatically taken through one-time setup màn hình hiển thị to create a Play Games account .
In the Start
method of your script, listen to the result of the automatic
sign-in attempt, fetch the authentication status, and disable Play Games
Services features if the user is not signed in.
using GooglePlayGames;
public void Start() {
PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication);
}
internal void ProcessAuthentication(SignInStatus status) {
if (status == SignInStatus.Success) {
// Continue with Play Games Services
} else {
// Disable your integration with Play Games Services or show a login button
// to ask users to sign-in. Clicking it should call
// PlayGamesPlatform.Instance.ManuallyAuthenticate(ProcessAuthentication).
}
}
}
The result code is an enum that you can use to identify the reason for a sign-in failure .
If you prefer using Unity’s Social platform, you can alternatively use the following code :
using GooglePlayGames;
public void Start() {
PlayGamesPlatform.Activate();
Social.localUser.Authenticate(ProcessAuthentication);
}
You cannot make any Google Play Games API calls until you get a successful
return value from Authenticate
. As a result, we recommend that games display a
standby screen until the callback is called to ensure users can’t start playing
the game until authentication completes.
Note:For more information about how Player IDs work, visit the topic here on next generation Player IDs
Retrieve server authentication codes
In order to access Google APIs on a backend web server on behalf of the current player, you need to get an authentication code from the client application and pass this to your web server application. The code can then be exchanged for an access token to make calls to the various APIs. For information about the workflow, see Google Sign-In for Websites .
To get the server side access code :
- Configure the web client id of the web application linked to your game in the Play Game Console .
-
Call
PlayGamesPlatform.Instance.RequestServerSideAccess
once the player is
authenticated to get the server side access code. - Pass this code to your server application .
PlayGamesPlatform.Instance.RequestServerSideAccess(
/* forceRefreshToken= */ false,
code -> {
// send code to server
});
Set up and add features
- Enable Play Games Services features .
- Add features to your game using the Play Games Services APIs :
- Achievements
- Leaderboards
-
Friends
- Save games
- Player stats
- Events
Source: https://thomaygiat.com
Category : Ứng Dụng
Hướng Dẫn Chi Tiết Xử Lý Lỗi E-66 Máy Giặt Electrolux
Mục ChínhHướng Dẫn Chi Tiết Xử Lý Lỗi E-66 Máy Giặt ElectroluxLỗi E-66 máy giặt Electrolux là gì?4 Nguyên nhân gây lỗi E-66 máy giặt…
Tủ Lạnh Sharp Lỗi H-36 Cách Xử Lý Đơn Giản
Mục ChínhTủ Lạnh Sharp Lỗi H-36 Cách Xử Lý Đơn GiảnGiới thiệu về lỗi H-36 trên tủ lạnh SharpNguyên nhân gây lỗi H-36 trên tủ…
Khắc phục lỗi E-62 máy giặt Electrolux không cần thợ
Mục ChínhKhắc phục lỗi E-62 máy giặt Electrolux không cần thợMã lỗi E-62 Máy giặt Electrolux là gì?Các bộ phận liên quan đến mã lỗi…
Tủ Lạnh Sharp Lỗi H-35 Nguy Cơ Không Thể Sửa Chữa!
Mục ChínhQuy Trình Tự Sửa Lỗi H-35 Tủ Lạnh Sharp Từng Bước An ToànMã lỗi H-35 trên tủ lạnh Sharp là gì?Nguyên nhân gây lỗi…
Tủ lạnh Sharp nháy lỗi H-34 Cuộc chạy đua với thời gian!
Mục ChínhTủ lạnh Sharp nháy lỗi H-34 Cuộc chạy đua với thời gian!Tìm Hiểu Lỗi H-34 Trên Tủ Lạnh Sharp Là Gì?Nguyên nhân phổ biến…
Hậu quả nghiêm trọng từ lỗi H-30 trên tủ lạnh Sharp
Mục ChínhHậu quả nghiêm trọng từ lỗi H-30 trên tủ lạnh SharpLỗi H-30, H-31, H-32, H-33 tủ Lạnh Sharp là gì?Tầm quan trọng của các…