C# 等待协同程序完成
我有一个协同程序,在C# 等待协同程序完成,c#,unity3d,unity5,coroutine,C#,Unity3d,Unity5,Coroutine,我有一个协同程序,在N秒后清除文本并将其返回到原始形状。问题是,在第一次返回后,协同路由将永远不会继续(等待几秒钟) 我在其他地方遇到了这个问题,并发现它正在发生,因为我在协同程序完成之前销毁了游戏对象,所以我让它返回bool,但现在我感到困惑,不能在这里使用相同的技巧,因为我是通过未初始化的脚本启动协同程序的。该脚本只有静态函数,我通过它启动协同程序。这是我的密码: void OnMouseDown() { bool safeDestroy = false; IGathera
N
秒后清除文本并将其返回到原始形状。问题是,在第一次返回后,协同路由将永远不会继续(等待几秒钟)
我在其他地方遇到了这个问题,并发现它正在发生,因为我在协同程序完成之前销毁了游戏对象,所以我让它返回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);
}
}
void OnMouseDown()
{
bool-safeDestroy=false;
IGatherable gather=CharacterCommands.character.GetComponent();
if(聚集!=null)
{
开关(项目ID)
{
案例3:
Drops[]d=ChestDrop.GetItemFromDropStash(Drops,gather,this);//下面是一个函数,它正在启动具有协同路由问题的函数
如果(d.Length==0)
{
safeDestroy=true;
}
其他的
{
滴数=d;
}
打破
违约:
if(ItemDatabase.GetItem(itemID).maxStackable
下面是函数本身:
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;
}
publicstaticdrops[]GetItemFromDropStash(Drops[]Drops,IGatherable聚集,MonoBehavior justToStartCorroutine)
{
foreach(一滴接一滴)
{
int r=单位发动机随机范围(1101);
如果(rInventory.GetItemFromInventoryById(drop.itemID).amount+drop.amount)
{
Inventory.AddItemToInventory(drop.itemID,drop.amount);
Parameters.centerText.text=“+”+drop.amount+”+ItemDatabase.GetItem(drop.itemID).itemName;
开关(ItemDatabase.GetItem(drop.itemID.itemRarity)
{
case ItemRarity.common:
Parameters.centerText.color=color.gray;
打破
case ItemRarity.normal:
Parameters.centerText.color=新颜色(80100255);
打破
case ItemRarity.raven:
Parameters.centerText.color=新颜色(255、80、80);
打破
case ItemRarity.special:
Parameters.centerText.color=新颜色(200,0,220);
打破
案例ItemRarity.legacy:
Parameters.centerText.color=新颜色(1992240);
打破
case ItemRarity.legendary:
Parameters.centerText.color=新颜色(2241690);
打破
}
bool t=Parameters.clearText(Parameters.centerText,3,justToStartCoroutine);
int i=Array.IndexOf(drop,drop);
列表tmp=新列表(删除);
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;
开关(ItemDatabase.GetItem(drop.itemID.itemRarity)
{
case ItemRarity.common:
Parameters.centerText.color=color.gray;
打破
case ItemRarity.normal:
Parameters.centerText.color=新颜色(80100255);
打破
case ItemRarity.raven:
Parameters.centerText.color=新颜色(255、80、80);
打破
case ItemRarity.special:
Parameters.centerText.color=新颜色(200,0,220);
打破
案例ItemRarity.legacy:
Parameters.centerText.color=新颜色(1992240);
打破
case ItemRarity.legendary:
Parameters.centerText.color=新颜色(2241690);
打破
}
bool t=Parameters.clearText(Parameters.centerText,3,justToStartCoroutine);
int i=Array.IndexOf(drop,drop);
列表tmp=新列表(删除);
tmp.RemoveAt(i);
drops=tmp.ToArray();
}
其他的
{
Parameters.centerText.text=“库存空间不足!”;
bool t=Parameters.clearText(Parameters.centerText,3,justToStartCoroutine);
}
}
}
返回下降;
}
我如何才能实现,使我的项目(其中OnMouseDown()
是)在协同路由完成之前不会销毁?如果要等待协同路由完成…则该方法也必须是协同路由
根据具体情况,您有3个选项:
将需要等待的代码移到现有的协程方法中(在最后一次当前的yield
之后)。
- 您还可以将一个委托传递到当前协同路由并将其用作回调,然后现有协同路由的每次使用都可以提供自己的回调委托。“等待完成”代码随后进入该委托内部
不要把电流关上-