/// <summary>/// 获取屏幕点击贴图像素位置/// 点击屏幕 点击到图片,计算图片的像素坐标(图片的屏幕像素范围(宽高),屏幕坐标到图片像素坐标的转换) 获取颜色/// </summary>/// <param name="screenPosition">屏幕坐标</param>/// <param name="rectT">图片位置</param>/// <param name="texture">Image 中的贴图</param>/// <returns>贴图像素 uv 值</returns>private Vector2 ReadScreenPositionTextureColor(Vector2 screenPosition, RectTransform rectT, Texture2D texture){float width = rectT.rect.width * rectT.localScale.x;float heigth = rectT.rect.height * rectT.localScale.y;//UI空间坐标到屏幕坐标的转换//图片范围Vector2 v0 = new Vector2(rectT.localPosition.x + (Screen.width - width) / 2, rectT.localPosition.y + (Screen.height - heigth) / 2);Vector2 v1 = new Vector2(rectT.localPosition.x + (Screen.width + width) / 2, rectT.localPosition.y + (Screen.height + heigth) / 2);if (screenPosition.x < v1.x && screenPosition.x > v0.x && screenPosition.y < v1.y && screenPosition.y > v0.y){//点击到图片区域float x = (screenPosition.x - v0.x) / width * texture.width;float y = (screenPosition.y - v0.y) / heigth * texture.height;return new Vector2(x, y);}else{Debug.Log("点击到图片之外");return new Vector2(0, 0);}}
使用:
public Texture2D main_tex;
public Image image;void Start(){image.sprite = Sprite.Create(main_tex, new Rect(0, 0, main_tex.width, main_tex.height), Vector2.zero);} private void Update(){if (Input.GetMouseButtonDown(0)){Vector2 vOut = ReadScreenPositionTextureColor(Input.mousePosition, image.GetComponent<RectTransform>(), main_tex);Color colorOut = rgb_tex.GetPixel((int)vOut.x, (int)vOut.y);Debug.Log(colorOut);}}