- Home /
Answer by aFeesh · Apr 17, 2020 at 04:48 PM
You need to change Gizmos.matrix to your Canvas's space. To do this get a reference to the transform of your Canvas root object, and use Transform.localToWorldMatrix
The code would like something like this:
private void OnDrawGizmos()
{
Gizmos.matrix = m_CanvasTransform.localToWorldMatrix;
Gizmos.DrawLine(m_Vector3From, m_Vector3To);
}
This will draw a line in your Canvas's space via Gizmos.
What exactly is: "m_CanvasTransform"? Some object in the canvas? The Canvas itself?
Mh probably the Transform component on the Canvas :p
private RectTransform m_CanvasTransform;
Answer by DaCookie · Mar 29, 2021 at 10:42 PM
Based on the answer of @aFeesh , and for anyone who find this post, I made an extension method to make our lives easier. This method will compute a matrix for drawing Gizmos on interfaces, by placing the (0; 0) point in the bottom-left corner of the Canvas.
using UnityEngine;
public static class CanvasExtensions
{
public static Matrix4x4 GetCanvasMatrix(this Canvas _Canvas)
{
RectTransform rectTr = _Canvas.transform as RectTransform;
Matrix4x4 canvasMatrix = rectTr.localToWorldMatrix;
canvasMatrix *= Matrix4x4.Translate(-rectTr.sizeDelta / 2);
return canvasMatrix;
}
}
Your answer
Follow this Question
Related Questions
How to render GL.Line in front of UI canvas? 0 Answers
Drawing in unity UI canvas 2 Answers
Need Help with How to make a Game like Canvas rider... 0 Answers
Draw a line to a new pontential character position/target 1 Answer
World space selection box 0 Answers