JavaScript => public function GetData(data: float[], offsetSamples: int): bool;
C# => public bool GetData(float[] data, int offsetSamples);
Description 描述
Fills an array with sample data from the clip.
该剪辑的采样数据数据。
The samples are floats ranging from -1.0f to 1.0f. The sample count is determined by the length of the float array. Use offsetSamples to start the read from a random position in the clip. If the read length from the offset is longer than the clip length, the read will wrap around and read the remaining samples from the start of the clip.
采样的浮点数范围是-1.0 ~ 1.0,采样数由浮点数数组的长度确定。使用offsetSamples从剪辑的随机位置开始读。如果从偏移值读取的长度比剪辑长,读取将环绕从剪辑开始读取剩余采样。
Note that with compressed audio files, the sample data can only be retrieved when the Load Type is set to Decompress on Load in the audio importer. If this is not the case then the array will be returned with zeroes for all the sample values.
注意,压缩的音频文件,当加载类型在音频导入器设置为加载时解压缩时,采样数据只能被读取。如果这不是这种情况,那么数组将返回所有采样值为零。
JavaScript:
// Read all the samples from the clip, reducing their gain by half
// as we go along.
function Start () {
var aud = GetComponent.();
var samples = new float[aud.clip.samples * aud.clip.channels];
aud.clip.GetData(samples, 0);
for (var i = 0; i < samples.Length; ++i)
samples[i] = samples[i] * 0.5f;
aud.clip.SetData(samples, 0);
}
C#:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Start() {
AudioSource aud = GetComponent();
float[] samples = new float[aud.clip.samples * aud.clip.channels];
aud.clip.GetData(samples, 0);
int i = 0;
while (i < samples.Length) {
samples[i] = samples[i] * 0.5F;
++i;
}
aud.clip.SetData(samples, 0);
}
}