【问题标题】:VR Cursor Additional Sections Fading In Each Image One at a Time on Pointer EnterVR 光标在每个图像中的附加部分在指针输入时一次一个淡入
【发布时间】:2017-09-20 17:18:31
【问题描述】:

我在为 Oculus 设置的 VR 中使用了一个光标。

与交互对象交互时的光标是通过使用颜色淡入四个图像组件之一来做出反应。这是为了表示加载。

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

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

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

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,并且我还尝试添加另一个带有等待的协程。我有点挠头。我终于在网上寻找了类似的渐进式渐变示例或一个接一个地更改事物的方法,但无济于事。

【问题讨论】:

  • 如何分别为每个部分设置淡入淡出方法,并设置/检查布尔值以查看时间已经足够淡化前一个图像,然后再移动到下一个
  • 我也在想同样的事情——但我想知道——会不会更干净。但是,也许您在 co 例程中考虑了这一点;就像一种阻止其他人直到完成的方法。我正在看看我能用它做什么......

标签: c# unity3d unity5


【解决方案1】:

执行以下操作,您可以在不受 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;
}

【讨论】:

  • 这非常有效。我想我已经养成了使用 CoRoutines 进行转换的习惯,并且倾向于忘记函数作为解决方案。虽然我确信随着我的进步,我将能够使用协程解决这个问题。这对我来说是一个教训。再次感谢你。是循环让我立即跳到下一个列表对象 - 对吗?
  • 我认为协程可能是更简洁的方式,但我发现这样的方法更简单,同样有效,特别是因为我对协程也不是很满意!使用更新中的 for 循环,我仍然对它的工作原理一无所知。我也需要学习一些东西。
  • 这让我感觉好多了。我想这是一个通过经验学习的问题。再次感谢!
【解决方案2】:

为您的 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;
    }
}

【讨论】:

  • 我毫不怀疑它是微不足道的,而且我自己做了更多的工作,它会起作用。感谢您的努力和洞察力!
猜你喜欢
  • 1970-01-01
  • 2016-05-24
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 2013-07-15
  • 2013-07-01
  • 2012-01-25
  • 2023-03-05
相关资源
最近更新 更多