Exemple #1
0
    // Use this for initialization
    void Start()
    {
        int num = analyzer.numOfBands;

        bars = new EqualizerBar[num];

        columnScale = maxXCoordinate / num;
        barXScale   = columnScale * 0.92f;

        LineRenderer lr = GetComponent <LineRenderer>();

        lr.positionCount = num;
        lr.colorGradient = barColorsGradient;
        //lr.material = new Material (Shader.Find("Particles/Additive"));

        for (int i = 0; i < num; i++)
        {
            GameObject go = GameObject.Instantiate(equalizerBarPrefab) as GameObject;
            go.transform.SetParent(this.transform);
            go.transform.localPosition = Vector3.right * (i - num / 2) * columnScale;
            EqualizerBar bar = go.GetComponent <EqualizerBar>();
            bars[i] = bar;
            //bar.visualizer = visualizer;
            bar.barPrefab = barPrefab;
            //bar.visualizerBand = i;
            bar.barYScale = barYScale;
            bar.barXScale = barXScale;
            bar.baseColor = barColorsGradient.Evaluate(Mathf.InverseLerp(0, num, i));
            float hertz = analyzer.GetBandMidFreq(i);
            if (hertz >= 10000)
            {
                hertz         = hertz / 1000;
                bar.text.text = hertz.ToString("F0") + "k";
            }
            else if (hertz >= 1000)
            {
                hertz         = hertz / 1000;
                bar.text.text = hertz.ToString("F1") + "k";
            }
            else
            {
                bar.text.text = hertz.ToString("F0");
            }

            Vector3 knobPosition = new Vector3((i - num / 2) * columnScale,
                                               barYScale * 7.5f, -7f);
            lr.SetPosition(i, knobPosition);

            GameObject knob = GameObject.Instantiate(knobPrefab) as GameObject;
            knob.transform.SetParent(this.transform);
            knobPosition.z = -0.8f;
            knob.transform.localPosition    = knobPosition;
            knob.GetComponent <EqKnob>().id = i;
        }
        AudioAnalyzer.spectrumBarsUpdated += UpdateVisuals;
    }