Create Sound.cs Script File, and copy code:
using UnityEngine; using System.Collections; public class Sound : MonoBehaviour { private AudioSource audios; public AudioClip AudioFile; // Use this for initialization void Awake(){ audios = gameObject.AddComponent("AudioSource")as AudioSource; audios.clip = AudioFile; audios.playOnAwake = false; audios.loop = true; } void Start () { } // Update is called once per frame void Update () { if(Input.GetKeyDown(KeyCode.W)){ audio.clip = AudioFile; audio.Play (); } if(Input.GetKeyUp (KeyCode.W)){ audio.clip = AudioFile; audio.Stop(); } } }and put your sound in AudioFile, Like this picture:
Download Sound File:
Walk Sound:
Reference:
Unity API AudioSource: http://docs.unity3d.com/ScriptReference/AudioSource.html
Unity API AudioClip: http://docs.unity3d.com/ScriptReference/AudioClip.html
ps:
in short, we should RTFM <- (wiki)
沒有留言:
張貼留言