- Home /
Question by
sarthakshah · May 19, 2015 at 09:19 AM ·
javascriptuiui imageimage effect
Unity UI : How to get masked image?
Hello,
I have used UI:Mask component for masking, its working great. Now i want to make new image from masked image, which shows me only the masked image. So how can i get it.
Thanks
Comment
Answer by fafase · May 19, 2015 at 09:24 AM
if you know the position on screen you can read the screen and apply it to a texture:
Texture2D tex = new Texture2D(128, 128);
tex.ReadPixels(new Rect(startPos, endPos, width, height), 0, 0);
tex.Apply();
Thanks
i have used following code
public GameObject masked;
public GameObject op;
// void Start ()
// {
// //renderer.material.mainTexture = masked.renderer.material.mainTexture;
// Rect rect = masked.GetComponent<RectTransform> ().rect;
// Texture2D tex = new Texture2D (128, 128);
// tex.ReadPixels (rect, 0, 0);
// tex.Apply ();
//
// renderer.material.mainTexture = tex;
// }
public bool grab;
//public Renderer display;
void OnPostRender ()
{
if (grab) {
print ("scs");
Rect rect = masked.GetComponent<RectTransform> ().rect;
Texture2D tex = new Texture2D (128, 128);
tex.ReadPixels (rect, 0, 0);
tex.Apply ();
op.renderer.material.mainTexture = tex;
grab = false;
}
}
but its not working.
Not working at what level? Try to debug the value of rect.