unity网络实战开发(丛林战争)-正式开发阶段(018-声音管理器模块的完善)

使用工具:VS2017,unity3d

使用语言:c#

作者:Gemini_xujian

参考:siki老师-《丛林战争》视频教程

上一篇文章中,已经完成了注册事件的处理,接下来将完善声音管理器模块。

01-开发声音管理器

为了使游戏运行起来更加富有活力,接下来,我将为游戏添加音效部分,音效部分由AudioManager进行管理。

首先,将工程中的音效相关的文件放到ReSources目录下,目的是方便我们后面通过方法加载相关的音效;之后,打开AudioManager脚本,定义音效文件的父级目录名字,将所有的音效进行声明。

    public const string path_Prefix = "Sounds/";
    public const string sound_ArrawShoot = "ArrawShoot";
    public const string sound_Bg_fast = "Bg(fast)";
    public const string sound_Bg_moderate = "Bg(moderate)";
    public const string sound_ButtonClick = "ButtonClick";
    public const string sound_Miss = "Miss";
    public const string sound_ShootPerson = "ShootPerson";
    public const string sound_Timer = "Timer";

完成上述操作后,重写Init方法进行初始化相关操作:在方法中,首先创建一个空物体作为音频源管理组件,之后声明两个AudioSource类型的组件,一个是背景音频源,一个是普通音频源,在Init方法中进行赋值,最后需要在方法中调用背景音频源循环播放背景声音,所以在这里,我新创建了一个方法用于声音播放的处理。


    private AudioSource bgAudioSource;
    private AudioSource normalAudioSource;

    public override void OnInit()
    {
        GameObject audioSourceGo = new GameObject("AudioManager");
        bgAudioSource = audioSourceGo.AddComponent<AudioSource>();
        normalAudioSource = audioSourceGo.AddComponent<AudioSource>();
        PlaySound(bgAudioSource, LoadSound(sound_Bg_moderate),0.5f, true);
    }
    public void PlaySound(AudioSource audioSource,AudioClip clip,float volume=1,bool loop=false)
    {
        audioSource.clip = clip;
        audioSource.volume = volume;
        audioSource.loop = loop;
        audioSource.Play();
    }
    private AudioClip LoadSound(string soundName)
    {
        return Resources.Load<AudioClip>(path_Prefix + soundName);
    }

声音播放处理方法中主要完成的是得到相应的音频源,设置音频源播放的音频片段、声音大小、循环模式、播放功能,设置这几个选项即可完成背景声音的播放,在Init方法中进行调用的时候,新定义了一个方法通过音频名来得到相应的音频片段,这就是刚开始将音频文件放到资源文件夹特定目录的原因。

最后,运行unity,音频正常播放,完整代码如下:

AudioManager.cs:

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

public class AudioManager : BaseManager
{
    public AudioManager(GameFacade facade) : base(facade)
    {
    }
    public const string path_Prefix = "Sounds/";
    public const string sound_ArrawShoot = "ArrawShoot";
    public const string sound_Bg_fast = "Bg(fast)";
    public const string sound_Bg_moderate = "Bg(moderate)";
    public const string sound_ButtonClick = "ButtonClick";
    public const string sound_Miss = "Miss";
    public const string sound_ShootPerson = "ShootPerson";
    public const string sound_Timer = "Timer";

    private AudioSource bgAudioSource;
    private AudioSource normalAudioSource;

    public override void OnInit()
    {
        GameObject audioSourceGo = new GameObject("AudioManager");
        bgAudioSource = audioSourceGo.AddComponent<AudioSource>();
        normalAudioSource = audioSourceGo.AddComponent<AudioSource>();
        PlaySound(bgAudioSource, LoadSound(sound_Bg_moderate),0.5f, true);
    }
    public void PlaySound(AudioSource audioSource,AudioClip clip,float volume=1,bool loop=false)
    {
        audioSource.clip = clip;
        audioSource.volume = volume;
        audioSource.loop = loop;
        audioSource.Play();
    }
    private AudioClip LoadSound(string soundName)
    {
        return Resources.Load<AudioClip>(path_Prefix + soundName);
    }
}

02-控制按钮的点击声音的播放

上次完成了对背景声音的播放,接下来,对按钮的点击声音进行处理。

首先,AudioManager中新建两个方法,一个是播放背景声音方法,一个是播放普通声音方法,两个方法都调用了PlaySound方法,因为点击按钮时发出的声音不需要循环播放,所以在设置参数的时候设置loop为false。

    public void PlayBgSound(string soundName)
    {
        PlaySound(bgAudioSource, LoadSound(soundName), 0.5f, true);

    }
    public void PlayNormalSound(string soundName)
    {
        PlaySound(normalAudioSource, LoadSound(soundName));

    }

之后,在GameFacade脚本中,创建两个方法分别调用以上两个方法,方便其他对象使用。

    public void PlayBgSound(string soundName)
    {
        audioMng.PlayBgSound(soundName);
    }
    public void PlayNormalSound(string soundName)
    {
        audioMng.PlayNormalSound(soundName);
    }

接下来,在BasePanel中定义一个按钮点击声音播放的方法,此方法调用中介类GameFacade中的PlayNormalSound方法用于按钮点击声音的播放,但在调用时会发现没有声明Gamefacade对象,所以这时可以仿照BasePanel类中已有的UIManager对象的获取方式获取GameFacade对象,获取到之后即可调用。

UIManager.cs->GetPanel():

            instPanel.GetComponent<BasePanel>().Facade = facade;

BasePanel.cs:

 protected UIManager uiMng;
    protected GameFacade facade;
    public UIManager UIMng
    {
        set
        {
            uiMng = value;
        }
    }
    public GameFacade Facade
    {
        set
        {
            facade = value;
        }
    }
    protected void PlayClickSound()
    {
        facade.PlayNormalSound(AudioManager.sound_ButtonClick);
    }

最后,分别在StartPanel、LoginPanel、RegisterPanel的按钮点击事件中进行调用即可。

LoginPanel.OnCloseClick():

 public void OnCloseClick()
    {
        PlayClickSound();
        transform.DOScale(0, 0.4f);
        transform.DOLocalMove(new Vector3(800,0,0), 0.4f).OnComplete(()=> {               uiMng.PopPanel(); });
    }

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值