Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jintaenate · Jan 02, 2021 at 10:29 AM · uicalculate

How to draw line smooth in unity (c# base coding problem)

I want to draw line from unityUI. (i dont want to use Line Renderer in unity).

So I figured out the coding below, but my problem is the line sizes are not constant.

Blockquote

 public class MyUILineRenderer : Graphic
 {
 public Vector2[] pointPos;
 public float[] angles;
 public MyUIGridRenderer gridRenderer;

 public Vector2Int gridSize = new Vector2Int(1, 1);
 public float lineThickness = 0.5f;
 public float width;
 public float height;
 public float unitWidth;
 public float unitHeight;

 public float angleHelp;
 public bool calAngle;

 public Color[] colors;

 protected override void OnPopulateMesh(VertexHelper vh)
 {
     vh.Clear();
     width = rectTransform.rect.width;
     height = rectTransform.rect.height;

     unitWidth = width / (float)gridSize.x;
     unitHeight = height / (float)gridSize.y;
     if (calAngle)
     {
         angles = new float[pointPos.Length];
     }

     if (pointPos.Length < 2)
     {
         return;
     }

     float angle = 0;

     for (int i = 0; i < pointPos.Length; i++)
     {
         Vector2 point = pointPos[i];

         if (i < pointPos.Length - 1)
         {
             if (calAngle)
             {
                 angle = angles[i] = -GetAngle(pointPos[i], pointPos[i + 1]) + angleHelp;
             }
             else
             {
                 angle = angles[i];
             }
         }
         DrawLine(vh, point, angle);
     }

     int count = pointPos.Length * 2 - 2;
     for (int i = 0; i < count; i += 2)
     {
         vh.AddTriangle(i + 0, i + 1, i + 3);
         vh.AddTriangle(i + 0, i + 2, i + 3);
     }
 }

 public float GetAngle(Vector2 currentPos, Vector2 targetPos) {
     return (float)(Mathf.Atan2(targetPos.y - currentPos.y, targetPos.x - currentPos.x) * (180 / Mathf.PI));
 }

 private void DrawLine(VertexHelper vh, Vector2 point, float angle)
 {
     UIVertex vertex = UIVertex.simpleVert;
     vertex.color = colors[0];

     vertex.position = Quaternion.Euler(0, 0, angle) * new Vector3(-lineThickness, 0);
     vertex.position += new Vector3(unitWidth * point.x, unitHeight * point.y);
     vh.AddVert(vertex);

     vertex.color = colors[1];
     vertex.position = Quaternion.Euler(0, 0, angle) * new Vector3(lineThickness, 0);
     vertex.position += new Vector3(unitWidth * point.x, unitHeight * point.y);
     vh.AddVert(vertex);
 }
 }

and when i draw in unity like this

alt text

As you guys can see that line thickness is not constant.

How could i make line like picture below?

alt text

line3.png (123.4 kB)
line.png (29.6 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image prakyathd801 · Jan 02, 2021 at 12:38 PM 0
Share

You need adjust the edges while displaying it.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

192 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Amount changes in script but not in Image fill Amount 0 Answers

Check if items are in correct slot using Unity3D UI 0 Answers

Health UI Display 2 Answers

How to disable character movement while editing InputField? 1 Answer

UI interaction bounds messed up 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges