Unity Array of Struct : 当设置一个数组下标的变量时,它为所有的设置它
Unity Array of Struct : When setting a variable of one of the array subscripts, it sets it for all of them
这是我创建的结构。
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;
}
}
在我的另一个 class 中,我通过调用
创建了一个实例
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# 属性。
如果您是 C# 新手,我真的建议您首先在 Unity 环境之外学习它 - 安装 Visual Studio 2015 Community Edition 并通过控制台应用程序等了解该语言的基础知识,使用一本好书的帮助。您将在一个更简单的环境中进行试验,并且您不会一直想知道奇怪的行为是由于 C# 还是 Unity。
这是我创建的结构。
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;
}
}
在我的另一个 class 中,我通过调用
创建了一个实例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# 属性。
如果您是 C# 新手,我真的建议您首先在 Unity 环境之外学习它 - 安装 Visual Studio 2015 Community Edition 并通过控制台应用程序等了解该语言的基础知识,使用一本好书的帮助。您将在一个更简单的环境中进行试验,并且您不会一直想知道奇怪的行为是由于 C# 还是 Unity。