未调用OnPointerEnter [英] OnPointerEnter is not being called
问题描述
当我盯着按钮时,我正在尝试对按钮进行动画处理.我得到了以下代码,我在其中从球体进行了Raycast查找它所命中的按钮.
I'm trying to animate the button while it is being gazed on. I have got the following code in which I Raycast from a sphere to find the button that it hits.
var eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = screenPosition;
var results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
foreach (var result in results)
{
Debug.Log(result.gameObject.name);
}
为了使按钮动起来,我在按钮上添加了以下代码.不幸的是,OnPointerEnter
或``OnPointerExit从未被调用.
In order to animate the button, I'm adding the following code to the button. Unfortunately, the OnPointerEnter
or ``OnPointerExit is never being called.
[RequireComponent(typeof(Button))]
public class InteractiveItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public Image progressImage;
public bool isEntered = false;
RectTransform rt;
Button _button;
float timeElapsed;
Image cursor;
float GazeActivationTime = 5;
// Use this for initialization
void Awake()
{
_button = GetComponent<Button>();
rt = GetComponent<RectTransform>();
}
void Update()
{
if (isEntered)
{
timeElapsed += Time.deltaTime;
progressImage.fillAmount = Mathf.Clamp(timeElapsed / GazeActivationTime, 0, 1);
if (timeElapsed >= GazeActivationTime)
{
timeElapsed = 0;
_button.onClick.Invoke();
progressImage.fillAmount = 0;
isEntered = false;
}
}
else
{
timeElapsed = 0;
}
}
#region IPointerEnterHandler implementation
public void OnPointerEnter(PointerEventData eventData)
{
CodelabUtils._ShowAndroidToastMessage("entered");
isEntered = true;
}
#endregion
#region IPointerExitHandler implementation
public void OnPointerExit(PointerEventData eventData)
{
CodelabUtils._ShowAndroidToastMessage("exit");
isEntered = false;
progressImage.fillAmount = 0;
}
#endregion
}
我想念什么吗?还是有其他方法可以实现这一目标?
am I missing something ? or is there any other way of achieving this?
推荐答案
我想您的Raycast应该在所有结果上触发OnPointerEnter
.
I guess your Raycast supposed to trigger the OnPointerEnter
on all results.
您将需要使用 ExecuteEvents.Execute
例如
You will need to use ExecuteEvents.Execute
like e.g.
ExecuteEvents.Execute(result.gameObject, eventDataCurrentPosition, ExecuteEvents.pointerEnterHandler);
并且在某些时候还必须调用pointerExitHandler
.
And will also at some point have to invoke pointerExitHandler
.
因此,我将像这样存储HashSet
I would therefore store a HashSet like
using System.Linq;
private HashSet<GameObject> previousEnters = new HashSet<GameObject>();
...
foreach (var result in results.Select(r => r.gameObject))
{
Debug.Log(result.name);
ExecuteEvents.Execute(result, eventDataCurrentPosition, ExecuteEvents.pointerEnterHandler);
// Store the item so you can later invoke exit on them
if(!previousEnters.Contains(result)) previousEnters.Add(result);
}
// This uses Linq in order to get only those entries from previousEnters that are not in the results
var exits = previousEnters.Except(results);
foreach(var item in exits)
{
if(item) ExecuteEvents.Execute(item, eventDataCurrentPosition, ExecuteEvents.pointerExitHandler);
}
您实际上可能想要实现自己的自定义 PointerInputModule
.
You might actually want to implement your own custom PointerInputModule
.
作为入门者,您也可以使用我的答案来回答使用Raycast而不是凝视指针基于Steam的VR Laserpointer的脚本,它可以与3D对象和UI元素进行交互.
As a starter you could also use my answer to Using Raycast instead of Gaze Pointer from a while ago where I created a script based on Steam's VR Laserpointer which allows to interact with 3D objects and UI elements.
注意:在智能手机上输入文字,但我希望这个想法会清楚
这篇关于未调用OnPointerEnter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!