- Home /
Question by
cicha09 · Apr 18, 2018 at 05:40 PM ·
uieventsystem
EventSystem OnPointerClick - not working
Hello, I have script for changing color of UI Components (for Outline and Text). But my method OnPointerClick works only on objects with Outline.
Event System detect everything correctly.
Here is my code:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class UIController : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
private Text currentButtonText = null;
private Outline currentOutline = null;
public void OnPointerEnter(PointerEventData eventData)
{
if (eventData.pointerCurrentRaycast.gameObject != null)
{
GameObject currentHover = eventData.pointerCurrentRaycast.gameObject;
if (currentHover != null)
{
SetMouseEnterAudio();
if (currentHover.GetComponent<Text>())
{
currentButtonText = currentHover.GetComponent<Text>();
currentButtonText.color = Color.gray;
}
else if (currentHover.GetComponent<Image>())
{
currentOutline = currentHover.GetComponent<Outline>();
currentOutline.effectColor = Color.white;
}
}
}
}
public void OnPointerExit(PointerEventData eventData)
{
if (currentButtonText != null)
{
SetMouseExitAudio();
currentButtonText.color = Color.white;
}
if(currentOutline != null)
{
SetMouseExitAudio();
currentOutline.effectColor = Color.black;
}
}
public void OnPointerClick(PointerEventData eventData)
{
// nothing happens here
Debug.Log("OnPointerClick");
if (eventData.pointerCurrentRaycast.gameObject != null)
{
SetMouseDownAudio();
GameObject currentClicked = eventData.pointerCurrentRaycast.gameObject;
}
}
#region Audio
public void SetMouseEnterAudio()
{
AudioController.instance.SetMouseEvent(AudioController.MouseEvents.enter);
}
public void SetMouseExitAudio()
{
AudioController.instance.SetMouseEvent(AudioController.MouseEvents.exit);
}
public void SetMouseDownAudio()
{
AudioController.instance.SetMouseEvent(AudioController.MouseEvents.down);
}
#endregion
}
And here is my example compnents:
example.png
(75.1 kB)
Comment
Your answer
Follow this Question
Related Questions
Find and set the object of an event type with script? 0 Answers
Is there a way to fire the inspector events from code? 0 Answers
Button.Select(); does not highlight? 12 Answers
How to get the fingerID (Touch) that is pressing a specific UI button. 0 Answers
When i press play the buttons work, however, the buttons dont work on my iphone. 1 Answer