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);**