我的 XNA/Monogame Tower Defense 中有几个变量需要在类之间传递。不幸的是,其中许多是自动生成的,所以我不能只引用它们。是否有类似全局变量的东西可供我使用?同样,这是在 Monogame(XNA)/C# 中。
请您参考如下方法:
只需创建一个静态类,您将在其中存储所有全局变量,并且可以从所有类访问它。
public static class MyGlobals
{
public static int LevelNumber { get; set; }
public static string CurrentScore { get; set; }
...
}
并通过以下方式从任何地方访问它
public class AnotherClass
{
public void SomeMethod()
{
MyGlobals.LevelNumber = ...
string score = MyGlobals.CurrentScore;
}
}
