2016-11-22 153 views
1

我正在使用Unity3d并面临问题。我正在做的是有一个Canvas和帆布有两个按钮上下。当按钮突出显示时,我想播放声音,我用两个按钮都附加了我的脚本,脚本可以正常使用鼠标。但我想用键盘箭头键实现这个机制,然后我使用Button Navigation System并将up按钮作为第一个选定对象的EventSystem脚本。现在看图像按钮突出显示,与键盘一起工作良好。但通过按键导航到next按钮时,声音不会播放。如何解决这个问题?通过键导航时不播放声音

enter image description here

代码:

public class HighLightSound : MonoBehaviour,IPointerEnterHandler 
{ 

    [SerializeField] 
    private AudioSource Source; 


    public void OnPointerEnter(PointerEventData ped) 
    { 
     Source.Play(); 
    } 


} 
+0

是否有场景的AudioListener? AudioSource是否有AudioClip播放?在方法中有断点并调试它时调用事件吗? – Wipster

回答

1

根据统一脚本API,你需要实现ISelectHandler和定义ONSELECT()方法。

https://docs.unity3d.com/ScriptReference/UI.Selectable.OnSelect.html

而你的情况将会给这个:

public class HighLightSound : MonoBehaviour,IPointerEnterHandler, ISelectHandler 
{ 
    [SerializeField] 
    private AudioSource Source; 

    public void OnPointerEnter(PointerEventData ped) 
    { 
     Source.Play(); 
    } 

    public void OnSelect (BaseEventData eventData) 
    { 
     Source.Play(); 
    } 
} 

希望有所帮助。

1

我猜你需要实现ISelectHandler还有:

public class HighLightSound : MonoBehaviour,IPointerEnterHandler, ISelectHandler 
{ 

    [SerializeField] 
    private AudioSource Source; 


    public void OnPointerEnter(PointerEventData ped) 
    { 
     Source.Play(); 
    } 

    public void OnSelect(EventSystems.BaseEventData eventData) 
    { 
     Source.Play(); 
    } 
}