繁体   English   中英

如何创建模块化 class 列表属性

[英]How to create modular class list property

我可能问错了问题,因为我无法在网上找到任何关于此的...

本质上,我的目标是在 project\Schemas\Move.cs 中定义一个移动 class:

public class Move
{
    private int power;
    private float accuracy;
    private string type; 
    private string style; 

    public Move(int p, float a, string t, string s, float sh)
    {
        this.power = p;
        this.accuracy = a;
        this.type = t;
        this.style = s;
    }

}

而且我希望可以从我的游戏引擎访问所有动作的集合,但我希望根据 project\Moves\move_* 下的其他文件自动构建该移动集合。

老实说,我不确定如何在 C# 中执行此操作。 在另一种语言中,我将简单地初始化一个全局集合,并在每个移动文件中调用GlobalCollection.Add(new Move(...))或类似的东西。

希望有人可以帮助我!

如果我正确理解您,您正在尝试拥有一个例如List<Move>并且您想在此处“注册”每个创建的Move实例。 那么为什么不简单地像

// This is a static class so it does not have to be instantiated
// but rather simply "lives" in the assets
public static class MoveManager
{
    public static List<Move> Moves = new List<Move>();
}

然后你可以这样

public class Move
{
    private int power;
    private float accuracy;
    private string type; 
    private string style;  

    public Move(int p, float a, string t, string s, float sh)
    {
        power = p;
        accuracy = a;
        type = t;
        style = s;

        // a static class member is simply accessed via the type itself
        MoveManager.Moves.Add(this);
    }
}

问题:你什么时候从列表中删除它们?

通常您想在解构器中执行此操作,例如

~Move()
{
    MoveManager.Moves.Remove(this);
}

但是在 Unity 中,解构函数不会被自动调用 - 尤其是当任何东西仍在引用此Move Moves时... 因此,如果需要,每次您真正想要销毁Move实例时,都必须“手动”清理Moves列表。

没有(好的)方法可以做你想做的事。

选项1:

自己亲手写出来。 例如,就像我在这里所做的那样 这将始终有效,并且是您要避免的事情。

选项 2:

反射性地检查程序集并加载它包含的所有类,如下所示 但这会容易做出意想不到的事情。 例如,当您创建独立构建时,Unity 会从编译中剔除这些类,因为它们不会在任何地方被引用。 或者无意中实例化了一个不应该的 object。 或者不记得“哦,是的,MoveXYZ 有一个独特的构造函数,它需要一个额外的参数。”

补充derHugo给出的答案,以防万一其他人偶然发现这一点。 我正在使用建议的方法,但发现为了在运行时“导入”类型,必须使用[RuntimeInitializeOnLoadMethod]设置。 可能还有其他方法,但这是我最终得到的 3 个移动文件,用于完成答案。

移动管理器.cs

 public static class MoveManager
 {
    public static List<Move> Moves = new List<Move>();
 }

移动.cs

public class Move
{
    public string name;
    public string description;
    public Target target;
    public int power;
    public float accuracy;
    public int cost;
    public game_Type type;
    public Style style;

    public Move(
        string name
        , string description
        , int power
        , float accuracy
        , int cost
        , string typeName
        , string styleName
        , string targetType
    )
    {
        this.name = name;
        this.power = power;
        this.accuracy = accuracy;
        this.description = description;

        this.type = game_Types.getByName(typeName);
        this.style = MasterStyles.getByName(styleName);
        this.target = new Target(targetType);

        // a static class member is simply accessed via the type itself
        Debug.Log("Registering " + name + " type!");
        MoveManager.Moves.Add(this);
    }
}

move_basic.cs

class move_basic
{
    [RuntimeInitializeOnLoadMethod]
    static void OnRuntimeMethodLoad()
    {
        // Name this move
        string name = "Attack!";
        // Describe this move
        string description = "Strike out at the enemy with tooth, claw, weapon, or whatever else is available!";
        // Choose the type of target this move is for
        // self/ally/selfAlly/enemy/enemies
        string target = "enemy";
        // The move typing - This determines cost reduction as well as potential attack bonuses
        string typeName = "physical";
        game_Type type = game_Types.Types.Find(x => x.name == typeName);
        // The move style - This determines additional bonuses and modifiers
        string styleName = "martial";
        // Style style = ??
        // Power of the move, base/'normal' power is 50.
        int power = 50;
        // Accuracy of the move, base/normal is 90
        int accuracy = 90;
        // MP Cost of the move
        int cost = 0;


        Move mv = new Move(name, description, power, accuracy, cost, typeName, styleName, target);
    }
}

下一步,现在我已经确认这项工作将是向已注册的动作添加方法,这是 IMO 真正的魔力所在。

当您需要创建一个可能需要包含可调用方法的易于扩展的对象库时,这似乎是一个非常理想的解决方案。 如果不是出于这种需要,从 csv 或其他东西导入属性可能会更好。

总的来说,我对统一和游戏设计还很陌生,所以我很高兴能编辑/更正/改进这篇文章以帮助未来的搜索者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM