private Renderer IsHit(Transform _transform) { TilemapRenderer tilemapRenderer = _transform.GetComponent <TilemapRenderer>(); if (tilemapRenderer) { return(tilemapRenderer); } return(MouseUtils.IsPixelHit(_transform)); }
/// <summary> /// Gets the renderers under the mouse, ordered so that highest item comes first /// </summary> /// <param name="layerMask">layers to check for hits in</param> /// <param name="gameObjectFilter"> optional filter to apply to each game object before sorting, the final list will only /// include spriterenderers whose game objects match the condition</param> /// <returns>the ordered sprite renderers that were under the mouse, top first</returns> public static IOrderedEnumerable <SpriteRenderer> GetOrderedObjectsUnderMouse(LayerMask layerMask, Func <GameObject, bool> gameObjectFilter = null) { var result = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(CommonInput.mousePosition), Vector2.zero, 10f, layerMask) //get the hit game object .Select(hit => hit.collider.transform.gameObject); if (gameObjectFilter != null) { //apply the GO filter result = result.Where(gameObjectFilter); } return(result //check for a pixel hit .Select(go => MouseUtils.IsPixelHit(go.transform)) .Where(r => r != null) //order by sort layer .OrderByDescending(r => SortingLayer.GetLayerValueFromID(r.sortingLayerID)) //then by sort order .ThenByDescending(renderer => renderer.sortingOrder)); }