unity AudioSource 소리 재생 후 실행 할 함수 나 조건 동작

소리 가 재생 되 었 는 지 판단 할 물체 에 스 크 립 트 를 걸 어 놓 습 니 다.

using System.Collections;
using UnityEngine;
using UnityEngine.Events;
[RequireComponent(typeof(AudioSource))]
public class AudioManager : MonoBehaviour
{
public static AudioManager instence = null;
private AudioSource _audio;
void Awake()
{
if (instence == null)
{
instence = this;
}
}
void Start()
{
    _audio = GetComponent<AudioSource>();
  
}
void Update()
{
//      A         
if (Input.GetKeyDown(KeyCode.A))
{
PlayAudio(GameObject.GetComponent().clip)
}
}
//             
public void PlayAudio(AudioClip clip, UnityAction callback = null, bool isLoop = false)
{
//                   
_audio.clip = clip;
_audio.loop = isLoop;
_audio.Play();
//             
StartCoroutine(AudioPlayFinished(_audio.clip.length, callback));
}
//             
private IEnumerator AudioPlayFinished(float time, UnityAction callback)
{   
    yield return new WaitForSeconds(time);
    //                
    # region               
    print("      ,      "); 
    #endregion
}
}
유 니 티 의 오디 오 소스 가 끝 난 감청
최근 오디 오 종료 후 호출 문제 가 발생 했 습 니 다.유 니 티 원생 의 오디 오 구성 요소 인 AudioSourse 는 기능 이 없어 서 직접 썼 습 니 다.
다음은 코드:

using Assets.Scripts.Entities;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine; 
public class AudioSourceInfo
{
    private float playTime = 0;
    public AudioSource AudioSource { get; private set; }
    public AudioState AudioState = AudioState.Idle;
    public Action AfterPlaying { get; set; }
    public int ID = 0;
    public AudioSourceInfo(GameObject go)
    {
        this.AudioSource = go.AddComponent<AudioSource>();
    }
    public AudioClip Clip
    {
        get
        {
            return this.AudioSource.clip;
        }
        set
        {
            this.AudioSource.clip = value;
            playTime = 0;
        }
    }
    public bool Loop
    {
        get
        {
            return this.AudioSource.loop ;
        }
        set
        {
            this.AudioSource.loop = value;
        }
    }
    public float Volume
    {
        get
        {
            return this.AudioSource.volume;
        }
        set
        {
            this.AudioSource.volume = value;
        }
    }
    public void Play()
    {
        if (null == this.AudioSource)
        {
            return;
        }
        this.AudioState = AudioState.IsPlaying;
        this.AudioSource.Play();
    }
    public void Pause()
    {
        if (null == this.AudioSource)
        {
            return;
        }
        if(this.AudioSource.isPlaying)
        {
            this.AudioState = AudioState.Pause;
            this.AudioSource.Pause();
        }
    }
    public void Stop()
    {
        if (null == this.AudioSource)
        {
            return;
        }
        this.AudioState = AudioState.Stop;
        this.AudioSource.Stop();
        if(AfterPlaying!= null)
        {
            this.AfterPlaying();
        }
    }
    private void Update()
    {
        if (this.AudioSource != null &&  this.AudioSource.clip!= null && this.AudioState == AudioState.IsPlaying)
        {
            playTime += Time.fixedDeltaTime;
            if (playTime >= this.Clip.length)
            {
                playTime = 0;
                this.Stop();
            }
        }
    }
} 
public enum AudioState
{
    Idle,
    IsPlaying,
    Pause,
    Stop,
}
 
추가:Unity3d AudioSource 는 재생 을 어떻게 감청 하고 논리 적 으로 처리 합 니까?
AudioSource 가 언제 재생 이 완료 되 고 관련 논 리 를 처리 하 는 지 알 고 싶 습 니 다.예 를 들 어 곡 전환,유 니 티 는 해당 하 는 이 벤트 를 제공 하지 않 았 습 니 다.
그래서 다음 과 같은 몇 가지 방안 을 생각 했다.
1.업데이트 시 판단 isPlaying
2.오디 오 재생 길이 가 져 오기,Invoke
나중에 api 를 봤 을 때 갑자기 협 정 을 사용 할 수 있다 는 생각 이 들 었 어 요.원리 가 Invoke 와 마찬가지 로 이것 이 가장 좋 은 방안 일 것 이다.

그러나 오디 오 가 멈 춘 후에 도 협 정 함 수 를 업데이트 하지 않 으 면 문제 가 생 긴 다.그 러 니까 멈 출 때 협 정 함 수 를 업데이트 하 세 요.
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.만약 잘못 이 있 거나 완전히 고려 하지 않 은 부분 이 있다 면 아낌없이 가르침 을 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기