- Home /
Can we animate the UI image by changing offsets ?
Hello, I would like to know if we can animate the UI image by changing its materials offsets. To make it look like its scrolling. I've done this with 2D objects planes and quad before. But not sure if there is a proper way to do the same with the Unity UI. I did a little experimenting but the results were not the same as expected.
Thanks in advance :)
Comment
Answer by stewartimel · Feb 12, 2016 at 06:27 PM
This is what I did:
public class BackgroundFollow : MonoBehaviour {
Transform control;
float current_x_offset = 0;
public float offset = 0f;
public float x_pos;
public float y_pos;
public float z_pos;
void Awake () {
control = GameObject.FindGameObjectWithTag ("player").transform;
Vector3 pos = new Vector3 (control.position.x + x_pos, y_pos, z_pos);
transform.position = pos;
}
void Start ()
{
SetOffset ();
}
public void SetOffset ()
{
offset = control.position.x - this.transform.position.x;
}
public void ScrollImage (float amount)
{
Vector3 pos = transform.position;
pos.x = control.position.x - this.offset;
transform.position = pos;
current_x_offset += amount;
Vector2 offset = new Vector2 (current_x_offset, 0f);
this.GetComponent<Renderer>().sharedMaterial.SetTextureOffset ("_MainTex", offset);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613201110im_/https://answers.unity.com/themes/thub/images/avi.jpg)