32. 代码优化

1.代码优化


1.代码优化

csharp 复制代码
基于枚举创建不同对象

1).根据工厂和字典模式

public static class BuffEffectFactory
{
    // 字典:枚举 -> 对象构造委托
    private static readonly Dictionary<BuffEffect, Func<BuffEffectBase>> _effectCreators = new();

    static BuffEffectFactory()
    {
        // 预注册所有Buff类型
        _effectCreators.Add(BuffEffect.RebuildCard, () => new RebuildBuffEffect());
        _effectCreators.Add(BuffEffect.ControlDice, () => new ControlDiceBuffEffect());
        _effectCreators.Add(BuffEffect.BuildLvVariation, () => new BuildLvBuffEffect());
        _effectCreators.Add(BuffEffect.AddBuffByCondition, () => new AddBuffEffect());
        _effectCreators.Add(BuffEffect.DirectionChange, () => new DirectionChangeBuffEffect());
    }

    public static BuffEffectBase Create(BuffEffect effectType)
    {
        if (_effectCreators.TryGetValue(effectType, out var creator))
        {
            return creator();
        }
        throw new ArgumentOutOfRangeException(nameof(effectType), $"未注册的Buff类型:{effectType}");
    }
}

csharp 复制代码
2).通过特性和反射的方式

// 1. 定义特性:标记类对应的枚举值
[AttributeUsage(AttributeTargets.Class)]
public class BuffEffectAttribute : Attribute
{
    public BuffEffect EffectType { get; }
    public BuffEffectAttribute(BuffEffect effectType) => EffectType = effectType;
}

// 2. 给Buff类标记特性
[BuffEffect(BuffEffect.RebuildCard)]
public class RebuildBuffEffect : BuffEffectBase { }

[BuffEffect(BuffEffect.ControlDice)]
public class ControlDiceBuffEffect : BuffEffectBase { }
csharp 复制代码
static BuffEffectFactory()
{
    // 优化1:只扫描指定程序集(避免遍历所有程序集,减少扫描量)
    var targetAssembly = Assembly.GetExecutingAssembly(); // 假设Buff类都在当前程序集
    var buffTypes = targetAssembly.GetTypes()
        .Where(type => typeof(BuffEffectBase).IsAssignableFrom(type) 
                     && !type.IsAbstract 
                     && type.GetCustomAttribute<BuffEffectAttribute>() != null);

    foreach (var type in buffTypes)
    {
        var attribute = type.GetCustomAttribute<BuffEffectAttribute>();
        if (attribute == null) continue;

        // 优化2:缓存构造函数委托,替代Activator.CreateInstance
        var constructor = type.GetConstructor(Type.EmptyTypes); // 无参构造
        if (constructor == null)
        {
            throw new InvalidOperationException($"{type.Name} 必须提供无参构造函数");
        }
        // 将构造函数转为委托,调用时无反射损耗
        var creator = (Func<BuffEffectBase>)Delegate.CreateDelegate(
            typeof(Func<BuffEffectBase>), constructor);
        
        _effectCreators.Add(attribute.EffectType, creator);
    }
}
相关推荐
RReality11 小时前
【Unity Shader URP】Matcap 材质捕捉实战教程
java·ui·unity·游戏引擎·图形渲染·材质
魔士于安11 小时前
unity urp材质球大全
游戏·unity·游戏引擎·材质·贴图·模型
南無忘码至尊13 小时前
Unity学习90天 - 第 6 天 -学习物理 Material + 重力与阻力并实现弹跳球和冰面滑动效果
学习·unity·游戏引擎
mxwin16 小时前
Unity 单通道立体渲染(Single Pass Instanced)对 Shader 顶点布局的特殊要求
unity·游戏引擎·shader
魔士于安18 小时前
unity 低多边形 无人小村 木质建筑 晾衣架 盆子手推车,桌子椅子,罐子,水井
游戏·unity·游戏引擎·贴图·模型
RReality18 小时前
【Unity Shader URP】简易卡通着色(Simple Toon)实战教程
ui·unity·游戏引擎·图形渲染·材质
魔士于安19 小时前
unity 骷髅人 连招 武器 刀光 扭曲空气
游戏·unity·游戏引擎·贴图·模型
瑞瑞小安21 小时前
Unity功能篇:文本框随文字内容动态调整
ui·unity
南無忘码至尊1 天前
Unity学习90天-第7天-学习委托与事件(简化版)
学习·unity·游戏引擎
君莫愁。1 天前
【Unity】解决UGUI的Button无法点击/点击无反应的排查方案
unity·c#·游戏引擎·解决方案·ugui·按钮·button