Google Play Games plugin for Unity - GitHub
文章推薦指數: 80 %
The Google Play Games plugin for Unity® is an open-source project whose goal is to provide a plugin that allows game developers to integrate with the Google ...
Skiptocontent
{{message}}
playgameservices
/
play-games-plugin-for-unity
Public
Notifications
Star
3k
Fork
894
GooglePlayGamespluginforUnity
Viewlicense
3k
stars
894
forks
Star
Notifications
Code
Issues
612
Pullrequests
10
Actions
Projects
0
Security
Insights
More
Code
Issues
Pullrequests
Actions
Projects
Security
Insights
master
Branches
Tags
Couldnotloadbranches
Nothingtoshow
Loading
{{refName}}
default
Couldnotloadtags
Nothingtoshow
{{refName}}
default
Loading
16
branches
152
tags
Code
Loading
Latestcommit
aerialninja
andCopybara-Service
Updatedependencyversionnumber
…
02f06dd
Oct14,2021
Updatedependencyversionnumber
PiperOrigin-RevId:403070843
Change-Id:I8e36516c8f1219818444e195cc551269558180e9
02f06dd
Gitstats
92
commits
Files
Permalink
Failedtoloadlatestcommitinformation.
Type
Name
Latestcommitmessage
Committime
current-build
v10.12release.
Nov24,2020
git-hooks
\nInternalsync\n
Dec10,2019
old-builds
\nInternalsync\n
Dec10,2019
samples
v10.12release.
Nov18,2020
scripts
RemoveMultiplayerAPI.
Aug14,2020
source
Updatedependencyversionnumber
Oct14,2021
CHANGELOG.txt
v10.12release.
Nov24,2020
CONTRIBUTING.md
Internalsync
Jan6,2020
Dockerfile
\nInternalsync\n
Dec10,2019
LICENSE
\nInternalsync\n
Dec10,2019
NEARBY.md
RemoveMultiplayerfromsamplegames.
Aug5,2020
README.md
Mergepullrequest#2902fromplaygameservices:Fix-a-typo
Oct13,2020
Settings.StyleCop
\nInternalsync\n
Dec10,2019
UPGRADING.txt
\nInternalsync\n
Dec10,2019
Viewcode
GooglePlayGamespluginforUnity
Overview
Upgrading
ConfigureYourGame
Copythegameresourcesfromtheconsole
Pastethegameresourcesintothepluginsetupdialog
SetupChecklist
AddAchievementsandLeaderboards
AddEvents
LoadYourGameProject
PluginInstallation
AndroidSetup
AdditionalinstructionsonbuildingforAndroidonWindows
RuntheProject
ISocialPlatformCompliance
NearbyConnectionsConfiguration
Configuration&InitializationPlayGameServices
AddingadditionalScopes
Signin
Incrementalauthorization
Friends
Viewfriends
LoadingfriendswiththeISocialframework
LoadingfriendswithPlayGamesPlatform
Determiningfriendslistvisibility
Viewaplayerprofile
PlayerStatistics
Settingpopupgravity
Revealing/UnlockinganAchievement
IncrementinganAchievement
PostingaScoretoaLeaderboard
ShowingtheAchievementsUI
ShowingtheLeaderboardUI
AccessingLeaderboarddata
UsingSocial.ILeaderboard
UsingPlayGamesPlatform.LoadScores()
Gettingplayernames
RecordingEvents
SavingGameStatetotheCloud
DisplayingsavedgamesUI
Openingasavedgame
Writingasavedgame
Readingasavedgame
Deletingasavedgame
Retrievingserverauthenticationcodes
Gettinganotherserverauthcodeafterexchangingthefirstcode
Retrievingplayer'semail
Retrievingplayer'sIDToken
VideoRecording
GetVideoCaptureCapabilities
LaunchtheVideoCaptureOverlay
GettheCurrentVideoCaptureState
SetupaListenerforLiveUpdatestotheCaptureState
Signout
Decreasingapksize
PlayGamesServicesProguardconfiguration
(Advanced)UsingthePluginWithoutOverridingtheDefaultSocialPlatform
SpecialThanks
README.md
GooglePlayGamespluginforUnity
Copyright(c)2014GoogleInc.Allrightsreserved.
TheGooglePlayGamespluginforUnity®isanopen-sourceprojectwhosegoal
istoprovideapluginthatallowsgamedeveloperstointegratewith
theGooglePlayGamesAPIfromagamewritteninUnity®.However,thisprojectis
notinanywayendorsedorsupervisedbyUnityTechnologies.
Unity®isatrademarkofUnityTechnologies.
iOSisatrademarkofApple,Inc.
Overview
TheGooglePlayGamespluginforUnityallowsyoutoaccesstheGooglePlayGames
APIthroughUnity'ssocialinterface.
Thepluginprovidessupportforthe
followingfeaturesoftheGooglePlayGamesAPI:
signin
friends
unlock/reveal/incrementachievement
postscoretoleaderboard
cloudsaveread/write
showbuilt-inachievement/leaderboardsUI
events
videorecordingofgameplay
nearbyconnections
NOTICE:ThisversionofthepluginnolongersupportsiOS.GooglePlaygamesservicesforiOSisdeprecated,
andisnotlikelytofunctionasexpected.DonotuseGooglePlaygames
servicesforiOSinnewapps.Seethedeprecationannouncementblogpostformoredetails.
Features:
easyGUI-orientedprojectsetup(integratedintotheUnityGUI)
noneedtooverride/customizetheplayerActivity
noneedtooverride/customizeAndroidManifest.xml
Systemrequirements:
Unity®5orabove.
TodeployonAndroid:
AndroidSDK
Androidv4.0orhigher
GooglePlayServiceslibrary,version11.6orabove
Upgrading
Ifyouhavealreadyintegratedyourprojectwithapreviousversionofthe
pluginandwishtoupgradetoanewversion,pleaserefertothe
upgradeinstructions.
ConfigureYourGame
Tousetheplugin,youmustfirstconfigureyour
gameinthe
GooglePlayDeveloperConsole.FollowtheinstructionsoncreatingaclientID.
Beparticularlycarefulwhenenteringyourpackagenameandyour
certificatefingerprints,sincemistakesonthosescreenscanbedifficultto
recoverfrom.
Copythegameresourcesfromtheconsole
Onceyouconfigureatleastoneresource(event,achievement,orleaderboard),
copytheresourceconfigurationfromtheGooglePlayDeveloperConsole,andpasteit
intothesetupconfigurationinUnity.TogettheresourcesgototheAchievements
tab,thenclickon"Getresources"onthebottomofthelist.
Thenclickthe"Androidsection".
Selectallthecontentsoftheresourceswindow,andcopythemtotheclipboard.
Pastethegameresourcesintothepluginsetupdialog
BackinUnity,openthesetupdialogWindow>GooglePlayGames>Setup...>AndroidSetup
Enterthedirectorytosaveconstants-Enterthefolderfortheconstantsfile.
Constantsclassname-thisisthenameoftheC#classtocreate,includingnamespace.
ResourcesDefinition-pastetheresourcedatafromthePlayGamesconsolehere.
WebclientID-thisistheclientIDofthelinkedwebapp.Itisonlyneededif
youhaveaweb-basedbackendforyourgameandneedaserverauthcodetobe
exchangedforanaccesstokenbythebackendserver,orifyouneedanidtoken
fortheplayertomakeother,non-game,APIcalls.
Thesetupprocesswillconfigureyourgamewiththeclientidandgeneratea
C#classthatcontainsconstantsforeachofyourresources.
SetupChecklist
Makesuretodothefollowingiftheyarerelevanttoyourgame:
AddtesteremailaddressestothetestingsectionofyourgameonthePlayGamesConsole.
TheSHA1fingerprintusedtocreatethelinkedAndroidappisfromthekeystore
usedtosigntheUnityapplication.
AddAchievementsandLeaderboards
Add
achievements
and
leaderboards
toyourgameintheGooglePlayDeveloperConsole.Foreachachievementand
leaderboardyouconfigure,makesuretonote
thecorrespondingachievementIDorleaderboardID,
asthosewillbeneededwhenmakingtheAPIcalls.
AchievementandleaderboardIDsarealphanumericstrings(e.g."Cgkx9eiuwi8_AQ").
AddEvents
Eventsallowyoutotrackuseractionsinyourgameandreportonthemwith
Analytics.
ReadmoreabouthowtoconfigureanduseEventson
GameConcepts-Events
LoadYourGameProject
Next,loadyourgameprojectintotheUnityeditor.
Ifyoudonothaveagameprojecttoworkwith,youcanusetheMinimalsample
availableinthesamplesdirectory.Usingthatsamplewillallowyouto
quicklytestyoursetupandmakesureyoucanaccesstheAPI.
Ifyouwanttotestalargersampleafteryouarefamiliarwiththeplugin,
trytheCubicPilotgame.
Moreinformationaboutbuildingthesamplescanbefoundinthe
samplesREADMEfile.
PluginInstallation
Todownloadtheplugin,clonethisGitrepositoryintoyourfilesystem(ordownloaditas
aZIPfileandunpackit).Then,lookfortheunitypackagefilein
thecurrent-builddirectory:
current-build/GooglePlayGamesPluginForUnity-X.YY.ZZ.unitypackage
Toinstalltheplugin,simplyopenyourgameprojectinUnityandimportthatfileinto
yourproject'sassets,asyouwouldanyotherUnitypackage.Thisisaccomplishedthrough
theAssets>ImportPackage>CustomPackagemenuitem(youcanalsoreachthismenuit
byright-clickingtheAssetsfolder).
Next,makesureyourcurrentbuildplatformissettoAndroid.From
File>BuildSettings…selectAndroidandclickSwitchPlatform.
YoushouldnowseeanewmenuitemwasaddedunderWindow>GooglePlayGames.
Ifyoudon'tseethenewmenuitems,refreshtheassetsby
clickingAssets>Refreshandtryagain.
AndroidSetup
Next,setupthepathtoyourAndroidSDKinstallationinUnity.Thisislocatedinthe
preferencesmenu,undertheExternalToolssection.
ToconfigureyourUnitygametorunwithGooglePlayGamesonAndroid,first
opentheAndroidSDKmanagerandverifythatyouhavedownloadedthefollowing
packages.DependingonifyouareusingtheSDKmanagerfromAndroidStudio,
orusingthestandaloneSDKmanager,thenameofthecomponentsmaybedifferent.
GooglePlayServices
AndroidSupportLibrary
LocalMavenrepositoryforSupportLibraries(AlsoknownasAndroidSupportRepository)
GoogleRepository
Android6.0(API23)(thisdoesnotaffecttheminSDKversion).
Next,configureyourgame'spackagename.Todothis,clickFile>BuildSettings,
selecttheAndroidplatformandclickPlayerSettingstoshowUnity's
PlayerSettingswindow.Inthatwindow,lookfortheBundleIdentifiersetting
underOtherSettings.Enteryourpackagenamethere(forexample
com.example.my.awesome.game).
InordertosignintoPlayGameServices,youneedtosignyourAPKfile,
makesurethatyouaresigningitwiththe
correctcertificate,thatis,theonethatcorrespondstotheSHA1certificate
fingerprintyouenteredintheDeveloperConsoleduringthesetup.
Next,clicktheWindow|GooglePlayGames|Setup-Androidsetupmenuitem.
ThiswilldisplaytheAndroidsetupscreen.
EntertheConstantsclassname.Thisisthenameofthefullyqualifiedclass
thatwillbeupdated(orcreated)whichcontainstheIDsofthegameresources.
Theformatofthenameis..Forexample,AwesomeGame.GPGSIds
Pastetheresourcedefinitiondata.ThisistheXMLdatafromtheGooglePlayDeveloperConsole
whichcontainstheresourceIDsaswellastheApplicationIDforAndroid.
ThisdataisfoundintheGooglePlayDeveloperConsolebyclicking"Getresources"onanyofthe
resourcepages(e.g.AchievementsorLeaderboards),thenclickingAndroid.
Afterpastingthedataintothetextarea,clicktheSetupbutton.
Note:Ifyouareusingawebapplicationorbackendserverwithyourgame,
youcanlinkthewebapplicationtothegametoenablegettingtheplayer's
idtokenand/oremailaddress.Todothis,linkawebapplicationtothe
gameintheGooglePlayDeveloperConsole,andentertheclientidfor
thewebapplicationintothesetupdialog.
AdditionalinstructionsonbuildingforAndroidonWindows
IfyouareusingWindows,youmustmakesurethatyourJavaSDKinstallation
canbeaccessedbyUnity.Todothis:
SettheJAVA_HOMEenvironmentvariabletoyourJavaSDKinstallationpath
(forexample,C:\ProgramFiles\Java\jdk1.7.0_45).
AddtheJavaSDK'sbinfoldertoyourPATHenvironmentvariable
(forexample,C:\ProgramFiles\Java\jdk1.7.0_45\bin)
Reboot.
Howtoeditenvironmentvariables:InWindows2000/XP/Vista/7,
right-clickMyComputer,thenProperties,thengotoAdvancedSystemProperties
(orSystemPropertiesandthenclicktheAdvancedtab),then
clickEnvironmentVariables.OnWindows8,pressWindowsKey+Wand
searchforenvironmentvariables
Formoreinformation,consultthedocumentationforyourversionofWindows.
RuntheProject
IfyouareworkingwiththeMinimalsample,youshouldbeabletobuild
andruntheprojectatthispoint.YouwillseeascreenwithanAuthenticatebutton,
andyoushouldbeabletosigninwhenyouclickit.
TobuildandrunonAndroid,click
File>BuildSettings,selecttheAndroidplatform,then
SwitchtoPlatform,thenBuildandRun.
ISocialPlatformCompliance
TheGooglePlayGamespluginimplementsUnity's
socialinterface,
forcompatibilitywithgamesthatalreadyusethatinterfacewhenintegratingwithother
platforms.However,somefeaturesareuniquetoPlayGamesandare
offeredasextensionstothestandardsocialinterfaceprovidedbyUnity.
ThestandardAPIcallscanbeaccessedthroughtheSocial.Activeobject,
whichisareferencetoanISocialPlatforminterface.Thenon-standard
GooglePlayGamesextensionscanbeaccessedbycastingtheSocial.Active
objecttothePlayGamesPlatformclass,wheretheadditionalmethodsare
available.
NearbyConnectionsConfiguration
Inordertousenearbyconnections,aserviceidwhichuniquelyidentifiesthe
setofapplicationsthatcaninteractneedstobeconfigured.
ThisisdonebyclickingtheWindow>GooglePlayGames>NearbyConnectionssetup...
menuitem.Thiswilldisplaythe
nearbyconnectionssetupscreen.OnthisscreenentertheserviceIDyouwanttouse.
Itshouldbesomethingthatidentifiesyourapplication,andfollowsthe
samerulesasthebundleid(forexample:com.example.myawesomegame.nearby).
Onceyouentertheid,pressSetup.
Tousenearbyconnections,theplayerdoesnotneedtobeauthenticated,
andnoGooglePlayDeveloperConsoleconfigurationisneeded.
Fordetailedinformationonnearbyconnectionusage,
pleaserefertonearbyconnections.
Configuration&InitializationPlayGameServices
Inordertosavegameprogressorrequireaccesstoaplayer'sGoogle+social
graph,thedefaultconfigurationneedstobereplacedwithacustom
configuration.TodothisusethePlayGamesClientConfiguration.
Ifyourgamedoesnotusethesefeatures,thenthereisnoneedto
usingGooglePlayGames;
usingGooglePlayGames.BasicApi;
usingUnityEngine.SocialPlatforms;
PlayGamesClientConfigurationconfig=newPlayGamesClientConfiguration.Builder()
//enablessavinggameprogress.
.EnableSavedGames()
//requeststheemailaddressoftheplayerbeavailable.
//Willbringupapromptforconsent.
.RequestEmail()
//requestsaserverauthcodebegeneratedsoitcanbepassedtoan
//associatedbackendserverapplicationandexchangedforanOAuthtoken.
.RequestServerAuthCode(false)
//requestsanIDtokenbegenerated.ThisOAuthtokencanbeusedto
//identifytheplayertootherservicessuchasFirebase.
.RequestIdToken()
.Build();
PlayGamesPlatform.InitializeInstance(config);
//recommendedfordebugging:
PlayGamesPlatform.DebugLogEnabled=true;
//ActivatetheGooglePlayGamesplatform
PlayGamesPlatform.Activate();
Afteractivated,youcanaccessthePlayGamesplatformthrough
Social.Active.YoushouldonlycallPlayGamesPlatform.Activateoncein
yourapplication.Makingthiscallwillnotdisplayanythingonthescreenand
willnotinteractwiththeuserinanyway.
AddingadditionalScopes
Youcanaddadditionalscopestotheauthenticationprocessbycalling
PlayGamesClientConfiguration.Builder().AddOauthScope(scope).
Note:addingadditionalscopeswithmostlikelyrequireuserconsentwhen
startingyourgame.
Signin
Tosignin,callPlayGamesPlatform.Instance.Authenticate,withaSignInInteractivityenum.
UsingtheenumCanPromptOncefollowsthebestsign-inpracticesanditshouldbeusedwhenyourgamestarts.Itwill:
Alwaysattempttosilentsign-in
Ifsilentsign-infails,checkiftheuserhaspreviouslydeclinedtosignin.
Iftheuserhasn’tpreviouslydeclinedtosignin,thenchecktheinternetconnectionandpromptinteractivesign-inifinternetisavailable
Iftheinteractivesign-iniscancelledbytheuser,torememberthisasa‘decline’forthe2ndstepofthenextsign-inattempt.
Additionally,youshouldputasign-in/sign-outbuttoncontrolforPlayGamessomewherethatmakessenseforyourgame,andwhereyouruserscaneasilyfindit.Forthisbutton,youshouldusetheenumCanPromptAlways.
ThefulllistofenumsforPlayGamesPlatform.Instance.Authenticateare:
CanPromptOncewillhelpusershaveaseamlesssign-inexperience,byautomaticallysigningtheminwhenyourgamestarts.Ifautomaticsign-inisnotsuccessful,theywillbepromptedtosigninmanuallywithaninteractivesign-inscreen.Iftheuserdoesnotsigninusingtheinteractivescreen,theywillnotbeaskedtosignininteractivelyagain.Usethiswhenyourgamestartsup.
CanPromptAlwayswhenused,interactivesign-inwillbestarted,ifsilentsign-infailsandtheuserwillseealwaysseeUIs(thisdoesnotcountthenumberofdeclines).Usethisforyourin-gamePGSsign-inbutton.
NoPromptwhenused,silentsign-inwillbeattemptedbutnoUIswillbeshowntotheuserifsilentsign-infails.
usingGooglePlayGames;
usingUnityEngine.SocialPlatforms;
...
//authenticateuser:
PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptOnce,(result)=>{
//handleresults
});
Theresultcodeisanenum,whichgivesyoudifferentfailurereasonsthatwillhelpyouunderstandsign-infailuresbetter.
Interactivesign-inwillshowtherequiredconsentdialogs.Iftheuserhasalready
signedintothegameinthepast,thisprocesswillbesilentandtheuserwill
nothavetointeractwithanydialogs.
NotethatyoucannotmakeanygamesAPIcalls(unlockachievements,postscores,
etc)untilyougetasuccessfulreturnvaluefromAuthenticate,soitis
goodpracticetoputupastandbyscreenuntilthecallbackiscalled,tomake
suretheusercan'tstartplayingthegameuntiltheauthenticationprocess
completes.
Incrementalauthorization
Asopposedtoaskingallscopesyouneedinadvance,youcanstartwiththemostbasiconesandthenaskfornewscopeswhenyouneedthemasit’ssuggestedinthequalitychecklist.Thiswillallowsilentsign-intosucceed,becausesilentsign-inwillalwaysfailifyouaskformorethanGAMES_LITEand,ifyouuseSavedGames,DRIVE.APP_DATA.Askingforscopesatthetimeyouneedthemwillalsohelpusersunderstandwhyyouareaskingforascopeandmakethemmorelikelytogivepermissions.
Tolearnifyoualreadyhaveapermissionforascopeandtoaskforanewone,youcanusethecodesnippetsbelow.
PlayGamesPlatform.Instance.HasPermission("email")
PlayGamesPlatform.Instance.RequestPermission("email",result=>{
//handleresults
});
Friends
PlayGamesFriendsallowsplayerstocreateandmaintainacross-gamesfriendslist.Youcanrequestaccesstothisfriendslisttohelpyourplayersplayyourgamewiththeirfriends.SeetheFriendsconceptpageformoredetailsonthefriendssystem.
ToenableFriends,usethefollowingfunctions:
Viewfriends:Requestaccesstoaplayer’sfriendslist,soyoucanaddtheirplaygamesfriendstoyourin-gamefriendslist
Viewaplayerprofile:LetaplayerviewthePlayGamesprofileofanotherplayer.Thisisessentialsoaplayerknowswhotheirfriendsare,andcanconnecttootherPlayGamesplayersinyourgame.ThiswillneedtobetiedtoaUIelementtotriggerthepopup.Seethefriendsguidelinesfordetails.
SeethebestpracticesguidelinesforinstructionsonhowbesttoimplementtheseAPIs.
Note:TouseFriends,youneedtoupdateyourPGSSDKtoversion20.0.0
Viewfriends
Therearetwowaystoloadfriends,eitherusingtheISocialframeworkordirectlywithPlayGamesPlatform.
LoadingfriendswiththeISocialframework
Social.localUser.LoadFriends((success)=>{
Debug.Log("FriendsloadedOK:"+ok));
foreach(IUserProfilepinSocial.localUser.friends){
Debug.Log(p.userName+"isafriend");
}
However,thiscallwillfailifthecurrentplayerhasnotyetgrantedpermissiontothegametoaccessthisinformation.UseGetLastLoadFriendsStatustocheckifLoadFriendsfailedduetomissingconsent.
PlayGamesPlatform.Instance.GetLastLoadFriendsStatus((status)=>{
//Checkforconsent
if(status==LoadFriendsStatus.ResolutionRequired){
//Askforresolution.
}
});
AgamecanaskthecurrentplayertosharethefriendslistbycallingAskForLoadFriendsResolution.
PlayGamesPlatform.Instance.AskForLoadFriendsResolution((result)=>{
if(result==UIStatus.Valid){
//Useragreedtosharefriendswiththegame.Reloadfriends.
}else{
//Userdoesn’tagreetosharethefriendslist.
}
});
Thisfunctionwillshowtheappropriateplatform-specificfriendssharingUI.ThisUIaskstheplayeriftheywanttosharetheirfriendswiththegame.
LoadingfriendswithPlayGamesPlatform
AnotherwayofloadingfriendsistouseLoadFriendsandLoadMoreFriends:
PlayGamesPlatform.Instance.LoadFriends(pageSize,forceReload,(status)=>{
//Checkifthecallissuccessfulandiftherearemorefriendstoload.
});
PlayGamesPlatform.Instance.LoadMoreFriends(pageSize,(status)=>{
//Checkiftherearemorefriendstoload.
});
ThepageSizeparamrepresentsthenumberofentriestorequestforthispage.Notethatifcacheddataalreadyexists,thereturnedbuffermaycontainmorethanthissize.Thebufferisguaranteedtocontainatleastthismanyentriesifthecollectioncontainsenoughrecords.IfforceReloadissettotrue,thiscallwillclearanylocally-cacheddataandattempttofetchthelatestdatafromtheserver.Thiswouldcommonlybeusedforactionslikeauser-initiatedrefresh.Normally,thisshouldbesettofalsetogaintheadvantagesofdatacaching.
IfthecallbackreturnsLoadFriendsStatus.LoadMore,thentherearemorefriendstoload.LoadFriendsStatus.ResolutionRequiredsignalsthattheuserhasnotsharedthefriendslistandyoucandirectlycallPlayGamesPlatform.Instance.AskForLoadFriendsResolution.
Determiningfriendslistvisibility
UsePlayGamesPlatform.Instance.GetFriendsListVisibilitytocheckiftheuserhassharedthefriendslistwiththegame.Possiblereturnstatusesare:
FriendsListVisibilityStatus.RequestRequiredindicatesyoumustaskforconsent.
FriendsListVisibilityStatus.Visibleindicatesthatloadingthefriendslistshouldsucceed.
FriendsListVisibilityStatus.Unknowngenerallyshouldn'thappen.YoucansetforceReloadtotruetorefreshthedata.
PlayGamesPlatform.Instance.GetFriendsListVisibility(forceReload,(friendsListVisibilityStatus)=>{});
Viewaplayerprofile
Toaddorremoveaplayerasafriend,usetheshowandcompareprofilefunction.ThisfunctiontriggersabottomsheetdialogshowingthePlayGamesprofileoftheuser;callthefunctionwiththeplayerIdoftherequestedplayer.Iftheplayerandfriendhavein-gamenicknames,usetheminthecalltoaddmorecontexttotheprofileUI:
PlayGamesPlatform.Instance.ShowCompareProfileWithAlternativeNameHintsUI(
mFirstFriendId,/*otherPlayerInGameName=*/null,/*currentPlayerInGameName=*/null,
(result)=>{
//Profilecomparisonviewhasclosed.
});
PlayerStatistics
ThePlayerStatsAPIletyoutailorgameexperiencestospecificsegments
ofplayersanddifferentstagesoftheplayerlifecycle.Youcanbuild
tailoredexperiencesforeachplayersegmentbasedonhowplayersare
progressing,spending,andengaging.Forexample,youcanusethisAPIto
takeproactiveactionstoencouragealessactiveplayertore-engagewith
yourgame,suchasbydisplayingandpromotingnewin-gameitemswhenthe
playersignsin.
Thecallbacktakestwoparameters:
Theresultcodelessthanorequaltozeroissuccess.
SeeCommonStatusCodesforallvalues.
ThePlayerStatsobjectoftypeGooglePlayGames.PlayGamesLocalUser.PlayerStats
FormoreinformationseePlayerStats.
Theplayerstatsareavailableafterauthenticating:
((PlayGamesLocalUser)Social.localUser).GetStats((rc,stats)=>
{
//-1meanscachedstats,0issucceess
//seeCommonStatusCodesforallvalues.
if(rc<=0&&stats.HasDaysSinceLastPlayed()){
Debug.Log("Ithasbeen"+stats.DaysSinceLastPlayed+"days");
}
});
Settingpopupgravity
Youcansetthegravityusedbypopupswhenshowinggameserviceselements
suchasachievementnotifications.ThedefaultisTOP.Thiscanonly
besetafterauthentication.Forexample:
Social.localUser.Authenticate((boolsuccess)=>
{
if(success)
{
((GooglePlayGames.PlayGamesPlatform)Social.Active).SetGravityForPopups(Gravity.BOTTOM);
}
});
Revealing/UnlockinganAchievement
Tounlockanachievement,usetheSocial.ReportProgressmethodwitha
progressvalueof100.0f:
usingGooglePlayGames;
usingUnityEngine.SocialPlatforms;
...
//unlockachievement(achievementID"Cfjewijawiu_QA")
Social.ReportProgress("Cfjewijawiu_QA",100.0f,(boolsuccess)=>{
//handlesuccessorfailure
});
Noticethataccordingtotheexpectedbehaviorof
Social.ReportProgress,
aprogressof0.0fmeansrevealingtheachievementandaprogressof100.0f
meansunlockingtheachievement.Therefore,torevealanachievement(thatwas
previouslyhidden)withoutunlockingit,simplycallSocial.ReportProgresswith
aprogressof0.0f.
IncrementinganAchievement
Ifyourachievementisincremental,thePlayGamesimplementationof
Social.ReportProgresswilltrytobehaveascloselyaspossibletothe
expectedbehavioraccordingtoUnity'ssocialAPI,butmaynotbeexact.For
thisreason,werecommendthatyoudonotuseSocial.ReportProgressfor
incrementalachievements.Instead,usethe
PlayGamesPlatform.IncrementAchievementmethod,whichisaPlayGames
extension.
usingGooglePlayGames;
usingUnityEngine.SocialPlatforms;
...
//incrementachievement(achievementID"Cfjewijawiu_QA")by5steps
PlayGamesPlatform.Instance.IncrementAchievement(
"Cfjewijawiu_QA",5,(boolsuccess)=>{
//handlesuccessorfailure
});
PostingaScoretoaLeaderboard
Topostascoretoaleaderboard,callSocial.ReportScore.
usingGooglePlayGames;
usingUnityEngine.SocialPlatforms;
...
//postscore12345toleaderboardID"Cfji293fjsie_QA")
Social.ReportScore(12345,"Cfji293fjsie_QA",(boolsuccess)=>{
//handlesuccessorfailure
});
TopostascoreandincludeametadatatagusethePlayGameServicesinstance
directly:
usingGooglePlayGames;
usingUnityEngine.SocialPlatforms;
...
//postscore12345toleaderboardID"Cfji293fjsie_QA"andtag"FirstDaily")
Social.ReportScore(12345,"Cfji293fjsie_QA","FirstDaily",(boolsuccess)=>{
//handlesuccessorfailure
});
Notethattheplatformandtheserverwillautomaticallydiscardscoresthatare
lowerthantheplayer'sexistinghighscore,soyoucansubmitscoresfreely
withoutanycheckstotestwhetherornotthescoreisgreaterthantheplayer's
existingscore.
ShowingtheAchievementsUI
Toshowthebuilt-inUIforallachievements,call
Social.ShowAchievementsUI.
usingGooglePlayGames;
usingUnityEngine.SocialPlatforms;
...
//showachievementsUI
Social.ShowAchievementsUI();
ShowingtheLeaderboardUI
Toshowthebuilt-inUIforallleaderboards,callSocial.ShowLeaderboardUI.
usingGooglePlayGames;
usingUnityEngine.SocialPlatforms;
...
//showleaderboardUI
Social.ShowLeaderboardUI();
Ifyouwishtoshowaparticularleaderboardinsteadofallleaderboards,you
canpassaleaderboardIDtothemethod.This,however,isaPlayGames
extension,sotheSocial.ActiveobjectneedstobecasttoaPlayGamesPlatform
objectfirst:
usingGooglePlayGames;
usingUnityEngine.SocialPlatforms;
...
//showleaderboardUI
PlayGamesPlatform.Instance.ShowLeaderboardUI("Cfji293fjsie_QA");
AccessingLeaderboarddata
Thereare2methodstoretrievingtheleaderboardscoredata.
UsingSocial.ILeaderboard
ThismethodusestheILeaderboardinterfacetodefinethescopeandfilters
forgettingthedata.Thisapproachallowsyoutoconfigure:
TheleaderboardId
Thecollection(socialorpublic)
Thetimeframe(daily,weekly,all-time)
Therankpositiontostartretrievingscores.
Thenumberofscores(thedefaultis25).
Filterbyuserid.
Ifthefromparameterisnon-positive,thentheresultsreturnedare
player-centered,meaningthescoresaroundthecurrentplayer'sscoreare
returned.
ILeaderboardlb=PlayGamesPlatform.Instance.CreateLeaderboard();
lb.id="MY_LEADERBOARD_ID";
lb.LoadScores(ok=>
{
if(ok){
LoadUsersAndDisplay(lb);
}
else{
Debug.Log("Errorretrievingleaderboardi");
}
});
UsingPlayGamesPlatform.LoadScores()
ThismethodusesthePlayGamesPlatformdirectly.Thisapproachprovides
additionalflexibilityandinformationwhenaccessingtheleaderboarddata.
PlayGamesPlatform.Instance.LoadScores(
GPGSIds.leaderboard_leaders_in_smoketesting,
LeaderboardStart.PlayerCentered,
100,
LeaderboardCollection.Public,
LeaderboardTimeSpan.AllTime,
(data)=>
{
mStatus="Leaderboarddatavalid:"+data.Valid;
mStatus+="\napprox:"+data.ApproximateCount+"have"+data.Scores.Length;
});
TheparametersforLoadScores()are:
leaderboardId
startposition(topscoresorplayercentered)
rowcount
leaderboardcollection(socialorpublic)
timespan(daily,weekly,all-time)
callbackacceptingaLeaderboardScoreDataobject.
TheLeaderboardScoreDataclassisusedtoreturninformationbacktothe
callerwhenloadingscores.Themembersare:
1.Id-theleaderboardid
2.Valid-trueifthereturneddataisvalid(thecallwassuccessful)
3.Status-theResponseStatusofthecall
4.ApproximateCount-theapproximatenumberofscoresintheleaderboard
5.Title-thetitleoftheleaderboard
6.PlayerScore-thescoreofthecurrentplayer
7.Scores-thelistofscores
8.PrevPageToken-atokenthatcanbeusedtocallLoadMoreScores()to
getthepreviouspageofscores.
9.NextPageToken-atokenthatcanbeusedtocallLoadMoreScores()to
getthenextpageofscores.
voidGetNextPage(LeaderboardScoreDatadata)
{
PlayGamesPlatform.Instance.LoadMoreScores(data.NextPageToken,10,
(results)=>
{
mStatus="Leaderboarddatavalid:"+data.Valid;
mStatus+="\napprox:"+data.ApproximateCount+"have"+data.Scores.Length;
});
}
ThiscallmayfailwhentryingtoloadfriendswithResponseCode.ResolutionRequirediftheuserhasnotsharedtheirfriendslistwiththegame.Inthiscase,useAskForLoadFriendsResolutiontorequestaccess.
Gettingplayernames
EachscorehastheuserIdoftheplayerthatmadethescore.Youcanuse
Social.LoadUsers()toloadtheplayerprofile.Rememberthatthecontents
oftheplayerprofilearesubjecttoprivacysettingsoftheplayers.
internalvoidLoadUsersAndDisplay(ILeaderboardlb)
{
//gettheuserids
List
延伸文章資訊
- 1Google Play Games 2021.08.29097 (400311724.400311724 ...
Google Play Games是Google公司专为电子游戏推出的社交网络,它和苹果公司的Game Center很相似。 有了Google Play Games,玩家可以搜寻到自己最感兴趣的...
- 2Google Play 遊戲服務功能- Play 管理中心說明
Google Play 遊戲服務功能. 您可以透過Google Play 遊戲服務,在遊戲中加入具有吸引力的社交功能和多人對戰功能。 ... Google Play Game Services ...
- 3Best Google Play games for Android - For The Win
Best Google Play games for Android · Share this article · Genshin Impact · Pikmin Bloom · League ...
- 4Google Play games services - Android Developers
Google Play games services provide a seamless zero-click sign-in system for players and a range o...
- 5Google Play 遊戲- Google Play 應用程式
Google Play 遊戲應用程式讓遊戲變得更好玩!你可以發掘你喜愛的各種新遊戲,與好友一較高下, 並追蹤自己的光榮記錄。玩家檔案可讓你展現遊戲技藝,讓其他玩家看看你 ...