代码之家  ›  专栏  ›  技术社区  ›  Aleksa Ristic

等待协同路由完成[复制]

  •  0
  • Aleksa Ristic  · 技术社区  · 9 年前

    在那之后我有一次合作 N 秒清除文本并将其恢复为原始形状。问题是,第一次返回后,协同程序就再也不会继续了( wait for seconds ).

    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() 是)在协同程序完成之前不会销毁?

    1 回复  |  直到 9 年前
        1
  •  1
  •   Draco18s no longer trusts SE    9 年前

    如果您想等待一个协程完成……那么该方法也必须是一个协程。

    根据具体情况,您有3种选择:

    1. yield .
      • 您还可以将委托传递到当前协同路由并将其用作回调,然后现有协同路由的每次使用都可以提供自己的回调委托。然后,“等待完成”代码进入该委托。
    2. 将当前的非协程方法转换为协程。
    3. 创建一个新的协同路由,将需要等待其他协同路由的代码放入其中,包括对现有协同路由的调用, 产量 在另一次合作中。