Show Leaderboard Score Directly - Google Play Service - android

I want to show leaderboard score directly as player press on leaderboard icon as like this:
When I was showing a leaderboard it just displaying, list of leaderboard. I want to see actual score directly.
Here is my code:
public void ShowLeaderboard ()
{
if (Social.localUser.authenticated)
Social.ShowLeaderboardUI ();
else {
// authenticate user:
Social.localUser.Authenticate ((bool success) => {
// handle success or failure
if (success) {
Debug.Log ("Login Success....");
PostHighScoreOnLeaderBoard();
Social.ShowLeaderboardUI ();
} else {
Debug.Log ("Login Failed....");
}
});
}
}
How can I show directly a players score?

If you wish to show a particular leaderboard instead of all leaderboards, you can pass a leaderboard ID to the method. This, however, is a Play Games extension, so the Social.Active object needs to be cast to a PlayGamesPlatform object first:
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
...
// show leaderboard UI
PlayGamesPlatform.Instance.ShowLeaderboardUI("Cfji293fjsie_QA");

If you would like the show the user's score directly like in your first image, you need to call PlayGamesPlatform.Instance.ShowLeaderboardUI("leaderboard_id") with the leaderboard ID passed in instead of Social.ShowLeaderboardUI().

Related

Sharing to Facebook shows error "We are Sorry, this post is no longer available. It may have been removed"

I'm trying to implement Facebook sharing in my game using Unity 3D + Facebook Unity SDK. But when I tried testing to post to my wall, this error shows up: "We are Sorry, this post is no longer available. It may have been removed." Can anybody help me? Thanks in advance.
BTW, here's my code:
using UnityEngine;
using System.Collections;
public class FacebookController : MonoBehaviour {
public bool isUsingFacebook = true; //Are we using Facebook SDK? This variable is only
//Feed parameters.
private string link = "market://details?id=com.LaserCookie.Queue"; //The link that will show the user the game's google play store address
private string linkName = "Queue"; //The link name
private string linkCaption = "Wow this game is great! 10/10 must play!"; // The caption of the link
private string linkDescription = "I achieved the score of " + PlayerController.instance.score.ToString() + "! Try to beat me if you can!"; //The description of the link
private string picture = "http://www.drycreekequestriancenter.com/testImage.jpeg"; //This is the image / game icon for the link. For now, it's shamelessly got from a random source. Thank you, random citizen...
void Awake()
{
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
//Init FB
private void Init()
{
if (!FB.IsInitialized)
{
FB.Init(OnInitComplete, OnHideUnity);
}
}
//Callback that will be called when the initialization is completed
private void OnInitComplete()
{
Debug.Log("FB.Init completed: Is user logged in? " + FB.IsLoggedIn);
//Check if we are logged in.
//If not, we will log in.
//If we are, post status.
if (!FB.IsLoggedIn)
{
LoginWithPublish();
}
else
{
PostImpl();
}
}
//Callback that will be called when the game is shown / not
private void OnHideUnity(bool isGameShown)
{
Debug.Log("Is game showing? " + isGameShown);
}
//Post to Facebook. This is the only exposed method because we only use this to post to Facebook.
//The initialization and login will be called when needed.
//It will first detect whether or not we have been initialized. And will init if we haven't.
//Then it will check whether or not we have been logged in with publish. And will log in if not.
public void PostToFacebook()
{
//Are we using facebook SDK?
if (isUsingFacebook)
{
if (!FB.IsInitialized) //Check for initialization
{
Init();
}
else if (!FB.IsLoggedIn) //Check for login
{
LoginWithPublish();
}
else //Post if we are already initia;ized and logged in
{
PostImpl();
}
}
}
//The true implementation of the posting
private void PostImpl()
{
FB.Feed("",link, linkName,linkCaption,linkDescription,picture);
}
//Login to Facebook with publish
private void LoginWithPublish()
{
// It is generally good behavior to split asking for read and publish
// permissions rather than ask for them all at once.
//
// In your own game, consider postponing this call until the moment
// you actually need it.
FB.Login("publish_actions", LoginCallback);
}
//Login callback
void LoginCallback(FBResult result)
{
if (result.Error != null)
{
Debug.Log( "Error Response:\n" + result.Error );
//TODO: Retry login if we have error? Or do we display a pop up?
}
else if (!FB.IsLoggedIn)
{
Debug.Log( "Login cancelled by Player" );
//TODO: Do we display a pop up?
}
else
{
Debug.Log( "Login was successful!" );
PostImpl();
}
}
}
You need to add Key Hash for FB application.
Go to My Apps, select you application, open Setting tab, add platform for android, and add you key hash.
check this link out
Setting a Release Key Hash
I've fixed the issue. It turns out it's because I used my still in development google store address as the link. I thought it would be automatically recognized regardless of my app is live or not. Thank you anyway. :)

