Monday, October 31, 2022
HomeGame Developmentunity - Updating UI Graphic vertex positions by means of code

unity – Updating UI Graphic vertex positions by means of code


I am attempting to replace the positions of vertices in my undertaking.

I would been utilizing Unity’s inbuilt Line Renderer beforehand which labored fantastic, however had some issues when factors obtained too shut to one another.

Now I am writing my very own Line Renderer which can be trying good up to now; it attracts strains. I am having no hassle with updating the values in inspector at runtime. Although doing it by means of code would not appear to do something…

I am utilizing the Graphic class from the UnityEngine.UI library as a result of I need to draw the strains as UI parts. And the answer I’ve discovered has been utilizing both Rebuild(), SetAllDirty() or SetVerticesDirty() to power the mesh to replace. This works, but additionally appears to go away outdated invisible vertices and tris within the scene each time it is known as. After a pair seconds of working, the scene has 300k vertices and is working at 10 Fps, when the mesh itself is barely about 2k verts.

    utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing UnityEngine.UI;

public class UICircleBuilder : Graphic
{
    public Level[] factors;
    public bool makeTri2;
    public Coloration secondColor;

    public CanvasUpdate replace;
  
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();
        UIVertex vertex = UIVertex.simpleVert;
        vertex.coloration = coloration;

        for (int i = 0; i < factors.Size-1; i++)
        {




            float vertexWidth = Mathf.Clamp(factors[i].width / 2, 0, 1000);
            float nextVertexWidth = Mathf.Clamp(factors[i+1].width / 2, 0, 1000);


            Vector2 angleBetweenPoints = (factors[i + 1].place - factors[i].place).normalized;
            Vector2 pointCross = Vector3.Cross(angleBetweenPoints, Vector3.ahead).normalized;
            Debug.DrawLine(factors[i + 1].place, factors[i].place, Coloration.inexperienced, 10);
            Debug.DrawLine(factors[i].place,factors[i].place + pointCross*100, Coloration.blue, 10);


            vertex.coloration = factors[i].coloration;
            //Vertex i
            vertex.place = (Vector3)factors[i].place - (Vector3)pointCross * vertexWidth;
            vh.AddVert(vertex);


            
            //Vertex i+1
            vertex.place = (Vector3)factors[i].place + (Vector3)pointCross * vertexWidth;
            vh.AddVert(vertex);

            
            if(i != factors.Size)
            {
                //Vertex i+2
                vertex.place = (Vector3)factors[i + 1].place - (Vector3)pointCross * nextVertexWidth;
                vh.AddVert(vertex);



                //Vertex i+3
                vertex.place = (Vector3)factors[i + 1].place + (Vector3)pointCross * nextVertexWidth;
                vh.AddVert(vertex);
            }

        }


        for (int i = 0; i < factors.Size-1; i++)
        {
            vh.AddTriangle(i*4, i*4+1, (i*4+3));
            Debug.Log(i*4 + ", " + (i*4 + 1) + ", " + (i*4 + 3));
                
            if (makeTri2)
            {
                vh.AddTriangle((i * 4 + 3), (i * 4 + 2), i * 4);
                Debug.Log((i * 4 + 3) + ", " + (i * 4 + 2) + ", " + i * 4);
            }

        }
        
    }

    public void UpdateMesh()
    {
        SetVerticesDirty();
    }
   
}

[System.Serializable]
public class Level
{
    public Vector2 place;
    [Range(0,1000)]
    public float width = 1;
    public Coloration coloration = Coloration.white;
    public Level(Vector2 place, float width = 1)
    {
        this.place = place;
        this.width = width;
    }
}

The mesh is generated utilizing this code, which reads an array of vector2 positions and creates the vertices, afterwards it attracts the tris

Scene stats after working for five seconds:

Scene stats after running for 5 seconds

I would be thankful for any concepts.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments