简体   繁体   中英

How to cache this in C#?

I'am new in C#. I Really wanted to cache this since i need to call it on Update method. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM