【问题标题】:How to call a function X number of times at the same time without one interrupting the other?如何在不中断另一个的情况下同时调用函数 X 次?
【发布时间】:2016-08-19 08:52:09
【问题描述】:

假设我有这个功能:

int num;

void aa()
{
  num=0;
  //Wait 1 sec
  num++;
  //Print ("num= "+num ..
  //Wait 1 sec
  num++;
  //Print ("num= "+num ..
}

如果我同时调用 aa() 两次,我会得到:

num=1 from first time called
num=2 from second time called
num=3 from first time called
num=4 from second time called

但我希望结果是

num=1 from first time called
num=1 from second time called
num=2 from first time called
num=2 from second time called

所以我想要一个函数的多个实例,但不中断或与另一个实例混合。

注意:我使用的是 Unity 和 C#。

【问题讨论】:

  • 听起来你需要 num 成为 aa 中的局部变量而不是字段......或者在不同的对象上调用 aa。如果没有更多信息,我们不能说更多。
  • 听起来你需要实例......

标签: c# function unity3d methods


【解决方案1】:
void aa()
{
  int num;
  num=0;
  num++;
  num++;
}

根据 Jon Skeet 对您的 OP 的评论。

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多