using UnityEngine; using System.Collections;public class SoundSwitch : MonoBehaviour {public AudioSource as1;public AudioSource as2;public AudioClip[] a1;// Use this for initializationvoid Start () {//代码关键点1(可选):增加两个AudioSource到GameObjectthis.gameObject.AddComponent("AudioSource");this.gameObject.AddComponent("AudioSource");//代码关键点2:GetComponents方法获得所有该GameObj上的AudioSource对象。这样就可以分别进行控制了。var as_array=this.gameObject.GetComponents(typeof(AudioSource));as1=(AudioSource)as_array[0];as2=(AudioSource)as_array[1];as1.clip=a1[1];as2.clip=a1[2];AudioPlay();}// Update is called once per framevoid Update () {}void AudioPlay(){as2.Play();as1.Play();} }
转载于:https://www.cnblogs.com/crazycowboy/archive/2012/06/28/2567213.html