Skip to content

Instantly share code, notes, and snippets.

@junaid109
Created June 7, 2023 14:29
Show Gist options
  • Save junaid109/4a6a402e11f5cc6bf8a03b830180f619 to your computer and use it in GitHub Desktop.
Save junaid109/4a6a402e11f5cc6bf8a03b830180f619 to your computer and use it in GitHub Desktop.
Link React Trans forms

Attach it to GameObject.

Assign the two RectTransforms that you want to link to the rectTransform1 and rectTransform2 variables in the inspector.

The Update method of the script will be called every frame, and it will update the position of rectTransform2 based on the current position of the right edge of rectTransform1.

using UnityEngine;
public class LinkRectTransforms : MonoBehaviour
{
public RectTransform rectTransform1;
public RectTransform rectTransform2;
private void Update()
{
// Get the current position of the right edge of RectTransform1
float rightEdge = rectTransform1.position.x + rectTransform1.rect.xMax;
// Set the position of the left edge of RectTransform2 based on the right edge of RectTransform1
rectTransform2.position = new Vector3(rightEdge - rectTransform2.rect.xMin, rectTransform2.position.y, rectTransform2.position.z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment