PlaySounds.cs 896 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlaySounds : MonoBehaviour
  5. {
  6. private AudioSource sound;
  7. public string soundName;
  8. private float isDestroyTimer;
  9. void Start()
  10. {
  11. // isLoopSound = false;
  12. sound = this.GetComponent<AudioSource>();
  13. PlaySound();
  14. }
  15. void Update()
  16. {
  17. //isDestroyTimer -= Time.deltaTime;
  18. //if (isDestroyTimer <= 0)
  19. //{
  20. // //Destroy(this.gameObject);
  21. //}
  22. }
  23. // Update is called once per frame
  24. public void PlaySound()
  25. {
  26. if (sound != null)
  27. {
  28. sound.clip = (AudioClip)Resources.Load(soundName, typeof(AudioClip));//调用Resources方法加载AudioClip资源
  29. sound.Play();
  30. // isDestroyTimer = sound.clip.length;
  31. }
  32. }
  33. }