Простое табло в Unity3D для ранжирования и размещения на 1-м, 2-м и 3-м местах - PullRequest
0 голосов
/ 25 октября 2019

Я работаю над простым табло в единстве, которое складывает и вычитает оценки с помощью кнопок, но не может понять, как получить оценки на 1-м, 2-м и 3-м месте.

Как и в большинстве игр, я хотел показать только первые три рекорда. Несмотря на то, что в этом примере показаны только два балла, вероятно, этой программе должно быть до 10 баллов.

Есть предложения?

public class Scores : MonoBehaviour
{
    public Text scoreText;
    public Text scoreText2;
    public Text firstPlace;
    public Text secondPlace;
    public Text thirdPlace;
    public int score;
    public int score2;

    public void Addition()
    {
        score++;
        scoreText.text = "" + score;
    }

    public void Subtraction()
    {      
        score--;
        scoreText.text = "" + score;
    }

    private void Update()
    {
        if (score > score2)
        {
            firstPlace.text = "Texas A&M";
            secondPlace.text = "University of Houston";
            thirdPlace.text = "LSU"
        }
     }
}  

1 Ответ

0 голосов
/ 25 октября 2019

Лучший способ добиться этого - словари:

// Create a dictionary
public Dictionary <float,string> scores = new Dictionary<float ,string>();

// Add scores you want to add
scores.Add(1,"SomeText");
scores.Add(4,"SomeOtherText");
scores.Add(3,"SomeOtherText");

// convert the keys into a list
float[] order = scores.Keys.ToList();
// sort array in reverse order
order.Sort.Reverse();

/// print the order
Console.Log("First Place"+scores[order[0]]);
Console.Log("Second Place"+scores[order[1]]);
Console.Log("Third Place"+scores[order[2]]);


Ссылки:
https://www.dotnetperls.com/sort-dictionary
https://www.geeksforgeeks.org/different-ways-to-sort-an-array-in-descending-order-in-c-sharp/

Пожалуйста, прости меня за любые орфографические ошибки.

...