示例#1
0
    public IEnumerator Collect(Wealth wealth)
    {
        if (u.collectNum >= u.MaxCollectNum)
        {
            Debug.Log("采集次数达到上限");
            yield break;
        }
        if (wealth.isCollected == true)
        {
            Debug.Log("不能同时采集");
            yield break;
        }
        wealth.isCollected = true;
        Color       color     = wealth.GetComponentInChildren <SpriteRenderer>().color;
        GameObject  newSlider = GameObject.Instantiate(SliderPrefab, Canvas.transform);
        AudioSource collectVoice;

        if (wealth.name.Equals("Wood"))
        {
            collectVoice = voice[3];
        }
        else if (wealth.name.Equals("Stone") || wealth.name.Equals("Iron"))
        {
            collectVoice = voice[4];
        }
        else if (wealth.name.Equals("Berry"))
        {
            collectVoice = voice[5];
        }
        else
        {
            collectVoice = null;
        }
        if (collectVoice != null)
        {
            collectVoice.Play();
        }
        newSlider.transform.position = Camera.main.WorldToScreenPoint(wealth.transform.position + new Vector3(0, 1.5f, 0));
        notInCollect = false;
        float timeCount = 0;

        while (true)
        {
            timeCount += Time.deltaTime;
            if (wealth.count == 1)
            {
                color.a = 1 - timeCount / CollectTime;
                wealth.GetComponentInChildren <SpriteRenderer>().color = color;
            }
            newSlider.GetComponent <Slider>().value = timeCount / CollectTime;
            if (newSlider.GetComponent <Slider>().value >= 1)
            {
                break;
            }
            yield return(null);
        }
        u.collectNum++;
        if (wealth.name.Equals("Berry"))
        {
            u.Energy += 25;
            u.ControlMaxEnergy();
            u.collectNum--;
            wealth.count--;
        }
        else
        {
            package[u.collectNum - 1] = wealth.name;
            wealth.count--;
        }
        if (wealth.count == 0)
        {
            GameObject.Destroy(wealth.gameObject);
        }
        wealth.isCollected = false;
        if (u.collectNum > 0)
        {
            bagImage.gameObject.SetActive(true);
        }
        GameObject.Destroy(newSlider);
        notInCollect = true;
        if (collectVoice != null)
        {
            collectVoice.Stop();
        }
    }