您当前的位置: 首页 >  苍狼王unity学院 c#

自己的C#框架之第四篇之声音的播放。

苍狼王unity学院 发布时间:2019-03-09 16:08:16 ,浏览量:5

1、新建类ProjectEvent,枚举消息。
public class ProjectEvent {
public enum UiSound
{
//鼠标移动播放的声音。
pointerEnterSound=ManagerID.ProjectManager+1,
//鼠标按下的声音
PointDownSound,
PlayNeedSound,
MaxValue
}
}

2、新建类ProjectBehaviour.CS,接收消息,监听消息。
public class ProjectBehaviour : ProjectBase {
//接收消息
private void Awake()
{
msgIds = new ushort[]
{
(ushort )ProjectEvent .UiSound.PlayNeedSound,
(ushort )ProjectEvent.UiSound.pointerEnterSound,
(ushort )ProjectEvent.UiSound.PointDownSound,
(ushort)ProjectEvent.UiSound.MaxValue,
};
RegistSelf(this,msgIds);
}
//监听消息
public override void ProcessEvent(MsgBase tmpMsg)
{
switch (tmpMsg.msgId)
{
case (ushort)ProjectEvent.UiSound.pointerEnterSound:
PlaypointerEnterSound();
break;
case (ushort)ProjectEvent.UiSound.PointDownSound:
PlayPointDownSound();
break;
default:
break;
}
}
//鼠标点击确认的声音。
private void PlayPointDownSound()
{
PlaySound(“OnClick”);
}
//鼠标移动上面的声音。
private void PlaypointerEnterSound()
{
PlaySound(“OnHover”);
}
//播放声音。
private void PlaySound(string audioClipName)
{
AudioClip audio = Resources.Load(“Audio/语音2019.3.5/” + audioClipName) as AudioClip;
GameObject manager = UIManager.Instance.GetGameObject(“manager”);
if (audionull|| managernull)
{
Debug.Log(“加载失败”);
return;
}
else
{
//没有AudioSource组件的话,创建一个AudioSource。
manager.GetOrCreatComponent();
manager.GetComponent().clip = audio;
}
if (!manager.GetComponent().isPlaying)
{
manager.GetComponent().Play();
}
}
3、发消息。
//播放鼠标点击声音。
GameObject makeSureButton = UIManager.Instance.GetGameObject(“jiaoXueButton”);
makeSureButton.GetComponent().AddButtonListener(PlaySound);
SendMsg(new MsgBase((ushort )ProjectEvent.UiSound.PointDownSound));

     //播放鼠标在物体之上的声音
    SendMsg(new MsgBase((ushort )ProjectEvent.UiSound.pointerEnterSound));
     GameObject makeSureButton = UIManager.Instance.GetGameObject("jiaoXueButton");
    **makeSureButton.GetComponent().zhPointEnter.AddListener(PlayPointEnter);**
关注
打赏
查看更多评论

苍狼王unity学院

暂无认证

  • 5浏览

    0关注

    252博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录