Hi…
I’m fairly new to all this and i was wondering if any of you just… plough the road so to speak with spaghetti-coding just to get the script working, then spend the time whittling away, might be just me… but I had this idea, I quickly thunk it all up, put it together till it was all working then I said to myself oh my lord I have to clean up that code, so it does the same thing just better & faster and in less lines… I must do this more, it’s called development doh I know… as they say there’s always version 1.1…
Below is the bulky clobber then the next one is the tidy version, which thankfully looks much nicer.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
/**
* GALAP - v 1.0a
* DEVELOPER NOTES : San Holo 05/04/2015
* ==========================================
* Note: Display high-scores as teletype message with color rotation
* TO-DO : everything
*
**/
public class HighscoreDisplay : MonoBehaviour
{
public TextMesh HighScoresLine1Mesh = null;
public TextMesh HighScoresLine2Mesh = null;
public TextMesh HighScoresLine3Mesh = null;
public TextMesh HighScoresLine4Mesh = null;
public TextMesh HighScoresLine5Mesh = null;
public TextMesh HighScoresLine6Mesh = null;
public TextMesh HighScoresLine7Mesh = null;
public TextMesh HighScoresLine8Mesh = null;
public TextMesh HighScoresLine9Mesh = null;
public TextMesh HighScoresLine10Mesh = null;
public float DisplayTime = 20.0f;
private float _timer = 0.0f;
private string text, message, text2, message2, text3, message3, text4, message4, text5, message5, text6, message6, text7, message7, text8, message8, text9, message9, text10, message10, text11, message11, text12, message12;
public float letterPause = 0.01f;
public bool FormatTopScore = false; // used to change 1st place for name entry screen
private GameObject topplayerstext, galaxypilotstext;
private TextMesh pilotsLeader, topLeader;
void Start ()
{
_timer = 0.0f;
if (Application.loadedLevelName == "MainMenu") {
topplayerstext = GameObject.Find ("Top_Players");
galaxypilotstext = GameObject.Find ("Pilots");
}
}
void OnEnable ()
{
GameManager gameManager = GameManager.instance;
if (gameManager != null) {
List<HighScoreEntry> highScores = gameManager.GetHighScores ();
if (highScores != null) {
// Assign some variables
StringBuilder HighScoresLine1 = new StringBuilder ();
StringBuilder HighScoresLine2 = new StringBuilder ();
StringBuilder HighScoresLine3 = new StringBuilder ();
StringBuilder HighScoresLine4 = new StringBuilder ();
StringBuilder HighScoresLine5 = new StringBuilder ();
StringBuilder HighScoresLine6 = new StringBuilder ();
StringBuilder HighScoresLine7 = new StringBuilder ();
StringBuilder HighScoresLine8 = new StringBuilder ();
StringBuilder HighScoresLine9 = new StringBuilder ();
StringBuilder HighScoresLine10 = new StringBuilder ();
// Get the names & scores one by one
HighScoreEntry hse = highScores [0]; // Top Score!
if (hse != null && hse.Name != null) {
if (FormatTopScore) {
HighScoresLine1.Append ("1st - ").Append (hse.Name).Append (" > ").Append (hse.Score);
} else {
HighScoresLine1.Append ("* 1ST : ").Append (hse.Name).Append (" - High Score : ").Append (hse.Score).Append (" *");
}
}
HighScoreEntry hse2 = highScores [1];
if (hse2 != null && hse2.Name != null) {
HighScoresLine2.Append ("2nd - ").Append (hse2.Name).Append (" > ").Append (hse2.Score);
}
HighScoreEntry hse3 = highScores [2];
if (hse3 != null && hse3.Name != null) {
HighScoresLine3.Append ("3rd - ").Append (hse3.Name).Append (" > ").Append (hse3.Score);
}
HighScoreEntry hse4 = highScores [3];
if (hse4 != null && hse4.Name != null) {
HighScoresLine4.Append ("4th - ").Append (hse4.Name).Append (" > ").Append (hse4.Score);
}
HighScoreEntry hse5 = highScores [4];
if (hse5 != null && hse5.Name != null) {
HighScoresLine5.Append ("5th - ").Append (hse5.Name).Append (" > ").Append (hse5.Score);
}
HighScoreEntry hse6 = highScores [5];
if (hse6 != null && hse6.Name != null) {
HighScoresLine6.Append ("6th - ").Append (hse6.Name).Append (" > ").Append (hse6.Score);
}
HighScoreEntry hse7 = highScores [6];
if (hse7 != null && hse7.Name != null) {
HighScoresLine7.Append ("7th - ").Append (hse7.Name).Append (" > ").Append (hse7.Score);
}
HighScoreEntry hse8 = highScores [7];
if (hse8 != null && hse8.Name != null) {
HighScoresLine8.Append ("8th - ").Append (hse8.Name).Append (" > ").Append (hse8.Score);
}
HighScoreEntry hse9 = highScores [8];
if (hse9 != null && hse9.Name != null) {
HighScoresLine9.Append ("9th - ").Append (hse9.Name).Append (" > ").Append (hse9.Score);
}
HighScoreEntry hse10 = highScores [9];
if (hse10 != null && hse10.Name != null) {
HighScoresLine10.Append ("10th - ").Append (hse10.Name).Append (" > ").Append (hse10.Score);
}
// Start the coroutine chain-reaction
if (HighScoresLine1Mesh != null) {
text = "";
message = HighScoresLine1.ToString ();
StartCoroutine (PauseFirstScore ());
}
if (HighScoresLine2Mesh != null) {
text2 = "";
message2 = HighScoresLine2.ToString ();
}
if (HighScoresLine3Mesh != null) {
text3 = "";
message3 = HighScoresLine3.ToString ();
}
if (HighScoresLine4Mesh != null) {
text4 = "";
message4 = HighScoresLine4.ToString ();
}
if (HighScoresLine5Mesh != null) {
text5 = "";
message5 = HighScoresLine5.ToString ();
}
if (HighScoresLine6Mesh != null) {
text6 = "";
message6 = HighScoresLine6.ToString ();
}
if (HighScoresLine7Mesh != null) {
text7 = "";
message7 = HighScoresLine7.ToString ();
}
if (HighScoresLine8Mesh != null) {
text8 = "";
message8 = HighScoresLine8.ToString ();
}
if (HighScoresLine9Mesh != null) {
text9 = "";
message9 = HighScoresLine9.ToString ();
}
if (HighScoresLine10Mesh != null) {
text10 = "";
message10 = HighScoresLine10.ToString ();
}
}
}
}
void Update ()
{
_timer += Time.deltaTime;
if (SceneManager_MainMenu.instance != null) {
if (_timer > DisplayTime) {
// clear & reset
_timer = 0.0f;
text = "";
HighScoresLine1Mesh.text = text;
text2 = "";
HighScoresLine2Mesh.text = text2;
text3 = "";
HighScoresLine3Mesh.text = text3;
text4 = "";
HighScoresLine4Mesh.text = text4;
text5 = "";
HighScoresLine5Mesh.text = text5;
text6 = "";
HighScoresLine6Mesh.text = text6;
text7 = "";
HighScoresLine7Mesh.text = text7;
text8 = "";
HighScoresLine8Mesh.text = text8;
text9 = "";
HighScoresLine9Mesh.text = text9;
text10 = "";
HighScoresLine10Mesh.text = text10;
if (topplayerstext) {
topplayerstext.GetComponent<MeshRenderer> ().enabled = false;
text12 = "";
topLeader.text = text12;
}
if (galaxypilotstext) {
galaxypilotstext.GetComponent<MeshRenderer> ().enabled = false;
text11 = "";
pilotsLeader.text = text11;
}
SceneManager_MainMenu.instance.NextScreen ();
}
}
}
IEnumerator TypeHighScoreLine1 ()
{
foreach (char letter in message.ToCharArray()) {
text += letter;
HighScoresLine1Mesh.text = text;
yield return 0;
if (FormatTopScore) {
yield return new WaitForSeconds (letterPause);
} else {
yield return new WaitForSeconds (0.01f); // Quicker delay setting for longer format
}
}
StartCoroutine (TypeHighScoreLine2 ());
}
IEnumerator TypeHighScoreLine2 ()
{
foreach (char letter in message2.ToCharArray()) {
text2 += letter;
HighScoresLine2Mesh.text = text2;
yield return 0;
yield return new WaitForSeconds (letterPause);
}
StartCoroutine (TypeHighScoreLine3 ());
}
IEnumerator TypeHighScoreLine3 ()
{
foreach (char letter in message3.ToCharArray()) {
text3 += letter;
HighScoresLine3Mesh.text = text3;
yield return 0;
yield return new WaitForSeconds (letterPause);
}
StartCoroutine (TypeHighScoreLine4 ());
}
IEnumerator TypeHighScoreLine4 ()
{
foreach (char letter in message4.ToCharArray()) {
text4 += letter;
HighScoresLine4Mesh.text = text4;
yield return 0;
yield return new WaitForSeconds (letterPause);
}
StartCoroutine (TypeHighScoreLine5 ());
}
IEnumerator TypeHighScoreLine5 ()
{
foreach (char letter in message5.ToCharArray()) {
text5 += letter;
HighScoresLine5Mesh.text = text5;
yield return 0;
yield return new WaitForSeconds (letterPause);
}
StartCoroutine (TypeHighScoreLine6 ());
}
IEnumerator TypeHighScoreLine6 ()
{
foreach (char letter in message6.ToCharArray()) {
text6 += letter;
HighScoresLine6Mesh.text = text6;
yield return 0;
yield return new WaitForSeconds (letterPause);
}
StartCoroutine (TypeHighScoreLine7 ());
}
IEnumerator TypeHighScoreLine7 ()
{
foreach (char letter in message7.ToCharArray()) {
text7 += letter;
HighScoresLine7Mesh.text = text7;
yield return 0;
yield return new WaitForSeconds (letterPause);
}
StartCoroutine (TypeHighScoreLine8 ());
}
IEnumerator TypeHighScoreLine8 ()
{
foreach (char letter in message8.ToCharArray()) {
text8 += letter;
HighScoresLine8Mesh.text = text8;
yield return 0;
yield return new WaitForSeconds (letterPause);
}
StartCoroutine (TypeHighScoreLine9 ());
}
IEnumerator TypeHighScoreLine9 ()
{
foreach (char letter in message9.ToCharArray()) {
text9 += letter;
HighScoresLine9Mesh.text = text9;
yield return 0;
yield return new WaitForSeconds (letterPause);
}
StartCoroutine (TypeHighScoreLine10 ());
}
IEnumerator TypeHighScoreLine10 ()
{
foreach (char letter in message10.ToCharArray()) {
text10 += letter;
HighScoresLine10Mesh.text = text10;
yield return 0;
yield return new WaitForSeconds (letterPause);
}
}
IEnumerator TypePilotsLeaderText ()
{
foreach (char letter in message11.ToCharArray()) {
text11 += letter;
pilotsLeader.text = text11;
yield return 0;
yield return new WaitForSeconds (letterPause);
}
}
IEnumerator TypeTopLeaderText ()
{
foreach (char letter in message12.ToCharArray()) {
text12 += letter;
topLeader.text = text12;
yield return 0;
yield return new WaitForSeconds (letterPause * 4); // changed to slow typing effect
}
}
IEnumerator PauseFirstScore ()
{
yield return new WaitForSeconds (1);
if (topplayerstext) {
topplayerstext.GetComponent<MeshRenderer> ().enabled = true;
topLeader = topplayerstext.GetComponent<TextMesh> ();
message12 = "*** BEST PLAYERS ***";
StartCoroutine (TypeTopLeaderText ());
}
StartCoroutine (TypeHighScoreLine1 ());
yield return new WaitForSeconds (1.2f);
if (galaxypilotstext) {
galaxypilotstext.GetComponent<MeshRenderer> ().enabled = true;
pilotsLeader = galaxypilotstext.GetComponent<TextMesh> ();
message11 = "-- GALAXY PILOTS --";
StartCoroutine (TypePilotsLeaderText ());
}
}
}
Now the better version
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
/**
* GALAP - v 1.0a
* DEVELOPER NOTES : Han Solo 05/04/2015
* ==========================================
* Note: Display high-scores as teletype message with color rotation
* TO-DO : nothing, all done.
*
**/
public class HighscoreDisplay : MonoBehaviour
{
public TextMesh[] HighScoresLineMesh = null;
public float DisplayTime = 20.0f, letterPause = 0.01f;
private float _timer = 0.0f;
private string text, text2, message, message2 = null;
private GameObject galaxypilotstext;
private TextMesh pilotsLeader;
private string[] RankMarker = {"ST","ND","RD","TH"};
private int rankStep = 0, recordsToShow = 10;
void Start ()
{
_timer = 0.0f;
if (Application.loadedLevelName == "MainMenu") {
galaxypilotstext = GameObject.Find ("Pilots");
}
}
void OnEnable ()
{
StartCoroutine (DelayScores ());
}
IEnumerator ShowScores ()
{
GameManager gameManager = GameManager.instance;
if (gameManager != null) {
List<HighScoreEntry> highScores = gameManager.GetHighScores ();
if (highScores != null) {
for (int i = 0; i < recordsToShow; ++i) {
StringBuilder HighScoresLine = new StringBuilder (i);
HighScoreEntry hse = highScores [i];
if (hse != null && hse.Name != null) {
HighScoresLine.Append (i + 1).Append (RankMarker [rankStep]).Append (" - ").Append (hse.Name).Append (" > ").Append (hse.Score);
message = HighScoresLine.ToString ();
}
if (HighScoresLineMesh [i] != null) {
foreach (char letter in message.ToCharArray()) {
text += letter;
HighScoresLineMesh [i].text = text;
yield return 0;
yield return new WaitForSeconds (letterPause);
}
if (rankStep < RankMarker.Length - 1) {
rankStep++;
}
message = "";
text = "";
}
}
}
}
}
void Update ()
{
if (Application.loadedLevelName == "MainMenu") {
_timer += Time.deltaTime;
if (SceneManager_MainMenu.instance != null) {
if (_timer > DisplayTime) {
_timer = 0.0f;
rankStep = 0;
for (int i = 0; i < recordsToShow; ++i) {
text = "";
HighScoresLineMesh [i].text = text;
}
if (galaxypilotstext) {
galaxypilotstext.GetComponent<MeshRenderer> ().enabled = false;
pilotsLeader.text = "";
}
SceneManager_MainMenu.instance.NextScreen ();
}
}
}
}
IEnumerator DelayScores ()
{
yield return new WaitForSeconds (1);
StartCoroutine (ShowScores ());
if (galaxypilotstext) {
galaxypilotstext.GetComponent<MeshRenderer> ().enabled = true;
pilotsLeader = galaxypilotstext.GetComponent<TextMesh> ();
message2 = "--- GALAXY PILOTS ---";
foreach (char letter in message2.ToCharArray()) {
text2 += letter;
pilotsLeader.text = text2;
yield return 0;
yield return new WaitForSeconds (letterPause);
}
text2 = "";
}
}
}
I hope you do this also otherwise I’m mental, cheers