C# 结构的统一数组:当设置一个数组下标的变量时,它会为所有数组下标设置它
这是我创建的结构C# 结构的统一数组:当设置一个数组下标的变量时,它会为所有数组下标设置它,c#,arrays,button,struct,unity3d,C#,Arrays,Button,Struct,Unity3d,这是我创建的结构 public struct Bar { private static float deltaTime = 1.0f; private static bool AutoRun = false; private static bool AutoRunBought = false; private static bool Start = false; // DELTA TIME public float GetDeltaTime(
public struct Bar
{
private static float deltaTime = 1.0f;
private static bool AutoRun = false;
private static bool AutoRunBought = false;
private static bool Start = false;
// DELTA TIME
public float GetDeltaTime()
{
return deltaTime;
}
public void SetDeltaTime(float _dt)
{
deltaTime = _dt;
}
public void IncrementDeltaTime(float _deltaIn)
{
deltaTime += _deltaIn;
}
public void DecrementDeltaTime(float _deltaIn)
{
deltaTime -= _deltaIn;
}
// AUTO RUN
public bool GetAutoRun()
{
return AutoRun;
}
public void SetAutoRun(bool _autoBought)
{
AutoRunBought = _autoBought;
}
public bool GetAutoRunBought()
{
return AutoRun;
}
public void SetAutoRunBought(bool _autoBought)
{
AutoRunBought = _autoBought;
}
// START
public bool GetStart()
{
return Start;
}
public void SetStart(bool _start)
{
Start = _start;
}
}
在我的另一个类中,我通过调用
scr_Globals.Bar[] myBars = new scr_Globals.Bar[2];
在我的更新中,我正在做
if (myBars[0].GetAutoRun() == true)
{
myBars[0].IncrementDeltaTime (incrementBar1);
if (myBars[0].GetDeltaTime () > 40.0f) {
myBars[0].SetDeltaTime (1.0f);
globals.IncrementTotalMoney(1.0f);
}
}
else
{
if (myBars[0].GetStart() == true)
{
myBars[0].IncrementDeltaTime (incrementBar1);
if (myBars[0].GetDeltaTime () > 40.0f) {
myBars[0].SetDeltaTime (1.0f);
globals.IncrementTotalMoney(1.0f);
myBars[0].SetStart(false);
}
}
}
上面的代码是为两个按钮编写的,所以我有相同的代码,但用于数组的位置1。
我有一个按钮,它是从Unity的UI创建的,当它被点击时,它激活了我创建的一个函数,该函数设置了一个布尔值。代码看起来像这样
public void OnButton1Click()
{
myBars[0].SetStart (true);
}
每当单击该按钮并调用该函数时,它都会将myBars[0]和myBars[1]SetStart设置为true。非常感谢您的帮助。您的字段都是静态的:
private static float deltaTime = 1.0f;
private static bool AutoRun = false;
private static bool AutoRunBought = false;
private static bool Start = false;
所以如果你写:
Bar x = new Bar();
Bar y = new Bar();
x.SetStart(true);
bool b = y.GetStart();
。。。然后b
将为真。GetStart
返回的值根本不依赖于调用它的值
你不希望这些字段是静态的——它们代表每个值状态的一部分,对吗
实际上我也建议不要使用可变结构,但那是另一回事。我还建议不要使用所有的GetXyz
/SetXyz
方法-而是学习C#properties
如果您是C#新手,我真的建议您先在Unity环境之外学习它-安装Visual Studio 2015社区版,并借助一本好书,通过控制台应用程序等了解该语言的基础知识。您将在一个简单得多的环境中进行实验,您不会经常怀疑奇怪的行为是由C#还是由Unity造成的。您的字段都是静态的:
private static float deltaTime = 1.0f;
private static bool AutoRun = false;
private static bool AutoRunBought = false;
private static bool Start = false;
所以如果你写:
Bar x = new Bar();
Bar y = new Bar();
x.SetStart(true);
bool b = y.GetStart();
。。。然后b
将为真。GetStart
返回的值根本不依赖于调用它的值
你不希望这些字段是静态的——它们代表每个值状态的一部分,对吗
实际上我也建议不要使用可变结构,但那是另一回事。我还建议不要使用所有的GetXyz
/SetXyz
方法-而是学习C#properties
如果您是C#新手,我真的建议您先在Unity环境之外学习它-安装Visual Studio 2015社区版,并借助一本好书,通过控制台应用程序等了解该语言的基础知识。您将在一个简单得多的环境中进行实验,您不会经常怀疑奇怪的行为是由C#还是由Unity造成的。这解决了我的问题。事实上,它们都是静态变量。感谢您对VisualStudio社区版的建议。我今天会下载。非常感谢。用
struct
开始学习C#可能不是最好的主意。从错误使用中得到的错误比引用类型的错误使用更令人费解。这解决了我的问题。事实上,它们都是静态变量。感谢您对VisualStudio社区版的建议。我今天会下载。非常感谢。用struct
开始学习C#可能不是最好的主意。从错误使用中得到的错误比引用类型的错误使用更令人费解。