等待协程完成
Wait for coroutine to finish
我有协程,在 N
秒后清除文本并 return 将其恢复为原始形状。问题是协程在第一个 return (wait for seconds
).
之后永远不会继续
我在其他地方遇到过这个问题,我发现这是因为我在协程完成之前销毁了 Gameobject,所以我把它变成了 return bool 但现在我很困惑,不能在这里使用相同的技巧,因为我是通过未初始化的脚本启动协程。该脚本只有静态函数,我通过它启动协程。这是我的代码:
void OnMouseDown()
{
bool safeDestroy = false;
IGatherable gather = CharacterCommands.character.GetComponent<IGatherable>();
if(gather != null)
{
switch(itemID)
{
case 3:
Drops[] d = ChestDrop.GetItemFromDropStash(drops, gather, this); //Here is function that is starting function with coroutine PROBLEM
if(d.Length == 0)
{
safeDestroy = true;
}
else
{
drops = d;
}
break;
default:
if(ItemDatabase.GetItem(itemID).maxStackable < Inventory.GetCoins() + amount)
{
Parameters.centerText.text = "Not enough space in your bag!";
safeDestroy = Parameters.clearText(Parameters.centerText, 3, this); //Coroutine i had same problem but done it this way.
}
else
{
gather.GatherItem(itemID, amount);
safeDestroy = true;
}
break;
}
}
if(safeDestroy)
{
Destroy(this.gameObject);
}
}
这里是函数本身:
public static Drops[] GetItemFromDropStash(Drops[] drops, IGatherable gather, MonoBehaviour justToStartCoroutine)
{
foreach(Drops drop in drops)
{
int r = UnityEngine.Random.Range(1, 101);
if(r < drop.chance)
{
if(ItemDatabase.GetItem(drop.itemID).maxStackable > Inventory.GetItemFromInventoryById(drop.itemID).amount + drop.amount)
{
Inventory.AddItemToInventory(drop.itemID, drop.amount);
Parameters.centerText.text = "+" + drop.amount + " " + ItemDatabase.GetItem(drop.itemID).itemName;
switch(ItemDatabase.GetItem(drop.itemID).itemRarity)
{
case ItemRarity.common:
Parameters.centerText.color = Color.gray;
break;
case ItemRarity.normal:
Parameters.centerText.color = new Color(80, 100, 255);
break;
case ItemRarity.rare:
Parameters.centerText.color = new Color(255, 80, 80);
break;
case ItemRarity.special:
Parameters.centerText.color = new Color(200, 0, 220);
break;
case ItemRarity.legacy:
Parameters.centerText.color = new Color(199, 224, 0);
break;
case ItemRarity.legendary:
Parameters.centerText.color = new Color(224, 169, 0);
break;
}
bool t = Parameters.clearText(Parameters.centerText, 3, justToStartCoroutine);
int i = Array.IndexOf(drops, drop);
List<Drops> tmp = new List<Drops>(drops);
tmp.RemoveAt(i);
drops = tmp.ToArray();
}
else if (Inventory.CheckForFreeSpaceInInventory() == true)
{
Inventory.AddItemToInventoryToNewSlot(drop.itemID, drop.amount);
Parameters.centerText.text = "+" + drop.amount + " " + ItemDatabase.GetItem(drop.itemID).itemName;
switch(ItemDatabase.GetItem(drop.itemID).itemRarity)
{
case ItemRarity.common:
Parameters.centerText.color = Color.gray;
break;
case ItemRarity.normal:
Parameters.centerText.color = new Color(80, 100, 255);
break;
case ItemRarity.rare:
Parameters.centerText.color = new Color(255, 80, 80);
break;
case ItemRarity.special:
Parameters.centerText.color = new Color(200, 0, 220);
break;
case ItemRarity.legacy:
Parameters.centerText.color = new Color(199, 224, 0);
break;
case ItemRarity.legendary:
Parameters.centerText.color = new Color(224, 169, 0);
break;
}
bool t = Parameters.clearText(Parameters.centerText, 3, justToStartCoroutine);
int i = Array.IndexOf(drops, drop);
List<Drops> tmp = new List<Drops>(drops);
tmp.RemoveAt(i);
drops = tmp.ToArray();
}
else
{
Parameters.centerText.text = "Not enough space in inventory!";
bool t = Parameters.clearText(Parameters.centerText, 3, justToStartCoroutine);
}
}
}
return drops;
}
我怎样才能让我的物品(OnMouseDown()
所在的位置)在协程完成之前不被破坏?
如果您想等待协程完成...那么该方法也必须是协程。
您有 3 个选项,具体取决于您的具体情况:
- 将需要等待的代码移到现有的协程方法中(在最后一个current
yield
之后)。
- 您还可以将委托传递给当前协程并将其用作回调,然后每次使用现有协程都可以提供自己的回调委托。 "wait for finish" 代码然后进入该委托。
- 将当前的非协程方法转为协程
- 创建一个新协程,将需要等待其他协程的代码推入其中,包括对现有协程的调用,
yield
在另一个协程上。
我有协程,在 N
秒后清除文本并 return 将其恢复为原始形状。问题是协程在第一个 return (wait for seconds
).
我在其他地方遇到过这个问题,我发现这是因为我在协程完成之前销毁了 Gameobject,所以我把它变成了 return bool 但现在我很困惑,不能在这里使用相同的技巧,因为我是通过未初始化的脚本启动协程。该脚本只有静态函数,我通过它启动协程。这是我的代码:
void OnMouseDown()
{
bool safeDestroy = false;
IGatherable gather = CharacterCommands.character.GetComponent<IGatherable>();
if(gather != null)
{
switch(itemID)
{
case 3:
Drops[] d = ChestDrop.GetItemFromDropStash(drops, gather, this); //Here is function that is starting function with coroutine PROBLEM
if(d.Length == 0)
{
safeDestroy = true;
}
else
{
drops = d;
}
break;
default:
if(ItemDatabase.GetItem(itemID).maxStackable < Inventory.GetCoins() + amount)
{
Parameters.centerText.text = "Not enough space in your bag!";
safeDestroy = Parameters.clearText(Parameters.centerText, 3, this); //Coroutine i had same problem but done it this way.
}
else
{
gather.GatherItem(itemID, amount);
safeDestroy = true;
}
break;
}
}
if(safeDestroy)
{
Destroy(this.gameObject);
}
}
这里是函数本身:
public static Drops[] GetItemFromDropStash(Drops[] drops, IGatherable gather, MonoBehaviour justToStartCoroutine)
{
foreach(Drops drop in drops)
{
int r = UnityEngine.Random.Range(1, 101);
if(r < drop.chance)
{
if(ItemDatabase.GetItem(drop.itemID).maxStackable > Inventory.GetItemFromInventoryById(drop.itemID).amount + drop.amount)
{
Inventory.AddItemToInventory(drop.itemID, drop.amount);
Parameters.centerText.text = "+" + drop.amount + " " + ItemDatabase.GetItem(drop.itemID).itemName;
switch(ItemDatabase.GetItem(drop.itemID).itemRarity)
{
case ItemRarity.common:
Parameters.centerText.color = Color.gray;
break;
case ItemRarity.normal:
Parameters.centerText.color = new Color(80, 100, 255);
break;
case ItemRarity.rare:
Parameters.centerText.color = new Color(255, 80, 80);
break;
case ItemRarity.special:
Parameters.centerText.color = new Color(200, 0, 220);
break;
case ItemRarity.legacy:
Parameters.centerText.color = new Color(199, 224, 0);
break;
case ItemRarity.legendary:
Parameters.centerText.color = new Color(224, 169, 0);
break;
}
bool t = Parameters.clearText(Parameters.centerText, 3, justToStartCoroutine);
int i = Array.IndexOf(drops, drop);
List<Drops> tmp = new List<Drops>(drops);
tmp.RemoveAt(i);
drops = tmp.ToArray();
}
else if (Inventory.CheckForFreeSpaceInInventory() == true)
{
Inventory.AddItemToInventoryToNewSlot(drop.itemID, drop.amount);
Parameters.centerText.text = "+" + drop.amount + " " + ItemDatabase.GetItem(drop.itemID).itemName;
switch(ItemDatabase.GetItem(drop.itemID).itemRarity)
{
case ItemRarity.common:
Parameters.centerText.color = Color.gray;
break;
case ItemRarity.normal:
Parameters.centerText.color = new Color(80, 100, 255);
break;
case ItemRarity.rare:
Parameters.centerText.color = new Color(255, 80, 80);
break;
case ItemRarity.special:
Parameters.centerText.color = new Color(200, 0, 220);
break;
case ItemRarity.legacy:
Parameters.centerText.color = new Color(199, 224, 0);
break;
case ItemRarity.legendary:
Parameters.centerText.color = new Color(224, 169, 0);
break;
}
bool t = Parameters.clearText(Parameters.centerText, 3, justToStartCoroutine);
int i = Array.IndexOf(drops, drop);
List<Drops> tmp = new List<Drops>(drops);
tmp.RemoveAt(i);
drops = tmp.ToArray();
}
else
{
Parameters.centerText.text = "Not enough space in inventory!";
bool t = Parameters.clearText(Parameters.centerText, 3, justToStartCoroutine);
}
}
}
return drops;
}
我怎样才能让我的物品(OnMouseDown()
所在的位置)在协程完成之前不被破坏?
如果您想等待协程完成...那么该方法也必须是协程。
您有 3 个选项,具体取决于您的具体情况:
- 将需要等待的代码移到现有的协程方法中(在最后一个current
yield
之后)。- 您还可以将委托传递给当前协程并将其用作回调,然后每次使用现有协程都可以提供自己的回调委托。 "wait for finish" 代码然后进入该委托。
- 将当前的非协程方法转为协程
- 创建一个新协程,将需要等待其他协程的代码推入其中,包括对现有协程的调用,
yield
在另一个协程上。