Waiting for Play Games to connect before displaying leaderboard

I'm not keen on the standard behavior of the Play Games Service to automatically attempt a connection when the app is first launched, so I have disabled this. In my main menu I have a 'display scores' button. What I want to happen when the user presses this button is this:
If the user is connected (logged in), go ahead and display the leaderboard
If the user is not connected, then display the connection dialog. Once connected, display the leaderboard
On the main menu, I will have an extra button "Log out" which will display only if the user is connected / logged in.
When the user clicks the button, I am carrying out the following:
Code
if (buttonPressed()){
//Display connection dialogue and initiate log in
getGameHelper().beginUserInitiatedSignIn();
//Check if the user is signed in before continuing
if (getGameHelper.isSignedIn()){
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(), myLeaderBoardID), 1);
}
}
If the user isn't connected: The user is presented with a connection dialogue - this works fine. They can then log in. Once they have done this, nothing else happens (the code has moved on and therefore does not display the leaderboard because the user isn't logged in - if I don't have the check to see if the user is signed in here the app would just crash). If the user then presses the button again, it will display the leaderboard.
How can I do all this with just one button press?
What I want is, if the user isn't logged in, to display the log-in dialogue, then as soon as the user has logged in, display the leaderboard. I need to make startActivityForResult wait until the user has completed sign in.
In short
I need to make my code wait until it's connected to Play before attempting to display the Leaderboard.
Any help would be appreciated
You can be notified of successful/failed sign-in as follows:
getGameHelper().setup(
new GameHelper.GameHelperListener() {
#Override
public void onSignInSucceeded() {
// execute code on successful sign-in
// for example, here you could show your leaderboard
}
#Override
public void onSignInFailed() {
// execute code on failed sign-in
}
};
);
You should of course do this before you attempt to sign-in. You can then show your leaderboard when sign-in succeeds. This code should be placed where you create your game helper (i.e. before the buttonPressed() code is executed).
Once this code is in place, you should change your buttonPressed() code to look as follows:
if ( buttonPressed() ) {
// check if user already signed-in and show leaderboard; otherwise do sign-in
if ( getGameHelper.isSignedIn() ) {
startActivityForResult( Games.Leaderboards.getLeaderboardIntent( getApiClient(), myLeaderBoardID ), 1 );
}
else {
getGameHelper().beginUserInitiatedSignIn();
// NOTE: do nothing further here; show the leaderboard in
// the listener's onSignInSucceeded()
}
}
One final note: the listener you create will be called for all sign-in operations, so if you need to have this functionality in multiple places (for example, if you want to do the same with achievements) then you will need to use some signal as to what needs to happen on successful sign-in and take the correct action in onSignInSucceeded().
Signalling an action for sign-in success:
Add this code to you class (global scope)
public final static int NO_ACTION = 0;
public final static int SHOW_LEADERBOARD = 1;
public final static int SHOW_ACHIEVEMENTS = 2;
public int signInAction = NO_ACTION;
Next set the action just before signing-in (based on where the sign-in occurs):
if ( buttonPressed() ) {
// check if user already signed-in and show leaderboard; otherwise do sign-in
if ( getGameHelper.isSignedIn() ) {
startActivityForResult( Games.Leaderboards.getLeaderboardIntent( getApiClient(), myLeaderBoardID ), 1 );
}
else {
// NEW: request leaderboard to be shown upon sign in
signInAction = SHOW_LEADERBOARD;
// NEW----------------------------------------------
getGameHelper().beginUserInitiatedSignIn();
// NOTE: do nothing further here; show the leaderboard in
// the listener's onSignInSucceeded()
}
}
And finally change the listener to respond the set sign-in action:
getGameHelper().setup(
new GameHelper.GameHelperListener() {
#Override
public void onSignInSucceeded() {
if ( signInAction == SHOW_LEADERBOARD ) {
// show your leaderboard here
}
else if ( signInAction == SHOW_ACHIEVEMENTS ) {
// show achievements here
}
// important! reset the sign-in action so that any subsequent sign-in
// attempts do not re-use the currently set action!
signInAction = NO_ACTION;
}
#Override
public void onSignInFailed() {
// execute code on failed sign-in
// important! it should also be cleared in case of an error
signInAction = NO_ACTION;
}
};
);
Of course, this is just one way to achieve this, but it should work just fine for most purposes. Just be sure to set the signInAction to the appropriate value before you perform the sign-in - and be sure to clear it when sign-in is complete.

in-app billing with phonegap for android app

