首先,我必须指定我在Unity 5.3中工作,而新的MonoDevelop不允许我进行调试.团结只是崩溃:(
所以我有一个"目标"列表,我需要根据3个标准进行排序:
这是我的代码:
public class Goal {
public int ID;
public int Level;
public bool Active;
}
...
List<Goal> goals;
goals.Sort((a, b) => {
// first chooses the Active ones (if any)
var sort = b.Active.CompareTo(a.Active);
if (sort == 0) {
// then sort by level
sort = (a.Level).CompareTo(b.Level);
// if same level, randomize. Returns -1, 0 or 1
return sort == 0 ? UnityEngine.Random.Range(-1, 2) : sort;
} else {
return sort;
}
});
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,有时我会在非活动目标之后获得一个或多个活动目标,但我不明白为什么.