Unity 4.6 RawImage UV animator

Hi,

I have a sprite sheet which I have attached to a Raw Image component in Unity. The Raw Image component has a UV Rect property with X/Y/W/H parameters. What I want do do is to animate the sprite sheet by changing those values over time through a script, which I have attached to the same Game Object.

I do this in my script:

RawImage image = gameObject.GetComponent();
Rect uvRect = image.uvRect;
uvRect.size = new Vector2(value, value);
uvRect.position = new Vector2(value, value);

The values are correctly reflected in the code but when I look at the inspector the values are unchanged and the image does not animate. If I change the values manually in the inspector during runtime, it works correctly. Any suggestions?

Kind Regards,
Robin

You could try

Rect uvRect = new Rect(value,value,value,value);
gameObject.GetComponent<RawImage>().uvRect = uvRect;

That worked well. Thank you!

I realized the reason for this is that Rects are structs, and in C# structs are value types so when I retrieved the rect, I got a copy of it, not a reference. So changes made to it was not reflected to the rect of the Raw Image. In other words, this should also work:

RawImage image = gameObject.GetComponent();
image.uvRect.size = new Vector2(value, value);
image.uvRect.position = new Vector2(value, value);