Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved How to draw a circle in a custom control using Vector Graphic?

Discussion in 'UI Toolkit' started by curtispelissier, Mar 28, 2023.

  1. curtispelissier

    curtispelissier

    Joined:
    Jul 26, 2019
    Posts:
    39
    Hello everyone,

    I'm currently trying to draw a circle using the Vector Graphic library. And it's not quite pretty neither simple.

    upload_2023-3-28_17-52-47.png

    I don't want to use the Unity's RadialProgress example because it generates a mesh ; I want to do that with svg. I found that VisualElements have an event `generateVisualContent`. So, I subscribe to it and when my element is renderer, I create a shape that I fill with the `VectorUtils.MakeCircleShape` method. But when I draw the contours, it seems broken.

    Code (CSharp):
    1.        
    2. private void OnGenerateVisualContent(MeshGenerationContext context)
    3.         {
    4.             var center = new Vector2(
    5.                 context.visualElement.resolvedStyle.width / 2,
    6.                 context.visualElement.resolvedStyle.height / 2);
    7.            
    8.             Shape shape = new();
    9.             VectorUtils.MakeCircleShape(shape, center, 100);
    10.  
    11.             var paint2D = context.painter2D;
    12.             paint2D.strokeColor = Color.black;
    13.             paint2D.fillColor = Color.yellow;
    14.  
    15.             foreach (var contour in shape.Contours)
    16.             {
    17.                 paint2D.BeginPath();
    18.                 foreach (var segment in contour.Segments)
    19.                     paint2D.BezierCurveTo(segment.P0, segment.P1, segment.P2);
    20.                 paint2D.ClosePath();
    21.             }
    22.  
    23.             paint2D.Fill();
    24.             paint2D.Stroke();
    25.         }
    26.  
    I know that Vector Graphics are still on the UITK roadmap, but I want to know if someone has already tried to draw simple elements using this package and UITK. Also, it seems weird that there are no methods to draw simple shapes by using the painter2D ; maybe I'm using it badly?

    Thank you :)
     
  2. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    323
    Try this:
    painter2D.Arc(center, radius, 0, Angle.FromDegrees(360), ArcDirection.Clockwise);
     
    curtispelissier likes this.
  3. curtispelissier

    curtispelissier

    Joined:
    Jul 26, 2019
    Posts:
    39
    Thank you! It works pretty well for a simple circle or arc draw. But is there an option to change the fill mode like VectorGraphics does?
    upload_2023-3-29_12-35-3.png
     
  4. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    323
    You can subtract the inside by adding another circle with a different direction:
    Code (CSharp):
    1. p.BeginPath();
    2. p.Arc(new Vector2(50, 50), 50, 0, Angle.Degrees(360), ArcDirection.Clockwise);
    3. p.Arc(new Vector2(50, 50), 25, 0, Angle.Degrees(360), ArcDirection.CounterClockwise);
    4. p.fillColor = Color.red;
    5. p.Fill();
    If you want to change the winding order of the fill mode, you can specify it as an argument to Fill():
    https://docs.unity3d.com/2023.1/Documentation/ScriptReference/UIElements.Painter2D.Fill.html

    Also I just found out that we have a page of the manual that explains how to create a progress bar with the Painter 2D:
    https://docs.unity3d.com/2023.1/Documentation/Manual/UIE-radial-progress-use-vector-api.html
     
    curtispelissier likes this.
  5. curtispelissier

    curtispelissier

    Joined:
    Jul 26, 2019
    Posts:
    39
    Ok! Really great to see that. Thank you for your answers!
     
    AlexandreT-unity likes this.