43 lines
1.5 KiB
C#
43 lines
1.5 KiB
C#
using UnityEngine;
|
|||
|
|
using UnityEngine.UI;
|
||
|
|
|
||
|
|
public class PanelImage : Image
|
||
|
|
{
|
||
|
|
// 覆盖 Raycast 方法
|
||
|
|
public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
|
||
|
|
{
|
||
|
|
// 获取当前Image的RectTransform
|
||
|
|
RectTransform rectTransform = this.rectTransform;
|
||
|
|
|
||
|
|
// 将屏幕坐标转换为Image的局部坐标
|
||
|
|
Vector2 localPoint;
|
||
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPoint, eventCamera, out localPoint);
|
||
|
|
|
||
|
|
// 将局部坐标转换为贴图坐标
|
||
|
|
Vector2 normalizedPoint = new Vector2(
|
||
|
|
(localPoint.x - rectTransform.rect.x) / rectTransform.rect.width,
|
||
|
|
(localPoint.y - rectTransform.rect.y) / rectTransform.rect.height
|
||
|
|
);
|
||
|
|
|
||
|
|
// 确保坐标在0到1之间
|
||
|
|
if (normalizedPoint.x < 0 || normalizedPoint.x > 1 || normalizedPoint.y < 0 || normalizedPoint.y > 1)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取当前图片的Sprite(确保图片是Sprite类型)
|
||
|
|
if (sprite == null || sprite.texture == null)
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 计算在纹理上的像素位置
|
||
|
|
int x = Mathf.FloorToInt(normalizedPoint.x * sprite.texture.width);
|
||
|
|
int y = Mathf.FloorToInt(normalizedPoint.y * sprite.texture.height);
|
||
|
|
|
||
|
|
// 检查该像素是否透明
|
||
|
|
Color pixelColor = sprite.texture.GetPixel(x, y);
|
||
|
|
return pixelColor.a > 0.1f; // 如果透明度小于0.1,视为透明区域
|
||
|
|
}
|
||
|
|
}
|