- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我创建的结构。
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 创建的按钮,当它被点击时,它激活了我创建的一个功能,该功能设置了一个 bool 值。该代码看起来像这样
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。
关于c# - Unity Array of Struct : When setting a variable of one of the array subscripts, 它为所有这些设置它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31616137/
我有一个数组 items[] items[] 中的每一项都是一个结构体。 item 有键 id、date、value(即 item.id、item.date、item.value) 我想使用 Stru
我想存储 100 名员工。 RollNo,姓名,工资,时间(各种数据,我无法在这里解释,但你可以看下面的代码片段来理解 main() { struct day { int hour
这个问题在这里已经有了答案: storage size of ‘names’ isn’t known (3 个答案) 关闭 5 年前。 我正在尝试蓝牙编程,遇到了这个我不明白的问题。基本上,当我使用
这是一个奇怪的事情: 我有一个结构,它包含指向相同类型结构的指针和指向其他类型结构的指针,以及一些其他值。 struct animal { struct animal * father;
我有一个结构定义如下(名称不同) struct str1 { int field1; struct str2; } 我在一个函数中有一个*str1。我想要一个指向 str2 的指针。 所以
DISK_DETECTION_INFO is defined as有什么原因吗? typedef struct _DISK_DETECTION_INFO { DWORD Size
我正在尝试打包一个字符串和一个字符串的长度。 fmt = '
我在创建结构时遇到问题。 我的结构: public struct Device: Codable { let data: DeviceData let meta: Meta? } pu
struct Item { var name:String? var type:String? var value:Int? var tag:Int? } ... ..
// NewReaderSize returns a new Reader whose buffer has at least the specified 43 // size. If the ar
这个问题在这里已经有了答案: Sorting a vector of custom objects (14 个答案) 关闭 3 年前。 在下面的 C++ 片段中, 如何基于 TwoInts 结构中的
#include struct Header { unsigned long long int alignment; }; int main(void) { struct Heade
我有一个目前看起来像这样的结构(缩写为仅显示基本部分): typedef struct { uint32_t baudrate; ... some other internally u
对此没有太多解释,这就是我所拥有的: public struct PACKET_HEADER { public string computerIp; publi
我有以下代码: struct MyStruct{ data: &'a str, } fn get(S: &'a MyStruct) -> &'a str{ S.data } fn se
struct S1 { char c; int i; }; struct S3 { char c1; struct S1 s; double c2; }; 我正
我有一个名为 Parameter 的协议(protocol): protocol Parameter { var name: String { get } var unit: Unit
有 2 个 struct 定义 A 和 A。我知道 struct A 可以包含指向 struct A 的 POINTER 但我不明白为什么 struct A 不能包含struct A(不是指针) 最佳
我有以下代码: struct MyStruct{ data: &'a str, } fn get(S: &'a MyStruct) -> &'a str{ S.data } fn se
为了说明这一点,这里有一个小的不可变结构和一个更新它的函数: (struct timeseries (variable observations) #:transparent) (define (ad
我是一名优秀的程序员,十分优秀!