GetSpectrumData 사운드에 맞춰 게임오브젝트의 형태 변환

using UnityEngine;
using System.Collections;

public class soundMag : MonoBehaviour {
 
 
    AudioSource audio;
    float[] spectrum = new float[64];
    public GameObject cube;
    
 
    void Start () {

        audio = GetComponent<AudioSource>();
 
 }
 
 void Update () {
        MusicVisualization();
 
    }

    
    void MusicVisualization()
    {
        audio.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
        cube.transform.localScale = new Vector3(1, spectrum[1], 1);
    }
}


https://www.youtube.com/watch?v=swzY1CHWcgk


// 스펙트럼 데이터 값을 쪼개어 넣기

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class soundMag : MonoBehaviour {
 
    AudioSource audio;
    float[] spectrum = new float[2048];
    public GameObject cube;
    List<GameObject> boxs = new List<GameObject>();
 
    void Start () {

        audio = GetComponent<AudioSource>();
        for(int i = 0; i < spectrum.Length; i++)
        {
            GameObject cub = Instantiate(cube) as GameObject;
            cub.transform.position = new Vector3(0 + (i+0.1f), 0, 0);
            boxs.Add(cub);
        }
 }
 
 void Update () {
        MusicVisualization();
    }

    void MusicVisualization()
    {
        //float[] spectrum 배열에 SpectrumData 값을 대입한다.
        audio.GetSpectrumData(spectrum, 0, FFTWindow.Triangle);
        //cube.transform.localScale = new Vector3(1, spectrum[1], 1);
        for(int i = 0; i < boxs.Count; i++)
        {
            boxs[i].transform.localScale = new Vector3(1, spectrum[i] * 70, 1);
        }
 
    }
}



https://youtu.be/bpuWtZlRTGQ

댓글

이 블로그의 인기 게시물

날짜 시간 시간차 시간 계산 하기

코루틴에서 CallBack 함수 적용하기

C++ 언어 퍼센트 구하는 방법 / 기본 언어 퍼센트 구하는 방법