- Home /
Unity C# Highscore table
Hi all,
So first post, I'm not very good at all this and currently working on my first small project (Endless runner)
So I have the the players score save when they die using Player.pref and it iwll only save a score which is higher than the previous one.
What i want to do is make the score save into an array (maximum size of either 3 or 5) and then display those scores in order descending.
Can anyone help me out with this? Can post the current scripts when I get home later
Answer by Hellium · Nov 30, 2017 at 10:20 PM
While my solution may be a little bit "too much", at least, it works ! :D
First of all, create a script called HighScoreManager.cs
and put the following script in it :
using System.IO;
using UnityEngine;
[CreateAssetMenu(menuName = "ScriptableObjects/HighScoreManager", order = 1)]
public class HighScoreManager : ScriptableObject
{
private string filePath;
[SerializeField]
private string fileName = "highScores" ;
[SerializeField]
private int highScoresCount = 5;
[SerializeField]
private int[] highScores;
private string FilePath
{
get
{
if ( string.IsNullOrEmpty( filePath ) )
filePath = Path.Combine( Application.persistentDataPath, fileName );
return filePath;
}
}
public int HighScoresCount
{
get { return highScoresCount; }
}
public float this[int i]
{
get { return highScores[i]; }
}
public void SubmitHighScore( int highScore = 1000 )
{
if( highScores == null )
highScores = new int[HighScoresCount];
int newHighScoreIndex = -1;
for ( int i = 0 ; i < highScores.Length ; i++ )
{
if( highScore >= highScores[i] )
{
newHighScoreIndex = i;
break;
}
}
if( newHighScoreIndex >= 0 )
{
for ( int i = highScores.Length - 1 ; i > newHighScoreIndex ; i-- )
{
Debug.Log( highScores[i] );
Debug.Log( highScores[i-1] );
highScores[i] = highScores[i - 1];
}
highScores[newHighScoreIndex] = highScore;
}
}
public void Save()
{
File.WriteAllText( FilePath, JsonUtility.ToJson( this ) );
}
public void Load()
{
string path = Path.Combine( Application.persistentDataPath, "highScores" );
if ( File.Exists( FilePath ) )
{
JsonUtility.FromJsonOverwrite( File.ReadAllText( FilePath ), this );
}
if ( highScores.Length < highScoresCount )
highScores = new int[highScoresCount];
}
}
Then, in the Project
view, in Unity, right click and click on ScriptableObjects
> HighScoreManager
. You now have an object responsible for managing your highscores, and you will be able to submit a new highscore, save and load your highscores.
Here is a dummy script to test it :
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
public HighScoreManager highScoreManager ;
private void Start()
{
highScoreManager.Load();
for ( int i = 0 ; i < highScoreManager.HighScoresCount ; i++ )
{
Debug.Log( highScoreManager[i] );
}
}
private void Update()
{
if ( Input.GetKeyUp( KeyCode.Keypad1 ) )
highScoreManager.SubmitHighScore( 10 );
if ( Input.GetKeyUp( KeyCode.Keypad2 ) )
highScoreManager.SubmitHighScore( 20 );
if ( Input.GetKeyUp( KeyCode.Keypad3 ) )
highScoreManager.SubmitHighScore( 30 );
}
private void OnDestroy()
{
highScoreManager.Save();
}
}
Answer by text23d · Nov 30, 2017 at 08:00 PM
int[] scoreArray; //this would be array of your high scores
Your answer
Follow this Question
Related Questions
How to Clone Array1 into Array2 in Unity? Using JavaScript. 0 Answers
How can you do calculations on two lists? 1 Answer
Linear Interpolation of Array Values 1 Answer
Get all GameObjects by variable value 2 Answers
Help with for loop and arrays 3 Answers