自学内容网 自学内容网

Unity类银河战士恶魔城学习总结(P154 Audio Distance Limite音频距离限制)

【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili

教程源地址:https://www.udemy.com/course/2d-rpg-alexdev/

本章节给骷髅战士加上了音频,并且限制了产生音效的范围

可爱的骷髅战士

AudioManager.cs

      关键字段

  • AudioManager instance
    实现单例模式,确保全局只有一个 AudioManager 实例。

  • [SerializeField] private AudioSource[] sfx
    存储音效的 AudioSource 数组,每个元素对应一个音效。

  • [SerializeField] private AudioSource[] bgm
    存储背景音乐的 AudioSource 数组,每个元素对应一首背景音乐。

  • public bool playBgm
    控制背景音乐是否播放的开关。

  • private int bgmIndex
    当前播放的背景音乐索引。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : MonoBehaviour
{
    public static AudioManager instance;

    [SerializeField] private float sfxMinimumDistance;//音效最小距离
    [SerializeField] private AudioSource[] sfx;//音效
    [SerializeField] private AudioSource[] bgm;//背景音乐


    public bool playBgm;
    private int bgmIndex;


    private void Awake()
    {
        if(instance != null)
            Destroy(instance.gameObject);
        else
            instance = this;
    }


    private void Update()
    {
        if (!playBgm)
            StopAllBGM();
        else
        {
            if(!bgm[bgmIndex].isPlaying)//如果背景音乐没有播放
                PlayBGM(bgmIndex);
        }
    }


    public void PlaySFX(int _sfxIndex,Transform _source)//播放音效
    {
        if(sfx[_sfxIndex].isPlaying)//如果音效正在播放
            return;


        if(_source!=null && Vector2.Distance(PlayerManager.instance.player.transform.position, _source.position) > sfxMinimumDistance)//距离过远不播放
            return;



        if (_sfxIndex < sfx.Length)
        {
            sfx[_sfxIndex].pitch = Random.Range(.85f, 1.15f);//设置音效的音调
            sfx[_sfxIndex].Play();
        }
    }

    public void StopSFX(int _index) => sfx[_index].Stop();//停止音效


    public void PlayRandomBGM()//播放随机背景音乐
    {
        bgmIndex = Random.Range(0, bgm.Length);
        PlayBGM(bgmIndex);
    }



    public void PlayBGM(int _bgmIndex)//播放背景音乐
    {
        bgmIndex = _bgmIndex;

        StopAllBGM();
        bgm[bgmIndex].Play();
    }


    public void StopAllBGM()
    {
        for (int i = 0; i < bgm.Length; i++)
        {
            bgm[i].Stop();
        }
    }

    
}

PlayerAnimationTriggers.cs

攻击触发的音效

AudioManager.instance.PlaySFX(2,null);//播放音效
using UnityEngine;

public class PlayerAnimationTriggers : MonoBehaviour
{
    private Player player => GetComponentInParent<Player>();

    private void AnimationTrigger()
    {
        player.AnimationTrigger();
    }

    private void AttackTrigger()//攻击触发函数
    {
        AudioManager.instance.PlaySFX(2,null);//播放音效

        Collider2D[] colliders = Physics2D.OverlapCircleAll(player.attackCheck.position, player.attackCheckRadius);//检测区域内的敌人

        foreach (var hit in colliders)//遍历检测到的对象
        {
            if (hit.GetComponent<Enemy>() != null)
            {
                EnemyStats _target = hit.GetComponent<EnemyStats>();//判断是否是敌人

                if (_target != null) //对敌人造成伤害
                    player.stats.DoDamage(_target);


                ItemData_Equipment weaponData = Inventory.instance.GetEquipment(EquipmentType.Weapon);

                if (weaponData != null)//应用武器特殊效果
                    weaponData.Effect(_target.transform);

            }
        }
    }


    private void ThrowSword()
    {
        SkillManager.instance.sword.CreateSword();
    }
}

SkeletonIdleState.cs

怪物的音效

AudioManager.instance.PlaySFX(24,enemy.transform);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SkeletonIdleState : SkeletonGroundedState
{

    public SkeletonIdleState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName, Enemy_Skelteon _enemy) : base(_enemyBase, _stateMachine, _animBoolName, _enemy)
    {
    }

    public override void Enter()
    {
        base.Enter();

        stateTimer = enemy.idleTime;
    }


    public override void Exit()
    {
        base.Exit();

        AudioManager.instance.PlaySFX(24,enemy.transform);
    }

    public override void Update()
    {
        base.Update();

        if (stateTimer < 0)
        {
            stateMachine.ChangeState(enemy.moveState);
        }

    }
}



原文地址:https://blog.csdn.net/suzh1qian/article/details/144157783

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!