ipe*_*nal 1 c# arrays struct button unity-game-engine
这是我创建的结构.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的另一个类中,我通过调用创建了一个实例
scr_Globals.Bar[] myBars = new scr_Globals.Bar[2];
Run Code Online (Sandbox Code Playgroud)
在我的更新我正在做
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码是针对这两个按钮完成的,所以我有相同的代码但是对于数组的位置1.我有一个从Unity的UI创建的按钮,当它被点击时,它激活了我创建的一个功能,它设置了一个bool.该代码看起来像这样
public void OnButton1Click()
{
myBars[0].SetStart (true);
}
Run Code Online (Sandbox Code Playgroud)
只要单击该按钮并调用该函数,它就会将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;
Run Code Online (Sandbox Code Playgroud)
所以,如果你写:
Bar x = new Bar();
Bar y = new Bar();
x.SetStart(true);
bool b = y.GetStart();
Run Code Online (Sandbox Code Playgroud)
......那b将是真的.返回的值GetStart不依赖于您调用它的值...
你不希望那些字段是静态的 - 它们意味着代表每个值的状态的一部分,对吗?
我实际上也建议反对可变结构,但这是另一回事.我还建议反对所有这些GetXyz/ SetXyz方法 - 转而学习C#属性.
如果您是C#的新手,我建议您首先在Unity环境之外学习它 - 安装Visual Studio 2015社区版,并通过一本好书来学习通过控制台应用程序等语言的基础知识.你将在一个更简单的环境中进行实验,你不会经常想知道奇怪的行为是由C#还是Unity引起的.