简体   繁体   English

音频源播放2分钟后执行一些操作

[英]Do something when the audio source has been playing for 2 mins

I've got a script working that blends one skybox into another. 我有一个脚本可以将一个天空盒融合到另一个天空盒中。 I need to write some code so that this blend animation happens when the audiosource is at 2 minutes. 我需要编写一些代码,以便当音频源在2分钟时发生此混合动画。

How can I do this? 我怎样才能做到这一点? I've read about AudioSource.timeSamples, but I don't really understand how I know when 2 minutes is reached. 我已经读过AudioSource.timeSamples,但是我不太了解何时到达2分钟。 Should I use some other method? 我应该使用其他方法吗?

void Start()
  {
  Debug.Log("Audio begins now.....");
  Invoke("TwoMinutesHasPassed", 120f);
  }

void TwoMinutesHasPassed()
  {
  Debug.Log("two minutes has passed");
  Debug.Log("now i will fade the background");
  StartCoroutine("FadeNow");
  }

private IEnumerator FadeNow()
  {
  tParam = 0f;
  while (tParam < 1)
    {
    tParam += Time.deltaTime * speed;  
    valToBeLerped = Mathf.Lerp(0, 1, tParam);
    Debug.Log("valToBeLerped is " + valToBeLerped.ToString("f4"));
    yield return null;
    }
  skyboxmaterial.SetFloat("_Blend", valToBeLerped);
  Debug.Log("fade is done.");
  }

Here is the working script: 这是工作脚本:

using UnityEngine;
using System.Collections;

public class animateSkyBox : MonoBehaviour {

public Skybox sky;
public Material skyboxmaterial; 
float tParam = 0f;
float valToBeLerped = 0f;
float speed = 0.1f; 



    void Start()
  {
        skyboxmaterial.SetFloat("_Blend", 0);
  Debug.Log("Audio begins now.....");
  Invoke("TwoMinutesHasPassed", 10f);
  }

void TwoMinutesHasPassed()
  {
  Debug.Log("two minutes has passed");
  Debug.Log("now i will fade the background");
  StartCoroutine("FadeNow");
  }

private IEnumerator FadeNow()
  {
  tParam = 0f;
  while (tParam < 1)
    {
    tParam +=  Time.deltaTime;  
    valToBeLerped = Mathf.Lerp(0, 1, tParam);
            skyboxmaterial.SetFloat("_Blend", valToBeLerped);
            yield return null;
    Debug.Log("valToBeLerped is " + valToBeLerped.ToString("f4"));

    }
  //skyboxmaterial.SetFloat("_Blend", valToBeLerped);
  Debug.Log("fade is done.");
  yield break;
  }

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM