Hello! I have a problem. I am a beginner in creating games, and the game I am working on now has a timer, which goes until you finish the game. The best time is saved. I took a script from the internet with leaderboard, but I want the best time to be put on the leaderboard, and I don’t know exactly how …
Can you help me,please?
(TIMER SCRIPT)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIController : MonoBehaviour
{
public Text timerS;
public Text timer;
private float time;
private float msec;
private float sec;
private float min;
public void Start()
{
timerS.text = PlayerPrefs.GetFloat("BestTime").ToString();
}
public void WatchStart()
{
StartCoroutine("StopWatch");
}
private void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Finish20")
{
Debug.Log("TIMER STOP");
StopCoroutine("StopWatch");
if (time > PlayerPrefs.GetFloat("BestTime"))
{ PlayerPrefs.SetFloat("BestTime", time); }
if (time < PlayerPrefs.GetFloat("BestTime"))
{ PlayerPrefs.SetFloat("BestTime", time); }
}
}
IEnumerator StopWatch()
{
while (true)
{
time += Time.deltaTime;
msec = (int)((time - (int)time) * 100);
sec = (int)(time % 60);
min = (int)(time / 60 % 60);
timer.text = string.Format("{0:00}:{1:00}:{2:00}", min, sec, msec);
yield return null;
}
}
}
(GOOGLE PLAY SCRIPT)
using UnityEngine;
using System;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
public class PlayServices : MonoBehaviour
{
public int playerScore;
string leaderboardID = "";
string achievementID = "";
void Start()
{
DontDestroyOnLoad(this);
try
{
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.Activate();
Social.localUser.Authenticate((bool success) => { });
}
catch (Exception exception)
{
Debug.Log(exception);
}
}
public void AddScoreToLeaderboard()
{
if (Social.localUser.authenticated)
{
Social.ReportScore(playerScore, "", success => { });
}
}
public void ShowLeaderboard()
{
if (Social.localUser.authenticated)
{
Social.ShowLeaderboardUI();
}
}
public void ShowAchievements()
{
if (Social.localUser.authenticated)
{
Social.ShowAchievementsUI();
}
}
public void UnlockAchievement()
{
if (Social.localUser.authenticated)
{
Social.ReportProgress(achievementID, 100f, success => { });
}
}
}