Skip to content

Instantly share code, notes, and snippets.

@hisasann
Created January 7, 2014 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hisasann/8297985 to your computer and use it in GitHub Desktop.
Save hisasann/8297985 to your computer and use it in GitHub Desktop.
Unityで音を再生する
using UnityEngine;
using System.Collections;
public class EventObjectSound : MonoBehaviour
{
public AudioClip audioClip;
private AudioSource audioSource;
public void PlaySound ()
{
audioSource.Stop ();
if (audioClip && audioSource) {
audioSource.Play ();
}
}
public void PlaySoundOneShot ()
{
audioSource.Stop ();
if (audioClip && audioSource) {
audioSource.PlayOneShot (audioClip);
}
}
public void StopSound ()
{
audioSource.Stop ();
}
// Use this for initialization
void Start ()
{
audioSource = gameObject.GetComponent<AudioSource> ();
audioSource.clip = audioClip;
}
// Update is called once per frame
void Update ()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment