#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
#if UNITY_EDITOR
[ExecuteInEditMode]
#endif
[AddComponentMenu("")]
public class SfxrAudioPlayer : MonoBehaviour {
// Properties
private bool isDestroyed = false; // If true, this instance has been destroyed and shouldn't do anything yes
private bool needsToDestroy = false; // If true, it has been scheduled for destruction (from outside the main thread)
private bool runningInEditMode = false; // If true, it is running from the editor and NOT playing
// Instances
private SfxrSynth sfxrSynth; // SfxrSynth instance that will generate the audio samples used by this
// ================================================================================================================
// INTERNAL INTERFACE ---------------------------------------------------------------------------------------------
// INTERNAL INTERFACE ---------------------------------------------------------------------------------------------
void Start() {
// Creates an empty audio source so this GameObject can receive audio events
AudioSource soundSource = gameObject.AddComponent<AudioSource>();
soundSource.clip = new AudioClip(); //报错!!!
soundSource.volume = 1f;
soundSource.pitch = 1f;
soundSource.priority = 128;
soundSource.Play();
}
void Update() {
// Destroys self in case it has been queued for deletion
if (sfxrSynth == null) {
// Rogue object (leftover)
// When switching between play and edit mode while the sound is playing, the object is restarted
// So, queues for destruction
needsToDestroy = true;
}
if (needsToDestroy) {
needsToDestroy = false;
Destroy();
}
}
void OnAudioFilterRead(float[] __data, int __channels) {
// Requests the generation of the needed audio data from SfxrSynth
if (!isDestroyed && !needsToDestroy && sfxrSynth != null) {
bool hasMoreSamples = sfxrSynth.GenerateAudioFilterData(__data, __channels);
// If no more samples are needed, there's no more need for this GameObject so schedule a destruction (cannot do this in this thread)
if (!hasMoreSamples) {
needsToDestroy = true;
if (runningInEditMode) {
// When running in edit mode, Update() is not called on every frame
// We can't call Destroy() directly either, since Destroy() must be ran from the main thread
// So we just attach our Update() to the editor's update event
#if UNITY_EDITOR
EditorApplication.update += Update;
#endif
}
}
}
}
// ================================================================================================================
// PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
public void SetSfxrSynth(SfxrSynth __sfxrSynth) {
// Sets the SfxrSynth instance that will generate the audio samples used by this
sfxrSynth = __sfxrSynth;
}
public void SetRunningInEditMode(bool __runningInEditMode) {
// Sets the SfxrSynth instance that will generate the audio samples used by this
runningInEditMode = __runningInEditMode;
}
public void Destroy() {
// Stops audio immediately and destroys self
if (!isDestroyed) {
isDestroyed = true;
sfxrSynth = null;
if (runningInEditMode || !Application.isPlaying) {
// Since we're running in the editor, we need to remove the update event, AND destroy immediately
#if UNITY_EDITOR
EditorApplication.update -= Update;
#endif
UnityEngine.Object.DestroyImmediate(gameObject);
} else {
UnityEngine.Object.Destroy(gameObject);
}
}
}
}
“AudioClip”不包含采用 0 个参数的构造函数
我试过查找解决办法,我找到的说:
在AudioClip类中添加以下内容: public AudioClip(){}
但是我不知道放哪里
我真的很菜,救命
你可以定义一个public AudioClip clip;在脚本的面板里将一段音频剪辑拖拽赋值,在你的开始方法里改为audiosouce. clip=cilp;就可以播放音频了
AudioClip是Unity中的音频资产,不是通过new创建出来的