简体   繁体   English

如何在 C# 中存储有组织的常量列表

[英]How to store an organised list of constants in C#

I want to store fixed settings for each game level.我想为每个游戏级别存储固定设置。 I want it to be structured like this:我希望它的结构如下:

namespace LevelSettings
{
    namespace Bar
    {
        public static int ExtraActiveBlocks = 3;

        "Level1"
        {   public static float Speed = 0.6f;
            public static string ActiveColor = "red";
            public static string InactiveColor = "black";
        },
        "Level2"
        {
            public static float Speed = 0.6f;
            public static string ActiveColor = "red";
            public static string InactiveColor = "black";
        },
        "Level3"
        {
            public static float Speed = 0.6f;
            public static string ActiveColor = "red";
            public static string InactiveColor = "black";
        }

This is obviously not valid C# code.这显然不是有效的 C# 代码。 However I'm asking how to code it in C#.但是我问如何在 C# 中对其进行编码。

So in my C# (Unity) script I want to access the fields like this:所以在我的 C# (Unity) 脚本中,我想访问这样的字段:

str currentLevel = "Level1"
float currentSpeed = LevelSettings.Bar.{currentLevel}.Speed;

Is this storage and access functionality achievable in C#?在 C# 中是否可以实现这种存储和访问功能? If so, how can the above be done?如果是这样,如何完成上述操作?

with a static dictionary inside your constants where the key is the level, and the value is an object representing the 3 properties.在常量中使用 static 字典,其中键是级别,值是代表 3 个属性的 object。

namespace LevelSettings
{
    public class LevelSetting
    {
       public float Speed {get;}
       public string ActiveColor{get;}
       public string InactiveColor{get;}
    
       public LevelSetting(float speed, string activeColor, string inactiveColor)
       {
           /// set the properties, omitted for brevity
       }
    }

    class Bar
    {
        public static int ExtraActiveBlocks = 3;
        public static IReadOnlyDictionary<string,LevelSetting> Levels = new Dictionary<string,LevelSetting>()
        {
           ["Level1"] = new LevelSetting(0.6f, "red","black"),
           ["Level2"] = new LevelSetting(0.6f, "red","black"),
           ["Level3"] = new LevelSetting(0.6f, "red","black")
        };
    }
}

You can then do然后你可以做

string currentLevel = "Level1"
float currentSpeed = LevelSettings.Bar.Levels[currentLevel].Speed;

You could use nested classes to get multiple levels:您可以使用嵌套类来获得多个级别:

public static class Bar{
    public const  ExtraActiveBlocks = 3;
    public static class Level1{
          public const float Speed = 0.6f;
          public const string ActiveColor = "red";
          public const string InactiveColor = "black";
    }
}

If you want to index your properties you need to use a list or dictionary:如果你想索引你的属性,你需要使用列表或字典:

public static class Bar{
    public const  ExtraActiveBlocks = 3;
    public static readonly IReadOnlyList<Level> Levels = new List<Level>(){ 
new Level(){Speed = 0.6, ActiveColor = "red", InactiveColor = "black"}};

    public class Level{
          public float Speed {get; init;}
          public string ActiveColor {get; init;}
          public string InactiveColor {get; init;}
    }
}

Note that you should make your constants either const or static readonly .请注意,您应该将常量设为conststatic readonly I would highly advice against using any mutable global state, since that will likely result in difficulty knowing what is accessing your properties.我强烈建议不要使用任何可变的全局 state,因为这可能会导致难以知道什么正在访问您的属性。

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

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