音频源播放2分钟时执行某些操作

时间:2016-04-04 16:18:43

标签: c# audio unity3d

我有一个脚本工作,将一个天空盒混合到另一个天空盒中。我需要编写一些代码,以便在音频源处于2分钟时发生这种混合动画。

我该怎么做? 我已经阅读了有关AudioSource.timeSamples的内容,但我真的不明白我何时知道2分钟的时间。我应该使用其他方法吗?

2 个答案:

答案 0 :(得分:2)

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.");
  }

答案 1 :(得分:0)

这是工作脚本:

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;
  }

  }