-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRankingSystem.cs
More file actions
86 lines (74 loc) · 3.01 KB
/
Copy pathRankingSystem.cs
File metadata and controls
86 lines (74 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System.Collections;
using System.Linq;
using TMPro;
using UnityEngine;
public class RankingSystem : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI scoreText_no1;
[SerializeField] private TextMeshProUGUI scoreText_no2;
[SerializeField] private TextMeshProUGUI scoreText_no3;
[SerializeField] private TextMeshProUGUI scoreText_no4;
[SerializeField] private AudioSource showRankingSound;
void Start()
{
// コルーチンを開始して、遅延後にランキング処理を実行
StartCoroutine(ShowRanking());
scoreText_no1.gameObject.SetActive(false);
scoreText_no2.gameObject.SetActive(false);
scoreText_no3.gameObject.SetActive(false);
scoreText_no4.gameObject.SetActive(false);
}
private IEnumerator ShowRanking()
{
// RecordScore の取得(Singleton優先、無ければ名前で検索)
var record = RecordScore.Instance;
if (record == null)
{
var go = GameObject.Find("RecordedScore");
if (go != null)
record = go.GetComponent<RecordScore>();
}
if (record == null)
{
Debug.LogError("RecordedScore のインスタンスが見つかりません。");
yield break; // コルーチンをここで停止
}
// スコア収集
var entries = new[]
{
new { Player = "1P", Score = record.score_1p },
new { Player = "2P", Score = record.score_2p },
new { Player = "3P", Score = record.score_3p },
new { Player = "4P", Score = record.score_4p },
};
// 値が大きい順に並び替え(同点はプレイヤー名で安定化)
var sorted = entries
.OrderByDescending(e => e.Score)
.ThenBy(e => e.Player)
.ToArray();
// ログとUIテキストの更新
Debug.Log($"1位: {sorted[0].Player} = {sorted[0].Score}");
yield return new WaitForSeconds(0.7f);
showRankingSound.Play();
scoreText_no1.text = $"1位 {sorted[0].Player} {sorted[0].Score}pt";
scoreText_no1.gameObject.SetActive(true);
Debug.Log($"2位: {sorted[1].Player} = {sorted[1].Score}");
yield return new WaitForSeconds(0.7f);
showRankingSound.Play();
scoreText_no2.text = $"2位 {sorted[1].Player} {sorted[1].Score}pt";
scoreText_no2.gameObject.SetActive(true);
Debug.Log($"3位: {sorted[2].Player} = {sorted[2].Score}");
yield return new WaitForSeconds(0.7f);
showRankingSound.Play();
scoreText_no3.text = $"3位 {sorted[2].Player} {sorted[2].Score}pt";
scoreText_no3.gameObject.SetActive(true);
Debug.Log($"4位: {sorted[3].Player} = {sorted[3].Score}");
yield return new WaitForSeconds(0.7f);
showRankingSound.Play();
scoreText_no4.text = $"4位 {sorted[3].Player} {sorted[3].Score}pt";
scoreText_no4.gameObject.SetActive(true);
}
void Update()
{
}
}