Unity 中有沒有辦法將腳本設定為音頻源?我有一個產生聲音的腳本,我現在想可視化它,但是使用 GetComponent 我只能得到一個空值。
我已經嘗試過使用 GetComponent,但為此我需要將腳本設定為源,并且它已經與 AudioListeners 一起使用,但我需要 AudioSource 來可視化它。我使用了幾個在線教程,但沒有人使用腳本作為源。
我只能看到一個我想避免的解決方案,它將來自 AudioSource 的資料寫入檔案,并以這種方式生成可視化。
編輯:我已經嘗試過 DareerAhmadMufti 的建議:

可悲的是它仍然不起作用。
public class AudioGaudioGenerator : MonoBehaviour
{
private Graph graph;
public AudioSource _audioSource;
float[] farr = new float[4096];
void Start()
{
graph = this.GetComponentInChildren<Graph>();
_audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
GetSpectrumAudioSource();
if (graph.showWindow0)
{
graph.SetValues(farr);
}
}
void GetSpectrumAudioSource()
{
_audioSource.GetOutputData(farr, 0);
}
這是 AudioGenerator 的腳本
uj5u.com熱心網友回復:
您不能將腳本用作 AudioSource。已經有一個 AudioSource 腳本。如果物件包含 AudioSource 腳本,那么您可以使用GetComponent<AudioSource>(). 但它必須已經在物件上。
uj5u.com熱心網友回復:
你的問題很模棱兩可。您不能將自定義腳本用作AudioSource,因為它是一個密封類。
如果要獲取 audioClip 或 audioSource 的訪問資料,請將 AudioSource 組件添加到附加腳本的同一物件中。喜歡:

在您的腳本中創建一個公共 AudioSource 變數并分配 AudioSource 組件,如上面的螢屏截圖所示。通過這種方式,您將能夠通過該變數訪問自定義腳本中所需的資料。
uj5u.com熱心網友回復:
您想使用腳本進行音頻可視化,但您只能使用“GetComponent”方法獲得空值。您可以在場景中創建一個位置坐標為 (0,0,0) 的空物件并將其重命名為 Audio Visualization,并為其添加 LineRenderer 和 AudioSource 組件。創建一個紅色著色器并將其分配給 LineRenderer。

創建一個立方體,然后創建一個綠色著色器并將其分配給立方體,將其拖到預制立方體中。
撰寫腳本:
public class AudioVisualization : MonoBehaviour
{
AudioSource audio;//Sound source
float[] samples = new float[128];//The length of the array to store the spectrum data
LineRenderer linerenderer;//Draw line
public GameObject cube;//cube prefab
Transform[] cubeTransform;//Position of cube prefab
Vector3 cubePos;//The middle position, used to compare the cube position with the spectral data of this frame
// Use this for initialization
void Start()
{
GameObject tempCube;
audio = GetComponent<AudioSource>();//Get the sound source component
linerenderer = GetComponent<LineRenderer>();//Get the line drawing component
linerenderer.positionCount = samples.Length;//Set the number of segments of the line segment
cubeTransform = new Transform[samples.Length];//Set the length of the array
//Move the gameobject mounted by the script to the left, so that the center of the generated object is facing the camera
transform.position = new Vector3(-samples.Length * 0.5f, transform.position.y, transform.position.z);
//Generate the cube, pass its position information into the cubeTransform array, and set it as the child object of the gameobject mounted by the script
for (int i = 0; i < samples.Length; i )
{
tempCube=Instantiate(cube,new Vector3(transform.position.x i,transform.position.y,transform.position.z),Quaternion.identity);
cubeTransform[i] = tempCube.transform;
cubeTransform[i].parent = transform;
}
}
// Update is called once per frame
void Update()
{
//get spectrum
audio.GetSpectrumData(samples, 0, FFTWindow.BlackmanHarris);
//cycle
for (int i = 0; i < samples.Length; i )
{
//Set the y value of the middle position according to the spectrum data, and set the x and z values ??according to the position of the corresponding cubeTransform
//Use Mathf.Clamp to limit the y of the middle position to a certain range to avoid being too large
//The more backward the spectrum is, the smaller
cubePos.Set(cubeTransform[i].position.x, Mathf.Clamp(samples[i] * ( 50 i * i*0.5f), 0, 100), cubeTransform[i].position.z);
//Draw a line, in order to prevent the line from overlapping with the cube, the height is reduced by one
linerenderer.SetPosition(i, cubePos-Vector3.up);
//When the y value of the cube is less than the y value of the intermediate position cubePos, the position of the cube becomes the position of the cubePos
if (cubeTransform[i].position.y < cubePos.y)
{
cubeTransform[i].position = cubePos;
}
//When the y value of the cube is greater than the y value of the cubePos in the middle position, the position of the cube slowly falls down
else if (cubeTransform[i].position.y > cubePos.y)
{
cubeTransform[i].position -= new Vector3(0, 0.5f, 0);
}
}
}
}
將腳本掛在音頻可視化上并將預制立方體分配給腳本。匯入音頻資源并將音頻資源分配給 audiosouce 組件。播放效果如下

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/484608.html
上一篇:自定義PRINT陳述句
下一篇:啟用的計時器不打勾
