[英]How to cache this in C#?
I'am new in C#.我是 C# 的新手。 I Really wanted to cache this since i need to call it on Update method.
我真的很想缓存它,因为我需要在 Update 方法上调用它。 Its create a lot of garbage collection.
它创建了大量的垃圾收集。
private bool IsPointerOverUIObject() {
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
return results.Count > 0;
}
Caching the result is probably not the right solution here - caching information dependent on mouse/touch pointer location means your game might produce unexpected/delayred results when the user moves their finger.在这里缓存结果可能不是正确的解决方案 - 缓存依赖于鼠标/触摸指针位置的信息意味着您的游戏可能会在用户移动手指时产生意外/延迟的结果。
From your comments:从您的评论中:
[I] wanted to make mobile game and make UI ignore click
[我]想做手游,让UI忽略点击
In that case, you can probably reduce computation overhead most effectively by moving the IsPointerOverUIObject()
call into the event trigger for the relevant click/touch event - there's no need to check where a click or touch release occurs unless someone is actually touching the screen.在这种情况下,您可以通过将
IsPointerOverUIObject()
调用移动到相关单击/触摸事件的事件触发器中来最有效地减少计算开销 - 除非有人实际触摸屏幕,否则无需检查单击或触摸释放发生的位置.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.