using UnityEngine; using System.Collections; public class audioTest : MonoBehaviour { public float deep; public AudioSource audio1; public GameObject a; public Material Myobj; // Use this for initialization void Start () { } // Update is called once per frame void Update () { //建立音谱数组,后面的三个参数我前面已经介绍过,这里是具体用法 float[] spectrum = audio1.GetSpectrumData(256, 0, FFTWindow.Hamming); int i = 1; //我们控制物体所用到的数值 float heght = 0; //以下内容是在scence中现实音谱内容 while (i < 255) { Debug.DrawLine(new Vector3(i - 1, spectrum[i] + 10, 0), new Vector3(i, spectrum[i + 1] + 10, 0), Color.red); Debug.DrawLine(new Vector3(i - 1, Mathf.Log(spectrum[i - 1]) + 10, 2), new Vector3(i, Mathf.Log(spectrum[i]) + 10, 2), Color.cyan); Debug.DrawLine(new Vector3(Mathf.Log(i - 1), spectrum[i - 1] - 10, 1), new Vector3(Mathf.Log(i), spectrum[i] - 10, 1), Color.green); Debug.DrawLine(new Vector3(Mathf.Log(i - 1), Mathf.Log(spectrum[i - 1]), 3), new Vector3(Mathf.Log(i), Mathf.Log(spectrum[i]), 3), Color.yellow); heght = heght + spectrum[i]; i++; } //经过本人测试heght值最多为1点几,所以为了表示明显,我*10,但由于会有0影响效果,所以+1 heght = heght * 10 + 1; //利用iTween插件变换图形,关于iTween我稍后做讲述 //iTween.ScaleTo(a, new Vector3(0.4f,heght*10-deep,0.2f), 0f); this.gameObject.transform.localScale = new Vector3 (0.1f, heght-deep, 0.1f); print(heght); if ((heght*10)>15f){ iTween.ColorTo(a, new Color(0, -heght, 256 - heght), 0f); } else if((heght*10) |