我想知道如何獲得我的rawImage紋理的像素顏色。統一2D:獲取原始圖像的顏色像素
這是我的場景:
public class ColorPicker : MonoBehaviour, IPointerClickHandler
{
public GameObject Cube;
public void OnPointerClick(PointerEventData eventData)
{
if (Input.GetMouseButtonUp(1))
{
Vector3 localPosition = transform.InverseTransformPoint(eventData.pressPosition);
Color color = (GetComponent<RawImage>().texture as Texture2D).GetPixel(Convert.ToInt32(localPosition.x),
Convert.ToInt32(localPosition.y));
Cube.GetComponent<Renderer>().material.color = color;
}
}
}
正如你所看到的,有了這個代碼,它的工作,但它不是縮減規模。
事實上,我無法達到藍色。我只能得到我的廣場的頂部。
當我點擊右鍵時,你能幫助我如何獲得顏色嗎?
感謝
爲什麼你使用'OnGUI()'? –
正如@ m.rogalski所指出的那樣,您應該使用'Update'來處理這種行爲,因此您不需要在GUI上做任何工作,所以不建議使用'OnGUI'。 – Rob