Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Game Center Support

Discussion in 'iOS and tvOS' started by DigitalDuane, Dec 26, 2011.

  1. natsuto

    natsuto

    Joined:
    Feb 8, 2012
    Posts:
    3
    I have been MonoDevelop dependence of the Assembly browser. But I then verify returns before the exception, I do not know how this is going.
     
  2. quitebuttery

    quitebuttery

    Joined:
    Mar 12, 2011
    Posts:
    327
    Uh....I can't undertand that sentence at all.

    Basically what I do is check if the localUser is authenticated--if not, I authenticate it. I do this at the beginning of the app, and also when the app comes back from suspension.
     
  3. gangrel

    gangrel

    Joined:
    Jan 3, 2012
    Posts:
    28
    I don't bother testing authenticated, I just authenticate.
     
  4. quitebuttery

    quitebuttery

    Joined:
    Mar 12, 2011
    Posts:
    327
    I'm trying to avoid logging the user in if he's already logged in in game center, since game center logins take a long time. But I guess that's not possible.
     
  5. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    Do we have an example project on how to use game center in unity?
    From what I read in this thread, it seems quite a hassle to set up, no?
    Documentation is lacking, some hackery magic is required.
    It would be great if someone shared a working example.
    thanks
     
  6. quitebuttery

    quitebuttery

    Joined:
    Mar 12, 2011
    Posts:
    327
    It's actually very simple to use--but it's totally undocumented. Maybe I'll whip up an example since I finally got mine working perfectly via trial and error.
     
  7. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    Thank you kind sir! Please do, if it's not too much trouble :)
     
  8. quitebuttery

    quitebuttery

    Joined:
    Mar 12, 2011
    Posts:
    327
  9. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Would love to see a similar tutorial for leaderboards if anyone knows how it's done?
     
  10. gangrel

    gangrel

    Joined:
    Jan 3, 2012
    Posts:
    28
    Here's a code sample, showing basic Game Center support...

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.SocialPlatforms;
    4.  
    5. public class Startup : MonoBehaviour
    6. {
    7.     // we'll create some buttons in OnGui, allowing us to bump achievement and
    8.     // score values for testing
    9.    
    10.     private double ach1 = 0;
    11.     private double ach2 = 0;
    12.     private double ach3 = 0;
    13.     private double ach4 = 0;
    14.    
    15.     private long score1 = 1000;
    16.     private long score2 = 200;
    17.    
    18.     private int buttonWidth = 120;
    19.     private int buttonHeight = 50;
    20.     private int buttonGap = 10;
    21.    
    22.     void Start()
    23.     {
    24.         Social.localUser.Authenticate(HandleAuthenticated);
    25.     }
    26.    
    27.     // authentication
    28.    
    29.     private void HandleAuthenticated(bool success)
    30.     {
    31.         Debug.Log("*** HandleAuthenticated: success = " + success);
    32.         if (success) {
    33.             Social.localUser.LoadFriends(HandleFriendsLoaded);
    34.             Social.LoadAchievements(HandleAchievementsLoaded);
    35.             Social.LoadAchievementDescriptions(HandleAchievementDescriptionsLoaded);
    36.         }
    37.     }
    38.    
    39.     private void HandleFriendsLoaded(bool success)
    40.     {
    41.         Debug.Log("*** HandleFriendsLoaded: success = " + success);
    42.         foreach (IUserProfile friend in Social.localUser.friends) {
    43.             Debug.Log("*   friend = " + friend.ToString());
    44.         }
    45.     }
    46.    
    47.     private void HandleAchievementsLoaded(IAchievement[] achievements)
    48.     {
    49.         Debug.Log("*** HandleAchievementsLoaded");
    50.         foreach (IAchievement achievement in achievements) {
    51.             Debug.Log("*   achievement = " + achievement.ToString());
    52.         }
    53.     }
    54.    
    55.     private void HandleAchievementDescriptionsLoaded(IAchievementDescription[] achievementDescriptions)
    56.     {
    57.         Debug.Log("*** HandleAchievementDescriptionsLoaded");
    58.         foreach (IAchievementDescription achievementDescription in achievementDescriptions) {
    59.             Debug.Log("*   achievementDescription = " + achievementDescription.ToString());
    60.         }
    61.     }
    62.    
    63.     // achievements
    64.    
    65.     public void ReportProgress(string achievementId, double progress)
    66.     {
    67.         if (Social.localUser.authenticated) {
    68.             Social.ReportProgress(achievementId, progress, HandleProgressReported);
    69.         }
    70.     }
    71.    
    72.     private void HandleProgressReported(bool success)
    73.     {
    74.         Debug.Log("*** HandleProgressReported: success = " + success);
    75.     }
    76.    
    77.     public void ShowAchievements()
    78.     {
    79.         if (Social.localUser.authenticated) {
    80.             Social.ShowAchievementsUI();
    81.         }
    82.     }
    83.    
    84.     // leaderboard
    85.    
    86.     public void ReportScore(string leaderboardId, long score)
    87.     {
    88.         if (Social.localUser.authenticated) {
    89.             Social.ReportScore(score, leaderboardId, HandleScoreReported);
    90.         }
    91.     }
    92.    
    93.     public void HandleScoreReported(bool success)
    94.     {
    95.         Debug.Log("*** HandleScoreReported: success = " + success);
    96.     }
    97.    
    98.     public void ShowLeaderboard()
    99.     {
    100.         if (Social.localUser.authenticated) {
    101.             Social.ShowLeaderboardUI();
    102.         }
    103.     }
    104.    
    105.     // gui
    106.    
    107.     public void OnGUI()
    108.     {
    109.         // four buttons, allowing us to bump and test setting achievements
    110.         int yDelta = buttonGap;
    111.         if (GUI.Button(new Rect(buttonGap, yDelta, buttonWidth, buttonHeight), "Ach 1")) {
    112.             ReportProgress("A0001", ach1);
    113.             ach1 = (ach1 == 100) ? 0 : ach1 + 10;
    114.         }
    115.         yDelta += buttonHeight + buttonGap;
    116.         if (GUI.Button(new Rect(buttonGap, yDelta, buttonWidth, buttonHeight), "Ach 2")) {
    117.             ReportProgress("A0002", ach2);
    118.             ach2 = (ach2 == 100) ? 0 : ach2 + 10;
    119.         }
    120.         yDelta += buttonHeight + buttonGap;
    121.         if (GUI.Button(new Rect(buttonGap, yDelta, buttonWidth, buttonHeight), "Ach 3")) {
    122.             ReportProgress("A0003", ach3);
    123.             ach3 = (ach3 == 100) ? 0 : ach3 + 10;
    124.         }
    125.         yDelta += buttonHeight + buttonGap;
    126.         if (GUI.Button(new Rect(buttonGap, yDelta, buttonWidth, buttonHeight), "Ach 4")) {
    127.             ReportProgress("A0004", ach4);
    128.             ach4 = (ach4 == 100) ? 0 : ach4 + 10;
    129.         }
    130.         // show achievements
    131.         yDelta += buttonHeight + buttonGap;
    132.         if (GUI.Button(new Rect(buttonGap, yDelta, buttonWidth, buttonHeight), "Show Achievements")) {
    133.             ShowAchievements();
    134.         }
    135.        
    136.         // two buttons, allowing us to bump and test setting high scores
    137.         int xDelta = Screen.width - buttonWidth - buttonGap;
    138.         yDelta = buttonGap;
    139.         if (GUI.Button(new Rect(xDelta, yDelta, buttonWidth, buttonHeight), "Score 1")) {
    140.             ReportScore("L01", score1);
    141.             score1 += 500;
    142.         }
    143.         yDelta += buttonHeight + buttonGap;
    144.         if (GUI.Button(new Rect(xDelta, yDelta, buttonWidth, buttonHeight), "Score 2")) {
    145.             ReportScore("L02", score2);
    146.             score2 += 100;
    147.         }
    148.         // show leaderboard
    149.         yDelta += buttonHeight + buttonGap;
    150.         if (GUI.Button(new Rect(xDelta, yDelta, buttonWidth, buttonHeight), "Show Leaderboard")) {
    151.             ShowLeaderboard();
    152.         }
    153.     }
    154. }
    155.  
    Some things to consider...

    Should ReportScore or ReportProgress fail, you'll need to hold on to the request for later resubmission. You'll likely want to write data to the application's Documents directory at some regular time interval and OnApplicationPause(), and read it back at Start().

    You probably won't need to LoadAchievements, unless you want to use your own leaderboard UI. You could potentially use the data there to keep track of the player's current progress, but it could be out of date due to a prior failure of ReportProgress.

    LoadAchievementDescriptions likely depends on your design. If you want, for example, to alert a player that they've completed an achievement, the descriptions would be handy to have.
     
  11. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Thanks gangrel.

    I'm having no luck with ReportScore - it doesn't return an error, but it also doesn't appear to submit the score.

    Connecting, displaying the Leaderboard, etc is all working fine. It's just that the Leaderboard is always empty. Can a player see their own score? Maybe I need to test with more than one account?

    Has anyone successfully got this all working including reporting scores?
     
  12. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Has anyone played around with this??? Still can't get it to record scores that I send through.
     
  13. luisanton

    luisanton

    Joined:
    Aug 25, 2009
    Posts:
    325
    This is (the Social API, much more for Game Center), for me, one of the most interesting additions to Unity, but it's still... obscure! Please, Unity People, update the documentation, write a tutorial or something :D

    I mean, even the example in the documentation throws errors... Am I missing something or is it that it's just too new?
     
    Last edited: Feb 21, 2012
  14. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    If you look at the previous page you would see an example and also a link to another example.
    It seems to work, but I am having the same problem as geppeto. The score that I submitted, doesn't show up, even though the handle returns true. If I login from another account, I still don't see the scores that I submitted. From what I read (in this thread) it might take 24hours for scores to appear, so hopefully it will show up soon...
     
  15. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    I've waited more than 24 hours... nothing ever gets added. Definitely something is not working quite right...
     
  16. bug5532

    bug5532

    Joined:
    Aug 16, 2011
    Posts:
    307
    Have you tried changing gamecenter accounts? that made it appear for me :)
     
  17. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    I tried different accounts and still no scores. It's been over 24 hours as well.
     
  18. gangrel

    gangrel

    Joined:
    Jan 3, 2012
    Posts:
    28
    I forgot to mention, in my prior post, that while I've seen achievements update, I haven't seen the leaderboard update. I'll write a Cocoa test for this, to verify scores work that way.
     
  19. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Pity you can't just view a record count or something through iTunesConnect. I've still got nothing though - submitted scores appear to just go into a black hole. I'll update if I get anything to work. Would love to hear from anyone else that solves it :)
     
  20. quitebuttery

    quitebuttery

    Joined:
    Mar 12, 2011
    Posts:
    327
    Any update? I'm actually getting errors from ISocials' report score. Something tells me it's broken. But maybe my iTunes Connect settings are off.
     
  21. gangrel

    gangrel

    Joined:
    Jan 3, 2012
    Posts:
    28
    I haven't yet had a chance to write a native test for this, but should be able to get to it today.
     
  22. gangrel

    gangrel

    Joined:
    Jan 3, 2012
    Posts:
    28
    Here's what I found.

    Social.ReportScore() works just fine. The issue is that Game Center doesn't appear to show leaderboard content for the localUser if the localUser is the only player with an entry in the leaderboard.

    If you test with Player1, set a score, and view the leaderboard, you won't see Player1.
    But if you then test with Player2, and view the leaderboard, you'll see Player1 but not Player2.
    If you then set a score with Player2, and view the leaderboard again, you'll see both Player1 and Player2.
     
  23. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    So the problem appears only, if there is just one dude in the leaderboard, who tries to see himself?

    Still not sure if i should go with Unity 3.5 or with other solutions (Prime etc.) for my ios game, which i want to release in the next few days.
     
  24. andererandre

    andererandre

    Joined:
    Dec 17, 2010
    Posts:
    683
    Is it possible to create a cumulative leaderboard to track the total lifetime score of a player? I can't find anything in iTunes Connect and I can't find a method in the Unity Social API to retrieve the current personal best from Game Center.
     
  25. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Just in case people missed the same thing I did, I thought I'd just post a quick follow-up.

    Mine's all working now - the missing element was an activation within iTunesConnect that I'd missed previously. Once you've created your app (in iTunesConnect), there is this Game Center option that needs to be enabled within the "Current Version" details. This is in addition to the more obvious "Manage Game Center" option that appears under the main "App Information" section.



    Maybe you guys didn't miss this, but just mentioning it in case...

    Also, you definitely can't see your own scores. I set a high score as myself, then sign out and back in with my wife's account (all on the Sandbox), and I can see my scores without any issues now.

    So no issues in Unity for me - it looks like all my problems were just caused by missing that step in iTunesConnect.
     
  26. Akta

    Akta

    Joined:
    Oct 15, 2010
    Posts:
    119
    Edit: my bad, I figured out the crash as soon as I posted, not really related to GC, sorry :)
     
    Last edited: Mar 11, 2012
  27. Derell Nar

    Derell Nar

    Joined:
    Jan 25, 2011
    Posts:
    10
    First of all.
    Thank you everyone, with your threads, I've been able to implement most of the GC functionalities.

    I do have one question:
    Every time I authenticate the localUser, it tries ot login to the ***Sandbox***.
    Is there a way to login to the live Game Center?
     
  28. gangrel

    gangrel

    Joined:
    Jan 3, 2012
    Posts:
    28
    Only a signed distribution build will log into Game Center. Simulator, developer, and ad-hoc distribution builds log into the sandbox. Which are you using?
     
  29. Derell Nar

    Derell Nar

    Joined:
    Jan 25, 2011
    Posts:
    10
    Ah, I figured it would be something like that...
    I'm using a dev build....

    Thank you gangrel!
     
    Last edited: Mar 19, 2012
  30. Jayanta

    Jayanta

    Joined:
    Sep 20, 2011
    Posts:
    9
    Hello Everybody I am unable to submit the High Score in game center using Prime31 Game Center Plugins in Unity3d. Here I had set my Leader board id 10 in the Game Center. Here is my code.

    GameCenterBinding.authenticateLocalPlayer();
    GameCenterBinding.loadLeaderboardTitles();

    if( leaderboards != null leaderboards.Count > 0 )
    {

    GameCenterBinding.reportScore( Random.Range( 1, 9999999 ), leaderboards[10].leaderboardId );


    }
    It's able to authenticate the Player but when I post the score then it display "No Scores" on the Leaderboard.
     
  31. Jayanta

    Jayanta

    Joined:
    Sep 20, 2011
    Posts:
    9
    I had short it out the bug.

    Actually I was use wrong Leader board ID in report score. Though our leader board ID is 10(total leader board in my developer panel) in Game center we have to use index number for particular Game like 0,1,2 etc. Because it's depend on numbers of leader board assign for the Game. Here One Leader board in my game so I am using

    if( leaderboards != null leaderboards.Count > 0 )
    {

    GameCenterBinding.reportScore( Random.Range( 1, 9999999 ), leaderboards[0].leaderboardId );


    }
    now it's working fine.. Thanks Prime31
     
  32. Patapouffe

    Patapouffe

    Joined:
    Apr 3, 2010
    Posts:
    88
    Game Center is working and so are the achievements but how do you make the achievement pop-up in-game to tell the player they got it ?

    Thanks.
     
  33. Derell Nar

    Derell Nar

    Joined:
    Jan 25, 2011
    Posts:
    10
    You have to use the following function call:

    Code (csharp):
    1. GameCenterPlatform.ShowDefaultAchievementCompletionBanner(true);
    http://unity3d.com/support/document...m.ShowDefaultAchievementCompletionBanner.html

    You just need to call it once, in the Start for example.
    But this feature is only available for iOS5 or Higher
     
    Last edited: Mar 27, 2012
  34. Patapouffe

    Patapouffe

    Joined:
    Apr 3, 2010
    Posts:
    88
    Great ! Thanks a lot Derell for the quick reply !
     
  35. Razieln64

    Razieln64

    Joined:
    May 3, 2008
    Posts:
    129
    This code works fine, but when I send the app in the background and bring it back again, it becomes broken and doesn't show the banner.

    Code (csharp):
    1. GameCenterPlatform.ShowDefaultAchievementCompletionBanner(true);
    I've also tried starting the app with achievements at 10% and completed them 100% but never got the banner to show. It only works when you start with an achievement at 0% and get to 100% in one session.
     
    Last edited: Mar 27, 2012
  36. Le_nain

    Le_nain

    Joined:
    Jun 20, 2011
    Posts:
    47
    Hi,

    I'm currently implementing the Game Center for my app, and I'm running into a problem:

    At Authentication time, I call Social.localUser.Authenticate(GCAuthenticationHandler); and it works like a charm => I get my AlertView showing and prmpting me to login.
    If I do login, everything is fine => user gets recognized and I can report/display scores.
    Though if I press the CANCEL button at login time, here comes the problem => it does cancel the login as it should, but from that point forward I can never login again (by calling Social.localUser.Authenticate(GCAuthenticationHandler); again) unless the app is killed. Unless I kill my app, all I get is Failed to authenticate local user The requested operation could not be completed because local player is already authenticating.
    It seems like Unity doesn't terminate the process of authentication if the user cancels, and there is no way for me to manually "reboot" this process.

    This is very bad and definitely not user-friendly to have to kill your app in case you didn't want to login to GC at the first place, or even if you did hit the cancel button by mistake...

    If anyone encoutered this problem, or know anything about this, please post. Otherwise I guess I'll have to file a bug to them.

    Thanks
     
  37. Ethan

    Ethan

    Joined:
    Jan 2, 2008
    Posts:
    501
    My app's performance drops drastically if i changed from a w-lan connection place to a connectionless place. After starting the game in the connectionless place i get this dialog like "Could not connect to GameCenter" with the options "retry" and "cancel".
    After i press "cancel", because i do not have a connection my game, which is in the next loaded scene, lags horribly.
    If i quit the game and reopen it (still without connection to the internet or anything), it doesn't open this small dialog and the game works fine again (of course GameCenter is unable to load, but the game works).

    In short:
    If the GameCenter "could not connect" dialog with the "retry" and "cancel" pops up, evil things happen which bust your FPS.

    Did anyone experience that?
     
  38. gprogrammer

    gprogrammer

    Joined:
    Apr 12, 2012
    Posts:
    3
    I am also facing the same problem. In my case even if I kill the application and re-deploy through xcode, it is not allowing me to re-login. I tried many times but it doesn't show the authentication popup again.
    My GC was running in sandbox mode.

    Is Unity team planning to fix this ?
     
  39. Le_nain

    Le_nain

    Joined:
    Jun 20, 2011
    Posts:
    47
    It's weird that even if you kill/redeploy the bug still happens.
    Any chance you did cancel the logging several times (3 times in a row if I recall correctly) without logging in correctly? Because this is part of the process of the GC to disable completely the loggin from within apps if the user cancelled it 3 times in a row; he then has to launch the GC app and connect from within it. Maybe try that, it might hopefully "reactivate" your login prompt?
     
  40. gprogrammer

    gprogrammer

    Joined:
    Apr 12, 2012
    Posts:
    3
    i am not able to get the login window up again. After calling Authenticate on localUser, it just simply throws error as "Authentication Failed"

    UPDATE: Got it finally after re-logging in from GC app.
     
    Last edited: Apr 15, 2012
  41. hieu.ivieoh

    hieu.ivieoh

    Joined:
    Dec 16, 2010
    Posts:
    12
    I using Social to connect GC, i success to reach the sandbox banner, load and see the leaderboard, but i can't see the change of leaderboard in spite of i have submitted the score by using
    Code (csharp):
    1. Social.LoadScores(LeaderboardID, scores => {
    2.         if (scores.Length > 0) {
    3.             Debug.Log ("Got " + scores.Length + " scores");
    4.             string myScores = "Leaderboard:\n";
    5.             foreach (IScore score in scores)
    6.                 myScores += "\t" + score.userID + " " + score.formattedValue + " " + score.date + "\n";
    7.             Debug.Log (myScores);
    8.         }
    9.         else
    10.             Debug.Log ("No scores loaded");
    11.     });
    i got the bug in Xcode and can't continue. So if i clear all debug.Log, that is ok, but nothing change with the Leaderboard. Any help please !
     
  42. Le_nain

    Le_nain

    Joined:
    Jun 20, 2011
    Posts:
    47
    For the leaderboard to display the scores, you need to have at least 2 players posting scores, so you need to create a 2nd test account, login with it and report a score as well with that 2nd account.
     
  43. gprogrammer

    gprogrammer

    Joined:
    Apr 12, 2012
    Posts:
    3
    I was able to post the scores with single login only. The only thing was my scores started showing up after a day. I don't know why it took so long for the servers to show the updated values. So you may need to check back leaderboard after some time for updates.
     
  44. hieu.ivieoh

    hieu.ivieoh

    Joined:
    Dec 16, 2010
    Posts:
    12
    i used
    Code (csharp):
    1. void ReportScore (long score, string leaderboardID)
    2.     {
    3.         Debug.Log ("Reporting score " + score + " on leaderboard " + leaderboardID);
    4.         Social.ReportScore(score, LeaderboardID, success => {
    5.             Debug.Log(success ? "Reported score successfully" : "Failed to report score");
    6.         });
    7.        
    8.     }  
    to report my score, but it's nothing change when i call the ReportScore function by Click the button. The variable "score" will be change when i click the button( score += 100).But when i sign in with other Account. I load the LeaderBoard or press the ReportScore Button, or LoadScore. Nothing change with the 1 score is 100 Points.
    Any Help Pls!
     
  45. Eirikir

    Eirikir

    Joined:
    Mar 6, 2011
    Posts:
    11
    I have set up the authentication and can log in properly but how do I report an achievement when one is earned in the game?
     
  46. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    How do you detect when the user closes the gamecenter ui using the apple "done" button?
     
  47. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    I would LOVE to know this too. I have a feeling the answer is going to be "You can't" though :(

    Would love to see that event implemented in future if it's not currently. I'm hiding the ad banner, for example, when the leaderboard/achievements are shown, but have no way to know when to put it back.
     
  48. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    In addition some of us may need to perform operations like unpausing our game :p If it's not in, it needs to be put in.
     
  49. echologin

    echologin

    Joined:
    Apr 11, 2010
    Posts:
    1,078
    Anyone having problems getting the gamecenter UI windows to show up at correct orientation ?

    UPDATE: it works fine on an ip4 but problem happens on my ipad1 it also corrects the orientation
    if you shake the ipad
     
    Last edited: Apr 19, 2012
  50. Eirikir

    Eirikir

    Joined:
    Mar 6, 2011
    Posts:
    11
    Do you have to first load scores in order to be able to report to leaderboards, in sandbox mode?

    EDIT: had to wait til the next day, everything is working now. Strange with this delay though.
     
    Last edited: Apr 20, 2012