Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
If you change the example WaitForEndOfFrame , in which the screenshot is taken, then you can take the color of only one pixel:
using UnityEngine;
using System.Collections;
public class ColorClick : MonoBehaviour
{
Texture2D tex;
//public Renderer test;
void Start()
{
tex = new Texture2D(1, 1, TextureFormat.RGB24, false);
}
IEnumerator ReadPixelColor()
{
// We should only read the screen buffer after rendering is complete
yield return new WaitForEndOfFrame();
float x = Input.mousePosition.x;
float y = Input.mousePosition.y;
// Read screen contents into the texture
tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
tex.Apply();
Color color = tex.GetPixel(0, 0);
//test.material.color = tex.GetPixel(0, 0);
//Debug.Log(color);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartCoroutine(ReadPixelColor());
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question