Hi Jeremy here, and I have my script (script below) that activates gamecenter and adds the game to gamecenter successfully and all that but I have not been able to figure out how to track the score of of my player and make it show up on the leaderboards in gamecenter? Its a simple high score based game like flappy bird but I can’t find any solid source on how to do it? Any help is greatly appreciated!
/// TnT_GameCenter
///
/// all gamecenter related sourcecode and tools
/// </summary>
public class TnT_GameCenter : MonoBehaviour
{
// -------------------------------
// parameter
// -------------------------------
// -------------------------------
// private members
// -------------------------------
private bool isLoggingIn = false; // true if logging in, false while not
private bool loggedIn = false; // stores if the user is logged into the game center
// -------------------------------
// standard monobehaviour
// -------------------------------
/// <summary>
/// Start this instance
/// starts login into gamecenter if not disabled
/// </summary>
void Start ()
{
isLoggedIn ();
login();
GameCenterPlatform.ShowDefaultAchievementCompletionBanner(true);
}
// -------------------------------
// public api
// -------------------------------
/// <summary>
/// Login this user
/// </summary>
public void login( )
{
// login only if gamecenter is enabled
if ( PlayerPrefs.GetInt ( "GameCenter", 1 ) == 0 )
return;
// now log in
Debug.Log ( "Logging into Gamecenter" );
if ( loggedIn == true )
return;
if ( isLoggingIn == true )
return;
isLoggingIn = true;
Social.localUser.Authenticate (ProcessAuthentication);
}
/// <summary>
/// checks if user is logged in
/// </summary>
/// <returns>
/// Ttrue if logged in
/// </returns>
public bool isLoggedIn ( )
{
Debug.Log ( "USER : " + Social.localUser );
return loggedIn;
}
/// <summary>
/// Saves the game score at the leaderboard of gamecenter
/// </summary>
/// <param name='ship'>
/// Ship to save
/// </param>
/// <param name='score'>
/// Score to save
/// </param>
public void saveGameScore ( string ship, int score )
{
// only if gamecenter is enabled
if ( !isLoggedIn () )
return;
// report global score
Social.ReportScore ( score, "grp.Best", ScoreSubmitted );
// report ship based score
if ( ship == "SpaceCab" )
{
Social.ReportScore ( score, "grp.SpaceCab", ScoreSubmitted );
return;
}
if ( ship == "BA75" )
{
Social.ReportScore ( score, "grp.BA75", ScoreSubmitted );
return;
}
if ( ship == "DartX" )
{
Social.ReportScore ( score, "grp.DartX", ScoreSubmitted );
return;
}
if ( ship == "SR1" )
{
Social.ReportScore ( score, "grp.NovaSR1", ScoreSubmitted );
return;
}
if ( ship == "CobraMKIII" )
{
Social.ReportScore ( score, "grp.CobraMKIII", ScoreSubmitted );
return;
}
if ( ship == "DMC" )
{
Social.ReportScore ( score, "grp.DMC", ScoreSubmitted );
return;
}
Debug.LogError ( "Unknown ship " + ship );
}
/// <summary>
/// Reports the progress of an achievement
/// </summary>
public void reportProgress ( string achievementId, double progress )
{
Debug.Log ( "REPORTING ACHIEVEMENT : " + achievementId );
if ( !isLoggedIn () )
return;
Social.ReportProgress ( achievementId, progress, AchievementSubmitted );
}
// -------------------------------
// internal tool functions
// -------------------------------
/// <summary>
/// called when the gamecenter login is finished
/// </summary>
/// <param name='success'>
/// tells if the authentication was successfull
/// </param>
void ProcessAuthentication (bool success)
{
isLoggingIn = false;
if (success)
{
Debug.Log ("Authenticated, checking achievements");
this.loggedIn = true;
Social.LoadAchievements ( LoadedAchievements );
}
else
{
Debug.Log ("Failed to authenticate");
this.loggedIn = false;
}
isLoggedIn();
}
/// <summary>
/// callback for reportScore
/// </summary>
/// <param name='success'>
/// Success true if submission of score succeeded
/// </param>
void ScoreSubmitted ( bool success )
{
if ( !success )
Debug.LogWarning ( "Failed to submit score" );
else
Debug.Log ( "submitted score succesfull" );
}
/// <summary>
/// callback for reportScore
/// </summary>
void AchievementSubmitted ( bool success )
{
if ( !success )
Debug.LogWarning ( "Failed to submit achievement" );
else
Debug.Log ( "Submitted achievement successfull" );
}
/// <summary>
/// callback for achievements loaded
/// </summary>
void LoadedAchievements ( IAchievement[] achievements )
{
if (achievements.Length == 0)
Debug.Log ( "No Achievments found" );
else
Debug.Log ("Got " + achievements.Length + " achievements");
}
}