I am developing an android phonegap application and I want to use in app billing in it. I installed the phonegap billing plugin and it works perfectly. Can you help me make it work correct with a link.
For example, here is the script code:
<script >
function successHandler (result) {
var strResult = "";
if(typeof result === 'object') {
strResult = JSON.stringify(result);
} else {
strResult = result;
}
alert("SUCCESS: \r\n"+strResult );
}
function errorHandler (error) {
alert("ERROR: \r\n"+error );
}
// Click on init button
function init(){
// Initialize the billing plugin
inappbilling.init(successHandler, errorHandler, {showLog:true});
}
// Click on purchase button
function buy(){
// make the purchase
inappbilling.buy(successHandler, errorHandler, "good_id");
}
// Click on ownedProducts button
function ownedProducts(){
// Initialize the billing plugin
inappbilling.getPurchases(successHandler, errorHandler);
}
// Click on Consume purchase button
function consumePurchase(){
inappbilling.consumePurchase(successHandler, errorHandler, "good_id");
}
// Click on subscribe button
function subscribe(){
// make the purchase
inappbilling.subscribe(successHandler, errorHandler,"good_id");
}
// Click on Query Details button
function getDetails(){
// Query the store for the product details
inappbilling.getProductDetails(successHandler, errorHandler, "good_id");
}
// Click on Get Available Products button
function getAvailable(){
// Get the products available for purchase.
inappbilling.getAvailableProducts(successHandler, errorHandler);
}
</script>
And i need it to work with for example -
Good
Thats after clicking on a link first of all i ll be able to pay before linking to a page.
Thanks. Sorry for my bad English.
I'm not sure i understood what you want, your english is worst than mine. But if you just want to launch a purchase when clicking and after redirect, you may use onclick event and window.location.replace :
<a href="good.html" id='good' data-ignore="true" onclick="buy()" >Good</a>
// Click on purchase button
function buy(){
// make the purchase
inappbilling.buy(successHandler, errorHandler, "good_id");
return false;
}
and then add window.location.replace('yourlinkpage'); on your successHandler
But maybe an input submit should be more appropriate?

List all the leaderboards in Google play game services

In the documentation the code snippet to display leaderboard is
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(), LEADERBOARD_ID), REQUEST_LEADERBOARD);
This goes into the given leaderboard with LEADERBOARD_ID
My game has severel leaderboards and what I want to do is display a list of them so that user can select a specific leaderboard.
Is it possible to do that?
If you want to use the default UI, you can use this:
startActivityForResult(Games.Leaderboards.getAllLeaderboardsIntent(getApiClient()), REQUEST_LEADERBOARD);
This is the new way of doing it:
Games.getLeaderboardsClient(this, GoogleSignIn.getLastSignedInAccount(this)!!)
.allLeaderboardsIntent
.addOnSuccessListener { intent ->
startActivityForResult(
intent,
RC_LEADERBOARD_UI
)
}
companion object{
private const val RC_LEADERBOARD_UI = 9004
}

How to detect sign out via the Google Play Leaderboards UI?

Signing out or disconnecting the GamesClient is straightforward when it is from your own UI, such as a button on the main menu.
However, users can also sign out from the game from the Google Play UI in the acheivements and leaderboard views displayed by the intents such as getAllLeaderboardsIntent(). (It's a bit hidden, but if you tap the menu in the upper right, it lets you sign out.)
There are a few promising listener interfaces like OnSignOutCompleteListener but they don't seem to work with a sign out via the google UI, only from your own UI calling GamesClient.signOut().
How can I detect that the user has signed out from the leaderboards or achievement intents? Is it possible to have a callback for this?
I want to be able to update my in-game UI to reflect the logged-in status.
Unfortunately GameHelper doesn't detect when you logout from Google play games.
What you need to do is to put this in your onActivityResult() method in your activity.
I encounter a crash error when I tried using aHelper.signOut() when res == RESULT_RECONNECT_REQUIRED is true.
Instead I created a resetAllValues() method which resets back all values to its default in GameHelper.
In my MainActivity.java
protected void onActivityResult(int req, int res, Intent data) {
super.onActivityResult(req, res, data);
if (res == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED) {
aHelper.resetAllValues();
} else {
aHelper.onActivityResult(req, res, data);
}
}
My method in GameHelper.java
public void resetAllValues()
{
mProgressDialog = null;
mAutoSignIn = true;
mUserInitiatedSignIn = false;
mConnectionResult = null;
mSignInError = false;
mExpectingActivityResult = false;
mSignedIn = false;
mDebugLog = false;
}
Duplicate from:
How can i check if user sign's out from games services default view?
As I see it, there is no elegant solution to that. You can check the response_code in onActivityResult for INCONSISTENT_STATE and cut off the GamesClient, but I'm not sure, if you can potetially get to an inconsistent state in any other manner...

Categories

Resources