- Home /
EventTrigger PointerEnter
Hi Guys, I have problem with Pointer Event Trigger - Pointer Enter. I have created from script Text, i want to change text color when I touch it with the mouse. But when I touch it function OnPointerEnter was not called. Only when I click on the text it's change color..
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
public class ComboBoxText : MonoBehaviour
{
public GameObject _current;
public GameObject _go;
public RectTransform _rt;
public Text _text;
public EventTrigger _et;
public EventTrigger.TriggerEvent _trig;
public EventTrigger.Entry _entry;
public ComboBoxText(GameObject _current, Font _font, int _fontSize)
{
_go = new GameObject();
_go.name = "Tested GameObject";
_go.transform.parent = _current.transform;
_go.AddComponent<RectTransform>();
_rt = _go.GetComponent<RectTransform>();
_rt.anchoredPosition3D = new Vector3(0f, 0f, 0f);
_rt.pivot = new Vector2(0f, 1f);
_rt.sizeDelta = new Vector2(130, 30);
_rt.localPosition = new Vector3(0f, 0f, 0f);
_rt.localScale = new Vector3(1f, 1f, 1f);
_rt.anchoredPosition3D = new Vector3(0f, 0f, 0f);
_rt.pivot = new Vector2(0f, 1f);
_rt.localScale = new Vector3(1f, 1f, 1f);
_go.AddComponent<Text>();
_text = _go.GetComponent<Text>();
_text.text = "TestText";
_text.font = _font;
_text.fontSize = _fontSize;
_text.color = new Color32(200, 200, 200, 255);
_text.alignment = TextAnchor.MiddleLeft;
_et = _go.GetComponent<EventTrigger>();
if (_et == null) { _et = _go.AddComponent<EventTrigger>(); }
_trig = new EventTrigger.TriggerEvent();
_trig.AddListener((data) => OnPointerEnter());
_entry = new EventTrigger.Entry() { callback = _trig, eventID = EventTriggerType.PointerEnter };
_et.delegates = new List<EventTrigger.Entry>();
_et.delegates.Add(_entry);
}
public void OnPointerEnter()
{
Debug.Log("OnPointerEnter");
_text.color = new Color32(200, 200, 200, 255);
}
}
Creating was called from another script:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
public class UI_ComboBox :
{
public GameObject _go;
public Font _font;
public int _fontSize;
private RectTransform _rt;
private ComboBoxText _mainText;
private uint _CurrentItemNum = 0;
void Start () {
_rt = _go.GetComponent<RectTransform>();
_mainText = new ComboBoxText(_go, _font, _fontSize);
_mainText._go.name = _go.name + "_MainText";
_mainText._rt.sizeDelta = new Vector2(_rt.sizeDelta.x, _rt.sizeDelta.y);
_mainText._rt.anchoredPosition3D = new Vector3(0f, 0f, 0f);
_mainText._text.text = " MainText";
}
}
Answer by Numli · Sep 09, 2015 at 09:58 AM
You need to include the interface reference to your class and also onPointerEnter requires a parameter of PointerEventData.
See http://docs.unity3d.com/ScriptReference/UI.Selectable.OnPointerEnter.html for a simple example.
I tried, but if I for example, create five ComboBoxText, and touch one of five, event OnPointerEnter() called on all five ComboBoxText... But code is true, except PointerEnter EventTrigger.. for example if I change it on PointerDown or PointerExit, it works well. But PointerEnter has call only when I click on object..