using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ColorTest : MonoBehaviour
{
public RenderTexture renderTexture;
public RawImage rawImage;
// Start is called before the first frame update
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (renderTexture != null)
{
// 假设要获取的像素坐标为(x, y)
//int x = 100;
//int y = 100;
int x = (int)Input.mousePosition.x;
int y = (int)Input.mousePosition.y;
// 创建一个临时的RenderTexture,用于临时存储渲染结果
RenderTexture.active = renderTexture;
// 创建一个2D纹理来存储渲染结果
Texture2D tempTexture = new Texture2D(renderTexture.width, renderTexture.height);
// 从RenderTexture中读取像素颜色到临时纹理
tempTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
tempTexture.Apply();
// 获取像素的颜色
Color pixelColor = tempTexture.GetPixel(x, y);
// 释放临时纹理
Destroy(tempTexture);
// 重置RenderTexture
RenderTexture.active = null;
rawImage.color = pixelColor;
Debug.Log("Pixel Color at (" + x + ", " + y + "): " + pixelColor);
}
}
}
}
04-27
1974
04-01