How to Draw Lines on the Ui Unity3d

Search Unity

Unity ID

A Unity ID allows you to buy and/or subscribe to Unity products and services, shop in the Asset Store and participate in the Unity community.

  1. Say I have a few draggable image elements. I want to draw some lines between them, like a flow chart or a family tree type structure. Do you know of a good way to do this?

    My thoughts are my options:

    • Draw a think black rotated image to act as a line
    • Draw an image and set the pixels of the image to draw a line say from top-left to bottom-right
    • Use GL.Line
    • Draw 100 tiny images of circles lines up in a line or curve
    • Draw many short rotated thin line images to make a curved line
    Which do you think is best and fastest?
  2. Check out this user contributed script for drawing lines in the new UI system (http://forum.unity3d.com/threads/new-ui-and-line-drawing.253772/) not added it to the UI Extension repo yet (link in sig) but working on it.

    Alternatively, if you want to draw lines outside of UI, then use one of the native Line Renderer components

  3. Hey there, I had the same problem and I solved it by drawing a thick black rotated image :))
    Works only between 2 objects, but can be easily extended
    Here is the code, if anyone needs it:
    You have to call the SetObjects function and give as parameters 2 gameObjects that have rect transform attached to them.
    1. public class LineBetweenObjects : MonoBehaviour
    2. private RectTransform object1;
    3. private RectTransform object2;
    4. private RectTransform rectTransform;
    5. // Start is called before the first frame update
    6.         image = GetComponent<Image> ( ) ;
    7.         rectTransform = GetComponent<RectTransform> ( ) ;
    8. public void SetObjects(GameObject one, GameObject two)
    9.         object1 = one. GetComponent <RectTransform> ( ) ;
    10.         object2 = two. GetComponent <RectTransform> ( ) ;
    11. if (object1. localPosition . x > object2. localPosition . x )
    12. // Update is called once per frame
    13. if (object1. gameObject . activeSelf && object2. gameObject . activeSelf )
    14.             rectTransform. localPosition = (object1. localPosition + object2. localPosition ) / 2 ;
    15.             Vector3 dif = object2. localPosition - object1. localPosition ;
    16.             rectTransform. sizeDelta = new Vector3(dif. magnitude, 5 ) ;
    17.             rectTransform. rotation = Quaternion. Euler ( new Vector3( 0, 0, 180 * Mathf. Atan (dif. y / dif. x ) / Mathf. PI ) ) ;
  4. That works like a charm. Here's my version, which dynamically creates the lines:
    1. void MakeLine( float ax, float ay, float bx, float by, Color col) {
    2.         GameObject NewObj = new GameObject( ) ;
    3.         NewObj. name = "line from " +ax+ " to " +bx;
    4.         Image NewImage = NewObj. AddComponent <Image> ( ) ;
    5.         NewImage. sprite = lineImage;
    6.         RectTransform rect = NewObj. GetComponent <RectTransform> ( ) ;
    7.         rect. SetParent (transform) ;
    8.         rect. localScale = Vector3. one ;
    9.         Vector3 a = new Vector3(ax*graphScale. x, ay*graphScale. y, 0 ) ;
    10.         Vector3 b = new Vector3(bx*graphScale. x, by *graphScale. y, 0 ) ;
    11.         rect. localPosition = (a + b) / 2 ;
    12.         rect. sizeDelta = new Vector3(dif. magnitude, lineWidth) ;
    13.         rect. rotation = Quaternion. Euler ( new Vector3( 0, 0, 180 * Mathf. Atan (dif. y / dif. x ) / Mathf. PI ) ) ;
    Note that you need to make sure the anchor points are set correctly. My graph starts bottom left, so I add:
    1. // set them to start bottom-left
    2.         rect. anchorMin = Vector2. zero ;
    3.         rect. anchorMax = Vector2. zero ;
    as the last lines of that function.

    The function above uses a few global variables to scale and set line width, etc. but it should be easy enough to figure it out. I pass in parameters as individual floats because that's how I get my data, but you could easily adapt it to accept a Vector3.

How to Draw Lines on the Ui Unity3d

Source: https://forum.unity.com/threads/any-good-way-to-draw-lines-between-ui-elements.317902/

0 Response to "How to Draw Lines on the Ui Unity3d"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel