【发布时间】:2017-12-12 03:24:37
【问题描述】:
我正在尝试找到一种方法,使用音频文件中的音频频谱动态更改轨迹渲染器的宽度参数。我已经成功地改变了游戏对象的比例,但不能改变轨迹渲染器本身的实际宽度参数。这是用于动态更改使用音频频谱的比例的代码。任何帮助或建议将不胜感激!谢谢
使用音频频谱更改比例的代码:
using UnityEngine;
using System.Collections;
public class trailTest : MonoBehaviour {
public float width = 5.0f;
public bool useCurve = true;
private TrailRenderer tr;
public int numberOfObjects = 1;
void Start ()
{
tr = GetComponent<TrailRenderer> ();
tr.material = new Material (Shader.Find ("Sprites/Default"));
}
void Update ()
{
AnimationCurve curve = new AnimationCurve ();
if (useCurve) {
curve.AddKey (0.0f, 0.0f);
curve.AddKey (1.0f, 1.0f);
} else {
curve.AddKey (0.0f, 1.0f);
curve.AddKey (1.0f, 1.0f);
}
float [] spectrum = AudioListener.GetSpectrumData (1024, 0, FFTWindow.Hamming);
for (int i = 0; i < numberOfObjects; i++) {
Vector3 previousScale = tr.transform.localScale;
previousScale.y = Mathf.Lerp (previousScale.y, spectrum [i] * 80, Time.deltaTime * 30);
tr.transform.localScale = previousScale;
}
}
}
【问题讨论】:
-
修改
tr.width不是tr.transform.... -
我可以使用参数
startWidth或endWidth -
这就是我的意思。 XXXWidth 属性。
-
我现在收到错误
Type float does not contain a definition for localScale and no extension method localScale of type float could be found. Are you missing an assembly reference?C# 脚本的新手,所以我正在学习如何调试。感谢您的帮助!