我有一个统一的 ScriptableObject
[CreateAssetMenu(fileName = "New Food", menuName = "Prismalia/ScriptableObject/Food")]
public class FoodScriptableObject : ScriptableObject, IComparable
{
[Header("Basic Info")]
public string itemID;
public new string name;
[Header("Main food stats")]
public string MainEffect;
public int MainEffectValue;
public bool MainValuePercent = false;
public int Amount;
[Header("Sub food stats")]
public string SubEffect;
public int SubEffectValue;
public bool SubValuePercent = false;
public string Description;
[Header("Enums")]
public Rarity Rarity;
public FoodType FoodType;
public MainFoodEffect MainFoodEffect;
public FoodEffectsTypes MainFoodEffectType;
public SubFoodEffect SubFoodEffect;
public FoodEffectsTypes SubFoodEffectType;
[Header("Visuals")]
public Sprite MainSprite;
public Sprite SubSprite;
public GameObject Prefab;
[Header("Arrays")]
public string[] RefinementRankDescription;
public int CompareTo(object obj)
{
var a = this;
var b = obj as FoodScriptableObject;
if (a.Amount < b.Amount)
return -1;
if (a.Amount > b.Amount)
return 1;
return 0;
}
}
我将其中很多保存在另一个 Scriptnd 的列表中,我根据数量对其进行排序
if(bSort)
{
scripts.Sort();
}
如何让它对 acd 和 dec 进行排序?基于数量以及如何使其从差异变量或枚举进行排序以按稀有度进行实例排序?
public enum Rarity
{
Common,
Uncommon,
Rare,
Epic,
Legendary,
Mythic
}
如果有人有帮助,请尽量不要使用 lamda 输入,因为我不明白 lamda
我尝试创建一个新列表并将其复制到它,因为我有多个可编写脚本的对象,并且我将所有对象保存在普通列表中并通过 GetType() 获取类型,然后使用值
首先,我知道您提到尝试不使用 lamda,但是我认为它为您的问题提供了一个很好的解决方案,并为其提供了进一步的功能。我希望这个例子可以帮助您更好地理解它。
首先我将 Rarity Enum 更改为具有值:
public enum Rarity
{
Common = 0,
Uncommon = 1,
Rare = 2,
Epic = 3,
Legendary = 4,
Mythic = 5
}
确保你有这个:
using System.Collections.Generic;
和using System.Linq;
以下是一些实现 asc 和 desc 排序的示例,以及将来可能有用的其他示例:
public static void OrderByExample()
{
List<FoodScriptableObject> foodsList = new List<FoodScriptableObject>();//populate this list with some data
List<FoodScriptableObject> foodsAsc = foodsList.OrderBy(foodItem => foodItem.Rarity).ToList();
List<FoodScriptableObject> foodsDesc = foodsList.OrderByDescending(foodItem => foodItem.Rarity).ToList();
List<FoodScriptableObject> orderedRarityThenName = foodsList
.OrderBy(foodItem => foodItem.MainEffectValue)
.ThenBy(foodItem => foodItem.name).ToList();
List<FoodScriptableObject> furtherOrdering = foodsList
.OrderBy(foodItem => foodItem.MainEffectValue)
.ThenBy(foodItem => foodItem.name)
.ThenBy(foodItem => foodItem.SubEffect).ToList();
}