VR 光标附加部分在每个图像中淡入淡出,一次一个在指针输入上

VR Cursor Additional Sections Fading In Each Image One at a Time on Pointer Enter

我有一个光标,我正在为 Oculus 设置的 VR 中使用它。

与交互式 object 交互时,光标的反应是使用颜色淡入四个图像组件之一。这是代表正在加载。

我通过执行以下操作进行了设置: 在按钮上设置事件触发器。
事件触发器有一个 On Pointer Enter 部分。 on pointer 部分已将光标加载到其中。 光标由一个parent和四个children.
组成 四个 children 是脚本中的图像列表。

当光标悬停在按钮上时,它们会一起淡入。

这几乎是正确的,但我希望它们在前一个完成淡入后淡入。

float waitTime = 10.00f;
bool switchOn = false;
public List<Image> CursorLoaders = new List<Image>();


void Update()

{
    if (switchOn)
    {
        for (int i = 0; i <= 3; i++)
        {
            StartCoroutine(FadeIn(CursorLoaders[i]));
        }
    }
}



public IEnumerator FadeIn(Image CursorPiece)
{
    float ElapsedTime = 0.0f;
    float TotalTime = 0.5f;

    while (ElapsedTime < TotalTime)
    {
        ElapsedTime += Time.deltaTime;
        CursorPiece.color = Color.Lerp(new Color(CursorPiece.color.r, CursorPiece.color.g, CursorPiece.color.b, 0.0f), new Color(CursorPiece.color.r, CursorPiece.color.g, CursorPiece.color.b, 1), (ElapsedTime / TotalTime));

        yield return null;
    }
}

public void ButtonCountDown()
{
    switchOn = true;

}

我在资产商店中查找过类似的东西,包括 VR 样本 - 它主要使用填充量和方向 - 这很好但不适合这个。我还在 coRoutine 中创建了 WaitForSeconds,我还尝试在等待中添加另一个协程。我有点挠头。我终于在网上寻找类似的渐进式淡入淡出示例或一个接一个改变事物的方法,但无济于事。

为您的 IEnumerator 函数添加延迟。添加起来很简单,应该可以正常工作。然后,当您对此调用 StartCoroutine 时,您会告诉方法哪一块 在开始淡出之前要等待多长时间(可能是某个值乘以数组索引)。

public IEnumerator FadeIn(Image CursorPiece, float delayTime)
{
    float ElapsedTime = 0.0f;
    float TotalTime = 0.5f;

    while (ElapsedTime < delayTime)
    {
        ElapsedTime += Time.deltaTime;
        yield return null;
    }
    ElapsedTime = 0;
    while (ElapsedTime < TotalTime)
    {
        ElapsedTime += Time.deltaTime;
        CursorPiece.color = Color.Lerp(new Color(CursorPiece.color.r, CursorPiece.color.g, CursorPiece.color.b, 0.0f), new Color(CursorPiece.color.r, CursorPiece.color.g, CursorPiece.color.b, 1), (ElapsedTime / TotalTime));

        yield return null;
    }
}

执行类似下面的操作,您可以在不受 for 循环约束的情况下淡化每个图像。

void Update()
{
    if (switchOn)
        FadeImages();
}

int counter = 0;
void FadeImages()
{
    ElapsedTime += Time.deltaTime;
    Image CursorPiece = CursorLoaders[counter];
    CursorPiece.color = Color.Lerp(new Color(CursorPiece.color.r, CursorPiece.color.g, CursorPiece.color.b, 0.0f), new Color(CursorPiece.color.r, CursorPiece.color.g, CursorPiece.color.b, 1), (ElapsedTime / TotalTime));

    if (ElapsedTime > TotalTime) //if the image has completely faded, move to the next one
    {
        ElapsedTime = 0; // reset fade timer
        counter++; // increment CursorLoader[] index
    }
    if (counter >= 4) // if you have faded all the elements, stop the method.
        switchOn = false;
}