How to offset a RectTransform?
Hi all
I have an app where I need to use Screen Space - Overlay as the canvas type and have been rendering a line from one UI element to another using the following code that I found after some research:
using UnityEngine;
using UnityEngine.UI;
public class UILineRenderer : MonoBehaviour
{
private RectTransform object1;
private RectTransform object2;
private Image image;
private RectTransform rectTransform;
private RectTransform object2Target;
// Start is called before the first frame update
void Start()
{
image = GetComponent<Image>();
rectTransform = GetComponent<RectTransform>();
}
public void SetObjects(GameObject one, GameObject two)
{
object1 = one.GetComponent<RectTransform>();
object2 = two.GetComponent<RectTransform>();
RectTransform aux;
if (object1.localPosition.x > object2.localPosition.x)
{
aux = object1;
object1 = object2;
object2 = aux;
}
image.enabled = true;
}
// Update is called once per frame
void Update()
{
if (object1 != null && object2 != null)
{
if (object1.gameObject.activeSelf && object2.gameObject.activeSelf)
{
rectTransform.localPosition = (object1.localPosition + object2.localPosition) / 2;
Vector3 dif = object2.localPosition - object1.localPosition;
rectTransform.sizeDelta = new Vector3(dif.magnitude, 5);
rectTransform.rotation = Quaternion.Euler(new Vector3(0, 0, 180 * Mathf.Atan(dif.y / dif.x) / Mathf.PI));
}
}
}
public void DisableLine()
{
image.enabled = false;
}
}
I just call this using:
uiLineRenderer.SetObjects(panelLineTarget, buttonLineTarget);
This creates the following result of drawling a line between the icon and the panel:
I'm trying to offset the RectTransform for the line so it starts at the top of the information icon. Does anyone have any idea how to slightly offset a RectTransform in a certain direction? I've tested a few different ways but had no luck.
Thanks very much for reading!
Your answer
Follow this Question
Related Questions
Updated with UI text element not correct (same frame) 0 Answers
Can i treat multiple image components as a single image? 0 Answers
How to move a UI Image in the z axis? 2D 0 Answers
Non-root Canvases will not be scaled: My UI has disappeared 2 Answers
RectTransform.anchoredPosition changes, but element is not visually updated 1 Answer