我正在尝试为一些大学作品制作一个排名表。想到的更好的方法是为每个位置创建一个包含字符串和整数的列表。我在文档中搜索:https://unity3d.com/es/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries ?gq=sort,在那里我找到了解决我的问题的完美示例,所以我开始工作。现在,我已经创建了界面:
public class Name_Puntuation: IComparable
public string namePlayer;
public int puntuation;
public Name_Puntuation( string newNomPlayer, int newPuntuation){
nomPlayer = newNomPlayer;
puntuation= newPuntuation;
}
public int CompareTo(Name_Puntuation other){
if (other == null) {
return 1;
}
return Puntuation - other.Puntuation;
}
Run Code Online (Sandbox Code Playgroud)
我还在我的 GameController 中添加了我想要对列表执行的操作。该类控制游戏的画布。我们将所有信息打印到文本字段,所以我想处理排名:
public class GameController : MonoBehaviour {
public List <Name_Puntuation> ranking;
void Start () {
playerName= GetComponent<MenuController> ().inp; //name the player choosed
ranking = new List<Name_Puntuation> ();
}
void Update () { }
public void Ranking(){ …Run Code Online (Sandbox Code Playgroud)