unity3d - Unity3D:使用 AnimationCurve 动态设置 AudioSource 音量
问题描述
这里是 Unity 的新手。对于这个项目,我正在读取外部 AnimationCurve,并尝试使用它来调整更新函数中每一帧的 GameObject 的音量。我找到了有关如何访问我的 AudioSource GameObject 组件的音量的文档(这是有效的),但我无法弄清楚如何使用我的评估曲线来更新音量。
具有导入的 AnimationCurve 和评估值的屏幕截图
我的cs脚本包含在下面。对不起,如果它有点不透明。这是一个神经科学实验,所以我需要仔细控制动画曲线。它读取用于在 SetAnimationFrames() 函数中将关键帧添加到新 AnimationCurve (curveThisTrl) 的时间和值(存储在 currentCurveData 中)。
关键部分在 Update() 中。初始的tone.volume 是正确的,所以我知道它正在从GameObject 中读取该属性。我还可以在每一帧的调试日志中看到正确的曲线值,所以我知道曲线评估正在工作。它只是没有像我想要的那样改变 GameObject 的音量属性。
希望这是足够的信息来发现问题,但如果不是,我可以尝试提供一个测试 AnimationCurve 以实现重现性。提前感谢您的帮助。
public class AnimationControl : MonoBehaviour {
private DataController dataControllerRef;
private CurveDataSingleTrial currentCurveData;
private float initTime;
public AnimationCurve curveThisTrl;
AudioSource tone;
void Awake()
{
initTime = Time.time;
}
// Use this for initialization
void Start ()
{
// Get references to key objects in scene
dataControllerRef = FindObjectOfType<DataController>(); // Has values from JSON
tone = GetComponent<AudioSource>();
Debug.Log(tone.volume);
// query the DataController for the current curve data
currentCurveData = dataControllerRef.GetCurrentCurveData();
SetAnimationFrames();
}
void SetAnimationFrames()
{
int numKeyFrames = currentCurveData.keyframeTimes.Length;
for (int c = 0; c < numKeyFrames; c++)
{
curveThisTrl.AddKey(currentCurveData.keyframeTimes[c], currentCurveData.keyframeVals[c]);
}
}
// Update is called once per frame
void Update ()
{
float currTime = Time.time - initTime;
tone.volume = curveThisTrl.Evaluate(currTime);
Debug.Log(tone.volume);
}
}