在一些简单的游戏中,属性可以直接使用基础类型来定义,比如 int hp=100,float speed=50等等,但当战斗的复杂性提升,加入buff、装备等变量后,属性要根据buff的增加、删除等进行频繁的改变,又假如有些计算过程需要对基础值或附加值的百分比进行修正,那么简单的数值--属性系统就难以实现这些功能。
在dota2中,属性我们可以区分为基础属性(力量,敏捷,智力),以及由基础属性计算得到的属性(最大生命值,最大法力值,生命恢复,法力恢复,攻击力,攻击速度等等)
在UNITY GAS 的示例中,作者的设计是分为两个类Attribute和AttributeValue
AttributeValue是最基本的数值,里面四个字段,Attribute存储自己所属的属性的引用,Modifier用于添加临时的修正(如装备、buff等)
public struct AttributeValue
{
public AttributeScriptableObject Attribute;
public float BaseValue;
public float CurrentValue;
public AttributeModifier Modifier;
}
[Serializable]
public struct AttributeModifier
{
public float Add;
public float Multiply;
public float Override;
public AttributeModifier Combine(AttributeModifier other)
{
other.Add += Add;
other.Multiply += Multiply;
other.Override = Override;
return other;
}
}
AttributeScriptableObject
属性的基类,继承ScriptableObject,用于生成属性配置的实例存储。
public class AttributeScriptableObject : ScriptableObject
{
/// <summary>
/// Friendly name of this attribute. Used for dislpay purposes only.
/// </summary>
public string Name;
public event EventHandler PreAttributeChange;
public void OnPreAttributeChange(object sender, PreAttributeChangeEventArgs e)
{
EventHandler handler = PreAttributeChange;
PreAttributeChange?.Invoke(sender, e);
}
public virtual AttributeValue CalculateInitialValue(AttributeValue attributeValue, List<AttributeValue> otherAttributeValues)
{
return attributeValue;
}
public virtual AttributeValue CalculateCurrentAttributeValue(AttributeValue attributeValue, List<AttributeValue> o

文章讨论了如何在复杂游戏中升级属性管理,从单一的基础类型到UnityGAS中的Attribute和AttributeValue设计,提出了一种更灵活的属性计算方式,包括基础属性、衍生属性和加成机制的改进,以支持动态变化和多英雄配置。
3524

被折叠的 条评论
为什么被折叠?



