Friday, May 8, 2015

Unity Step by Step How to setup Game Center for IOS

Hello,

So I just learned how to setup Game Center for IOS in Unity I will show you how

Step1: Open https://itunesconnect.apple.com

Step2: Create new app

Step3: Under the "Game Center" tab, add a new leaderboard, give a leaderboard ID and leaderboard name, example leaderboard ID "mygame"

Step4: Open Unity

Step5: Create a new C# script "game.cs" and attach to the camera

Step6: put the following code into your script


//////////inside game.cs/////////////////////////////////

using UnityEngine.SocialPlatforms;    
public class game : MonoBehaviour 
{

    int score;

    void Start () 
    {

         score=3;
         Social.localUser.Authenticate (ProcessAuthentication);

    }

    void ProcessAuthentication (bool success
    {
    

        if (success
        {
            Debug.Log ("Authentication successful");
            Social.CreateLeaderboard();
            Social.CreateLeaderboard().id = "mygame";

        }
        else
        {
            Debug.Log ("Failed to authenticate");
        }
    }



    void OnGUI() 
    {

            if(GUI.Button (new Rect (480f,1500f,300f,300f), "<color=white><size=80>SUBMIT</size></color>"))
            {
                Social.ReportScore(score,"mygame",HighScoreCheck);


            }


            if(GUI.Button (new Rect (950f,1500f,300f,300f), "<color=white><size=40>STAT</size></color>"))
            {
                Social.ShowLeaderboardUI();


            }


    }


    static void HighScoreCheck(bool result
    {
        if(result)
            Debug.Log("score submission successful");
        else
            Debug.Log("score submission failed");
    }

}
/////////////////////////////////////////////////

Code Explanation:
1. Under the Start() function, you put Social.localUser.Authenticate (ProcessAuthentication);

- This mean you try to login the user to the game center

2. void ProcessAuthentication (bool success

- This is the function that process the user login, after user login successfully, you create a leaderboard with id "mygame", must match the ID you created on step 3

3. void OnGUI() 
    
- This is the function that show 2 button, button 1 "Submit" let you submit your highscore to the leaderboard, button 2 "Stat" will load the game center showing your highscore

4. static void HighScoreCheck(bool result


- This is the function that tell you if the score submission is successful or not

//////////////////////////////////////////////////////////
Step7: You can run in Unity to test but the "Game Center" leaderboard screen will not appear in Unity

Step8: Build your Unity Project into IOS Xcode

Step9: Open your IOS Xcode in Xcode

Step10: Under XCode, On Capabilities Tab, turn the GameCenter "ON"

Step11: Plug your Iphone to your Mac, and run the XCode program into your Iphone to test it

Step12: It might take a while before the highscore showing, for my case, when i click the "submit" then "stat" , the Game Center leaderboard opened and showed "No Item", after 3 hour, I open my program again and click "stat" I can see my highscore showing on the Game Center Leaderboard

There you go, hopefully it can help you in some way =)



No comments:

Post a Comment