You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
1 month ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using Unity.Multiplayer.Samples.BossRoom;
|
||
|
using UnityEngine;
|
||
|
using Unity.Netcode;
|
||
|
public class Scoreboard : NetworkBehaviour
|
||
|
{
|
||
|
PlayerScoreComponent[] playerScoreComponents;
|
||
|
List<PlayerScoreComponent> m_PlayerScoreComponents;
|
||
|
public List<PlayerItem> playerItems;
|
||
|
public GameObject playerItemPrefab;
|
||
|
public Transform Parent;
|
||
|
public override void OnNetworkSpawn()
|
||
|
{
|
||
|
if (IsServer)
|
||
|
{
|
||
|
Starter();
|
||
|
}
|
||
|
}
|
||
|
public void Starter()
|
||
|
{
|
||
|
playerScoreComponents = FindObjectsOfType<PlayerScoreComponent>();
|
||
|
for (int i = 0; i < playerScoreComponents.Length; i++)
|
||
|
{
|
||
|
playerScoreComponents[i].index = i;
|
||
|
m_PlayerScoreComponents.Add(playerScoreComponents[i]);
|
||
|
}
|
||
|
Instantiator(playerScoreComponents.Length);
|
||
|
StartCoroutine(ScoreUpdater());
|
||
|
}
|
||
|
public void Instantiator(int count)
|
||
|
{
|
||
|
for (int i = 0; i < count; i++)
|
||
|
{
|
||
|
playerItems.Add(Instantiate(playerItemPrefab, Parent).GetComponent<PlayerItem>());
|
||
|
playerItems[i].PlayerScore.text = playerScoreComponents[i].CurrentScore.ToString();
|
||
|
playerItems[i].PlayerName.text = playerScoreComponents[i].serverCharacter.uIStateDisplayHandler.m_UIState.playerName.ToString();
|
||
|
}
|
||
|
}
|
||
|
IEnumerator ScoreUpdater()
|
||
|
{
|
||
|
yield return new WaitForSeconds(0.5f);
|
||
|
while (true)
|
||
|
{
|
||
|
yield return new WaitForSeconds(0.1f);
|
||
|
m_PlayerScoreComponents.Sort((p1, p2) => p2.CurrentScore.CompareTo(p1.CurrentScore));
|
||
|
for (int i = 0; i < playerItems.Count; i++)
|
||
|
{
|
||
|
playerItems[i].transform.SetSiblingIndex(m_PlayerScoreComponents[i].index);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|