Hi,
i’m trying to get color codes out of an texture which has a few different colored areas.
Here’s my code:
public class ColorRef : MonoBehaviour {
Color pixelColor;
public Camera cam;
void Start() {
cam = GetComponent<Camera>();
}
void Update() {
if (!Input.GetMouseButton(0))
return;
RaycastHit hit;
if (!Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit))
return;
Renderer rend = hit.transform.GetComponent<Renderer>();
MeshCollider meshCollider = hit.collider as MeshCollider;
if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null)
return;
Texture2D tex = rend.material.mainTexture as Texture2D;
Vector2 pixelUV = hit.textureCoord;
pixelColor = tex.GetPixel ((int)hit.textureCoord.x,(int)hit.textureCoord.y);
print(pixelColor.ToHexStringRGB());
}
}
But every time I click on the texture my class returns the same color. And every time it’s a color that is not used in the texture.
It’s imported as advanced and read/write is enabled.
What’s going wrong ?