小编Ryo*_*olu的帖子

派生类的构造函数可以具有比其基类的构造函数更多的参数吗?

我有一个基类和2个派生类.基类有一些简单的受保护浮点变量,我的基类的构造函数如下:

public Enemy(float _maxhp, float _damage)
{
    maxhp = _maxhp;
    health = _maxhp;
    damage = _damage;
}
Run Code Online (Sandbox Code Playgroud)

但是,我的派生类Range还有2个浮点变量attack and attackSpeed需要在创建新实例时作为参数传入Range但我似乎无法做到这一点,因为There is no argument that corresponds to the required formal parameter '_maxhp' of 'Enemy.Enemy(float, float)'当我尝试使用构造函数进行派生时出现错误有这些参数的课程:

public Range(float _maxhp, float _damage, float _attack, float _attackSpeed)

但是具有相同数量的params的构造函数可以工作

public Range(float _maxhp, float _damage)

为什么会发生这种情况并且有某种解决方法?提前致谢.

c# constructor class unity-game-engine

3
推荐指数
1
解决办法
760
查看次数

Unity:什么在我的函数中分配内存?

我对内存和优化比较陌生。我的任务是优化一个现有项目,在查看 Unity 中的分析器时,我发现有一个函数会生成一堆东西供 GC 清理。

该函数是这样的:

public bool AnyCanvasOpen()
{
    for (int i = 0; i < canvasList.Count; i++)
        if (canvasList[i].activeSelf && canvasList[i].name != "MainUICanvas")
            return true;

    return false;
}
Run Code Online (Sandbox Code Playgroud)

其中 canvasList 只是游戏对象的列表。我的 282B 分配可能是什么原因?

c# memory arrays string unity-game-engine

2
推荐指数
1
解决办法
1080
查看次数

标签 统计

c# ×2

unity-game-engine ×2

arrays ×1

class ×1

constructor ×1

memory ×1

string ×1