Created
July 6, 2016 04:39
-
-
Save kinifi/cb304a1c4556d811b36d29c2a7fb54ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using Steamworks; | |
public class SubmitToLeaderboard : MonoBehaviour { | |
//the enum to select | |
public Leaderboard mLeaderboardToSubmit; | |
// Default to Global Leaderboards. | |
private ELeaderboardDataRequest m_eRequestType = ELeaderboardDataRequest.k_ELeaderboardDataRequestGlobal; | |
private int m_numLeaderboards; | |
private LeaderboardEntry_t[] m_Entries; | |
private void OnEnable() | |
{ | |
SteamManager.Leaderboards.SetCallbacks(OnLeaderboardDownloaded, OnScoreUploaded); | |
StateChanged(); | |
//load the number of Leaderboards we have | |
m_numLeaderboards = SteamLeaderboards.k_nNumLeaderboards; | |
Debug.Log("Number of Leaderboards: " + m_numLeaderboards); | |
} | |
private void OnDisable() | |
{ | |
SteamManager.Leaderboards.SetCallbacks(); | |
} | |
public bool isSteamInit() | |
{ | |
if (!SteamManager.Initialized) | |
{ | |
return false; | |
} | |
else | |
{ | |
return true; | |
} | |
} | |
/// <summary> | |
/// Upload a Score to a leaderboard | |
/// </summary> | |
/// <param name="board">The Leaderboard to upload to</param> | |
/// <param name="score">Score To Upload</param> | |
public void UploadScore(int board, int score) | |
{ | |
SteamManager.Leaderboards.UploadScore(board, score); | |
Debug.Log("Submitted to Leaderboard: " + board + "| Score: " + score); | |
} | |
// Callbacks from Steam, registered in OnEnable | |
private void OnLeaderboardDownloaded(LeaderboardEntry_t[] entries) | |
{ | |
m_Entries = entries; | |
} | |
private void OnScoreUploaded(Leaderboard mLeaderboardToSubmit) | |
{ | |
Debug.Log("Score Uploaded to: " + mLeaderboardToSubmit); | |
// Refresh the leaderboard | |
SteamManager.Leaderboards.GetLeaderboard(mLeaderboardToSubmit, m_eRequestType); | |
} | |
// The Leaderboard display state changed, redownload the leaderboard. | |
private void StateChanged() | |
{ | |
m_Entries = null; | |
SteamManager.Leaderboards.GetLeaderboard(mLeaderboardToSubmit, m_eRequestType); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment