draw a graph om an image

Hi Everyone,

When I try to draw a graph on my image the position of the graph is not were I want it to be. What I try is to draw the graph on the upper right corner of my image. This is my code;

    void Start () {

        //World Corners are clockwise starting at the BottomLeft.
        //worldCorners[0] == bottom left
        //worldCorners[1] == Top Left
        //worldCorners[2] == Top right
        //worldCorners[3] == bottom right

        //set boundaries for BO scrollmenu
        RectTransform img = Image.GetComponent<RectTransform>();
        Vector3[] worldCorners = new Vector3[4];
        img.GetWorldCorners(worldCorners);

        graphWidth = worldCorners[0].x - worldCorners[3].x;
        graphHeight = worldCorners[1].y - worldCorners[0].y;

        pos = transform.worldToLocalMatrix.MultiplyPoint(new Vector2(graphWidth, graphHeight));

        LineR = GetComponent<LineRenderer>();
        LineR.SetPosition(2, pos);

    }

How do I draw the graph in the upper right corner of my image ?

Thanks in advance

Does anyone have any idea ? The solution should be simple but I can’t get it to work. What I basically want is to draw a graph on the image.

Anyone that have an idea how to fix this. it has been a few days but I still haven’t figured it out.

OK, let’s back up. What you say you’re doing doesn’t match what your code appears to be doing. So which is it you actually want to do?

What your code is doing is using a LineRenderer to create lines in 3D space. In the past, this has not been a very effective way of drawing lines in Unity, leading to solutions like Vectrosity (which I use in a number of my projects). I’ve heard mumblings that maybe LineRenderer has been improved in recent versions of Unity, but I’m afraid I don’t know much about that; I still use Vectrosity when I need to draw 3D lines.

But no method of drawing lines in 3D space is “drawing a graph on an image,” which is what you say you want to do. That is not simple; Unity doesn’t have any built-in methods for drawing lines on an image. You have to manipulate the texture pixels directly, via things like Texture2D.GetPixels. I have some utility functions I’ve written over the years to do things like draw straight lines into a texture, but they’re not really in good shape for sharing. (I also have PixelSurface, which can do this and much more, but it’s probably overkill for your needs.)

Finally, there’s also 2D drawing in the UI system. This too is rather complicated to do from scratch, though there are some decent assets for it — I don’t yet have one I recommend, but if you search the Asset Store for “2D drawing” I’m sure you’ll find some solutions.

HTH,

  • Joe
1 Like