155 lines
3.0 KiB
C#
Raw Normal View History

2025-05-18 01:04:31 +08:00
// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2020-12-15
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "Cha_Sound" company="Kunpo"></copyright>
// <summary></summary>
// ***********************************************************************
#if SDK_WECHAT_WASM
#define PLAY_PLATFORM_SOUND
#endif
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace G
{
/// <summary>
/// 角色声音类(支持3d)
/// </summary>
public class Cha_Sound : MonoBehaviour
{
#region Field
/// <summary>
///
/// </summary>
public string[] sndYells = new string[3];
public string[] sndSkillYells = new string[2];
public string[] sndBlades;
public string sndFootstep;
public string sndGetItem;
public string sndBoom;
public string sndDodge;
public string sndSwing;
public string sndSkillStart;
public string sndRiseAttack;
public string sndBlock;
public string sndBlockBreak;
public string sndDied;
private AudioSource _audioSource;
private readonly Dictionary<string, AudioClip> _audioClips = new Dictionary<string, AudioClip>();
#endregion
#region Method
/// <summary>
///
/// </summary>
public void PlaySoundDodge()
{
PlaySound(sndDodge);
}
public void PlaySoundAttack()
{
int si = Random.Range(0, 5);
if (si < 2)
{
PlaySound(sndYells[si]);
}
}
public void PlayYell(int index)
{
PlaySound(sndYells[index]);
}
public void PlaySkillStart()
{
PlaySoundOneShot(sndSkillStart);
}
public void PlayBoom()
{
PlaySound(sndBoom);
}
public void PlaySound(string name)
{
#if !PLAY_PLATFORM_SOUND
if (_audioClips.TryGetValue(name, out var audioClip))
PlaySound(audioClip);
#else
SoundProxy.PlayFxAsync(name);
#endif
}
private void PlaySound(AudioClip clip)
{
if (_audioSource && clip)
{
_audioSource.clip = clip;
_audioSource.Play();
}
}
public void PlaySoundOneShot(string name)
{
#if !PLAY_PLATFORM_SOUND
if (_audioClips.TryGetValue(name, out var audioClip))
PlaySoundOneShot(audioClip);
#else
SoundProxy.PlayFxAsync(name);
#endif
}
/// <summary>
///
/// </summary>
/// <param name="clip"></param>
private void PlaySoundOneShot(AudioClip clip)
{
if (_audioSource && clip)
{
_audioSource.PlayOneShot(clip);
}
}
#endregion
#region Unity
// Use this for initialization
private void Awake()
{
_audioSource = GetComponent<AudioSource>();
SoundProxy.Instance.SetAudioMixer(_audioSource, SoundProxy.FX_LAYER_INDEX);
}
private IEnumerator Start()
{
#if !PLAY_PLATFORM_SOUND
var handle = AssetProxy.Instance.TryGetTemporaryAssetsAsync<AudioClip>("cha_sound");
yield return handle;
foreach (var clip in handle.Result)
{
_audioClips.Add(clip.name, clip);
}
#else
yield return null;
#endif
}
#endregion
}
}