为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
冉冉说 2024-01-20 21:47:36
我可能问错了问题,因为我在网上找不到任何关于此的信息......本质上我的目标是在 project\Schemas\Move.cs 中定义一个移动类: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(...))或类似的东西。希望有人可以帮助我!
查看完整描述

3 回答

?
森林海

TA贡献2011条经验 获得超2个赞

如果我正确理解你的意思,你正在尝试拥有一个例如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列表总是可以的!Moves因此,如果需要,每次您确实想要销毁实例时,都必须“手动”清理列表Move。


查看完整回答
反对 回复 2024-01-20
?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

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


MoveManager.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);

    }

}

既然我已经确认这项工作有效,下一步就是向注册的动作添加方法,这才是真正的魔力所在。


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


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


查看完整回答
反对 回复 2024-01-20
?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

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

选项1:

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

选项2:

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


查看完整回答
反对 回复 2024-01-20
  • 3 回答
  • 0 关注
  • 67 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信