1、勾选image的Read/Write这一步是让Cpu得到image数据信息
2、上干货
using UnityEngine;
using UnityEngine.UI;
public class ImageAlpahJudge : MonoBehaviour, ICanvasRaycastFilter
{
[Header("透明度过滤阈值")]
public float alpahThreshold = 0.1f;
Image _image;
void Start()
{
_image = GetComponent<Image>();
}
public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
{
//将选中的点转换为Image区域内的本地点
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(_image.rectTransform, sp, eventCamera, out localPoint);
Vector2 pivot = _image.rectTransform.pivot;
Vector2 normalizedLocal = new Vector2(pivot.x + localPoint.x / _image.rectTransform.rect.width, pivot.y + localPoint.y / _image.rectTransform.rect.height);
Vector2 uv = new Vector2(
_image.sprite.rect.x + normalizedLocal.x * _image.sprite.rect.width,
_image.sprite.rect.y + normalizedLocal.y * _image.sprite.rect.height);
uv.x /= _image.sprite.texture.width;
uv.y /= _image.sprite.texture.height;
//获取指定纹理坐标(u, v)处的像素颜色。它返回一个Color结构,其中包含红、绿、蓝和alpha通道的值。
//Color c = _image.sprite.texture.GetPixel((int)uv.x, (int)uv.y);
//用于在纹理上执行双线性插值以获取像素颜色值,这个方法使用双线性插值算法来估算纹理中某个位置的颜色,而不是直接从纹理的像素中读取颜色。
Color c = _image.sprite.texture.GetPixelBilinear(uv.x, uv.y);
var b = c.a > alpahThreshold;
Debug.Log(b);
return b;
}
void Update()
{
}
}