AudioSource.PlayClipAtPoint 在指定位置播放剪辑

static function PlayClipAtPoint (clip : AudioClip, position : Vector3, volume : float = 1.0F) : void

Description描述

Plays the clip at position. Automatically cleans up the audio source after it has finished playing.

在指定位置播放剪辑。播放完成后自动消除音频源。

The audio source that is playing the sound is returned

正在播放声音的音频源被返回。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public AudioClip clip;
	void Awake() {
		AudioSource.PlayClipAtPoint(clip, new Vector3(5, 1, 2));
	}
}
// Plays the clip at position
//在指定位置播放剪辑
var clip : AudioClip;
AudioSource.PlayClipAtPoint(clip, Vector3 (5, 1, 2));

If you want further control over playback, you can use the following code instead.

如果想进一步控制播放,可以使用下面代码。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public AudioClip theClip;
	AudioSource PlayAudioClip(AudioClip clip, Vector3 position, float volume) {
		GameObject go = new GameObject("One shot audio");
		go.transform.position = position;
		AudioSource source = go.AddComponent<AudioSource>();
		source.clip = clip;
		source.volume = volume;
		source.Play();
		Destroy(go, clip.length);
		return source;
	}
	void Awake() {
		PlayAudioClip(theClip, transform.position, 1);
	}
}
var theClip : AudioClip;

PlayAudioClip(theClip, transform.position, 1);

function PlayAudioClip (clip : AudioClip, position : Vector3, volume : float) {
	var go = new GameObject ("One shot audio");
	go.transform.position = position;
	var source : AudioSource = go.AddComponent (AudioSource);
	source.clip = clip;
	source.volume = volume;
	source.Play ();
	Destroy (go, clip.length);
	return source;
}
最后修改:2011年4月6日 Wednesday 10:44

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。