Scoreboard implementation part 1
Get scores from server Submit score to server Basic scoreboard scene
This commit is contained in:
@@ -34,6 +34,8 @@ public class GameManager : MonoBehaviour
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
//dont destroy on load
|
||||
DontDestroyOnLoad(this.gameObject);
|
||||
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
|
||||
|
||||
StartWave();
|
||||
@@ -111,4 +113,9 @@ public class GameManager : MonoBehaviour
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
|
||||
}
|
||||
|
||||
public void OpenScoreboard()
|
||||
{
|
||||
SceneManager.LoadScene("Scoreboard");
|
||||
}
|
||||
}
|
||||
|
||||
28
Assets/Scripts/Core/MenuManager.cs
Normal file
28
Assets/Scripts/Core/MenuManager.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class MenuManager : MonoBehaviour
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void StartGame()
|
||||
{
|
||||
UnityEngine.SceneManagement.SceneManager.LoadScene("Main");
|
||||
}
|
||||
|
||||
public void QuitGame()
|
||||
{
|
||||
Application.Quit();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Core/MenuManager.cs.meta
Normal file
11
Assets/Scripts/Core/MenuManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 53ecff72f2724144d9a7cf7267fe1679
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
117
Assets/Scripts/Core/ScoreboardManager.cs
Normal file
117
Assets/Scripts/Core/ScoreboardManager.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ScoreboardManager : MonoBehaviour
|
||||
{
|
||||
public string username;
|
||||
public Button btnSubmit;
|
||||
public TMPro.TextMeshProUGUI currentScore;
|
||||
|
||||
[Header("Global Scores")]
|
||||
public TMPro.TextMeshProUGUI globalNames;
|
||||
public TMPro.TextMeshProUGUI globalScores;
|
||||
|
||||
private int playerScore = 0;
|
||||
private bool submitted = false;
|
||||
|
||||
[System.Serializable]
|
||||
public class Score
|
||||
{
|
||||
public string username;
|
||||
public int score;
|
||||
}
|
||||
[System.Serializable]
|
||||
public class Scores
|
||||
{
|
||||
public Score[] scores;
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
StartCoroutine(GetScores());
|
||||
//find gamemanager
|
||||
GameManager gm = GameObject.Find("GameManager").GetComponent<GameManager>();
|
||||
//set current score
|
||||
currentScore.text = gm.score.ToString();
|
||||
playerScore = gm.score;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
btnSubmit.interactable = username.Length > 0 && !submitted;
|
||||
}
|
||||
|
||||
public void UpdateUsername(string _username)
|
||||
{
|
||||
username = _username;
|
||||
}
|
||||
|
||||
public void SubmitScore()
|
||||
{
|
||||
btnSubmit.interactable = false;
|
||||
StartCoroutine(SendScoreToServer());
|
||||
}
|
||||
|
||||
IEnumerator SendScoreToServer()
|
||||
{
|
||||
string additional_data = "{}";
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("username", username);
|
||||
form.AddField("score", playerScore);
|
||||
form.AddField("additional_data", additional_data);
|
||||
using (UnityWebRequest www = UnityWebRequest.Post("http://vps5.minzkraut.com:3030/scores", form))
|
||||
{
|
||||
yield return www.SendWebRequest();
|
||||
|
||||
if (www.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
Debug.Log(www.error);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Score submitted");
|
||||
btnSubmit.GetComponentInChildren<TMPro.TextMeshProUGUI>().text = "Submitted!";
|
||||
submitted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator GetScores()
|
||||
{
|
||||
using (UnityWebRequest webRequest = UnityWebRequest.Get("http://vps5.minzkraut.com:3030/scores"))
|
||||
{
|
||||
// Request and wait for the desired page.
|
||||
yield return webRequest.SendWebRequest();
|
||||
|
||||
|
||||
|
||||
switch (webRequest.result)
|
||||
{
|
||||
case UnityWebRequest.Result.ConnectionError:
|
||||
case UnityWebRequest.Result.DataProcessingError:
|
||||
Debug.LogError("Error: " + webRequest.error);
|
||||
break;
|
||||
case UnityWebRequest.Result.ProtocolError:
|
||||
Debug.LogError("HTTP Error: " + webRequest.error);
|
||||
break;
|
||||
case UnityWebRequest.Result.Success:
|
||||
Debug.Log("Received: " + webRequest.downloadHandler.text);
|
||||
Scores scores = JsonUtility.FromJson<Scores>("{\"scores\":" + webRequest.downloadHandler.text + "}");
|
||||
globalNames.text = "";
|
||||
globalScores.text = "";
|
||||
foreach (Score score in scores.scores)
|
||||
{
|
||||
globalNames.text += score.username + "\n";
|
||||
globalScores.text += score.score + "\n";
|
||||
Debug.Log(score.username + ": " + score.score);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Core/ScoreboardManager.cs.meta
Normal file
11
Assets/Scripts/Core/ScoreboardManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2aa5c6310c7a2b145b3c495581360ebc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -12,6 +12,7 @@ public class UIManager : MonoBehaviour
|
||||
public TextMeshProUGUI waveLabel;
|
||||
|
||||
public Button restartButton;
|
||||
public Button scoreboardButton;
|
||||
private GameManager gameManager;
|
||||
|
||||
// Start is called before the first frame update
|
||||
@@ -34,6 +35,7 @@ public class UIManager : MonoBehaviour
|
||||
{
|
||||
gameOverLabel.gameObject.SetActive(true);
|
||||
restartButton.gameObject.SetActive(true);
|
||||
scoreboardButton.gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user