簡體   English   中英

我可以使用IEnumerator作為更新功能嗎?

[英]Can i use IEnumerator as Update func?

我有一些對象之類的結構-4個子對象-每個孩子還有2個孩子。 我想為每個子對象更改其顏色的alpha。

我創建了應該更改Alpha的IEnumarator,但是當我對其進行測試時,它僅更改為0.8,而不是0,並且我還想按時間更改它,例如平穩地更改2秒,但是它發生得很快

imageComponents = gameObject.GetComponentsInChildren<Image>();
textComponents = gameObject.GetComponentsInChildren<Text>();    

IEnumerator HideAllBoosters(Transform _object)
    {
        foreach (Image image in imageComponents)
        {
            Color _color = image.GetComponent<Image>().color;
            _color = new Color(_color.r, _color.g, _color.b, 0);
            image.GetComponent<Image>().color = Color.Lerp(image.color, _color, 10 * Time.deltaTime);
        }
        foreach (Text text in textComponents)
        {
            Color _color = text.GetComponent<Text>().color;
            _color = new Color(_color.r, _color.g, _color.b, 0);
            text.GetComponent<Text>().color = Color.Lerp(text.color, _color, 10 * Time.deltaTime);
        }
        yield return null;
    } 

Idk如何正確執行操作,mb我應該在Update中為每個對象更改顏色,但是我不確定它對於清晰易懂的代碼有好處,所以我要問的是-我可以為每個對象使用另一個IEnumerator就像更新一樣工作,像:

    foreach (Image image in imageComponents)
    {
         StartCourutine(changeAlpha(image));
    }

錯誤在於傳遞10 * Time.deltaTime作為Lerp(Vector3 a, Vector3 b, float t);tLerp(Vector3 a, Vector3 b, float t);

如果您查看Lerp()文檔

當t = 0時返回a。 當t = 1時返回b。 當t = 0.5時,返回a和b之間的中間點。

這意味着,為了使您的Alpha具有良好的淡入淡出效果, Lerp()t值應在一定時間內從1變為0(如果要淡入則為0到1)。 現在,您傳入的是10 * Time.deltaTime ,根據幀速率,這將始終是相同的值。 (在這種情況下,約為0.8)。

要解決此問題,您需要將t設置為0到1之間緩慢增加/減少(取決於您要淡入還是淡出的值)的值。 一種實現方法是將邏輯封裝在while循環中。

float speed = 0.01f; 
float time = 0;
while ( time < 1)
{
    time += speed;
    //Rest of code
}

這將遞增time由值speed (在這種情況下0.01)每循環運行時,在這種情況下為100次迭代(0.01 * 100 = 1)。

現在,我們可以在Lerp()方法中將此time值用作t值,以使其平滑過渡

image.color = Color.Lerp(image.color, _color, time);

如果您希望淡入淡出花費更多或更少的時間,只需增加或減小speed的值即可。

整個實現看起來像這樣(請注意,我也做了一些優化,稍后將介紹)

public float speed = 0.01f; //The speed at which the fade happens

private Image[] imageComponents;
private Text[] textComponents;

void Start()
{
    imageComponents = gameObject.GetComponentsInChildren<Image>(); //Cache the images so we don't have to find them every time
    textComponents = gameObject.GetComponentsInChildren<Text>(); //Cache the texts

    StartCoroutine(HideAllBoosters());//Start the Coroutine
}

IEnumerator HideAllBoosters()
{
    float t = 0; //Start value for our loop
    while (t < 1) // 1 indicates the max value of t at which the loop stops. In this case after 100 iterations since the speed is 0.01
    {
        t += speed;

        foreach (Image image in imageComponents)
        {
            Color _color = image.color;
            _color = new Color(_color.r, _color.g, _color.b, 0);
            image.color = Color.Lerp(image.color, _color, t); //t dictates how far in the interpolation we are.
        }
        foreach (Text text in textComponents)
        {
            Color _color = text.color;
            _color = new Color(_color.r, _color.g, _color.b, 0);
            text.color = Color.Lerp(text.color, _color, t);
        }
        yield return null;
    }
}

我已經完成的優化:通過imageComponentstextComponents循環的數組已經是ImageText類型。 這意味着,當您在foreach()循環中循環瀏覽它們時, Image imageText text已經屬於它們各自的類型,並且已經擁有對其組件的引用。 這意味着您.GetComponent<Image>().GetComponent<Text>()調用。

例如:

Color _color = image.GetComponent<Image>().color;

是相同的

Color _color = image.color;

我還刪除了HideAllBoosters所需的Transform _object HideAllBoosters參數,因為該方法似乎根本沒有使用。 可能是您稍后在此問題范圍之外的函數中確實使用了該值。 如果是這種情況,則需要將其包括在內。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM