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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to divide circle & set color for each path ?

Discussion in '2D' started by David James, Nov 18, 2015.

  1. David James

    David James

    Joined:
    Jan 29, 2015
    Posts:
    61
    Hello everybody,

    I have a problem . I want create a circle & divide this to three path,set color for each path. How to make this?
    Thanks :) !




    Thanks :)
     
  2. David James

    David James

    Joined:
    Jan 29, 2015
    Posts:
    61
    Please, help me ! :(
     
  3. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    612
    in 2D?

    - create a procedural Mesh circle with triangles that have one vertex in the center
    - weight the arcs of the triangles by your percentages
    - if an arc is larger than a certain degree amount you create a new triangle
     
    David James likes this.
  4. David James

    David James

    Joined:
    Jan 29, 2015
    Posts:
    61
    Right, Nice...Thank you :)

    But How to create a circle from the triangle ??
     
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    David James likes this.
  6. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    612
    I got some code that may help you. I use it to visualize an attack Cone with a procedural mesh, i just post it here, you have to make it work for you, but it should look like the pink cone in this gif if you make it work:

    Animation.gif


    Code (CSharp):
    1. //Cone Mesh
    2.     private    static GameObject    attackCone;
    3.     private    static Mesh            coneMesh;
    4.     //public    Material        coneMat;
    5.            
    6.     private    void CreateAttackCone(){    //create a new cardPrefab procedurally    and a selection border
    7.        
    8.         attackCone                        = new GameObject();
    9.         attackCone.transform.position    = Vector3.zero;//pos;    //not important
    10.         attackCone.transform.name        = "AttackCone";
    11.        
    12.     //Card
    13.         coneMesh                = attackCone.AddComponent<MeshFilter>().mesh;
    14.         /*MeshRenderer coneMeshR    = */attackCone.AddComponent<MeshRenderer>();
    15.         //coneMeshR.material        = coneMat;
    16.        
    17.         int vertCount = 19;
    18.                                    
    19.         //coneMesh.Clear();
    20.         Vector3[]    vertices    = new Vector3    [vertCount];
    21.         //Color[]        colors        = new Color        [vertCount];
    22.         Vector2[]    uv            = new Vector2    [vertCount];
    23.         Vector3[]    normals        = new Vector3    [vertCount];
    24.         int[]        triangles    = new int        [(vertCount-1)*3];
    25.         Vector4[]    tangents    = new Vector4    [vertCount];
    26.                            
    27.         for(int i = 0; i<vertCount-1; i++){
    28.            
    29.             vertices[ i]    = Vector3.zero;
    30.            
    31.             if(i<vertCount-2){
    32.                 triangles[i*3+0]    = 0;
    33.                 triangles[i*3+1]    = i+1;
    34.                 triangles[i*3+2]    = i+2;
    35.             }else{
    36.                 triangles[i*3+0]    = 0;
    37.                 triangles[i*3+1]    = i+1;
    38.                 triangles[i*3+2]    = 1;
    39.             }
    40.                    
    41.         }
    42.        
    43.         for(int i = 0; i<vertCount; i++){            
    44.             uv[i]        = new Vector2(vertices[i].x, vertices[i].z);
    45.             tangents[i]    = Vector4.zero;
    46.         }                    
    47.        
    48.         coneMesh.vertices    = vertices;
    49.         coneMesh.uv            = uv;
    50.         coneMesh.tangents    = tangents;
    51.         coneMesh.normals    = normals;
    52.         coneMesh.triangles    = triangles;
    53.        
    54.         coneMesh.RecalculateNormals();
    55.         coneMesh.RecalculateBounds();
    56.     }
    57.    
    58.     private    static void UpdateCone(float range, float coneAngle){
    59.        
    60.         float angle        = -coneAngle/2F;
    61.         Vector3[]    vertices    = new Vector3    [coneMesh.vertices.Length];
    62.        
    63.         for(int i = 1; i<vertices.Length; i++){            
    64.             vertices[ i]    = Quaternion.Euler(0, angle, 0)    *(Vector3.forward*range);
    65.             angle += coneAngle/(vertices.Length-1);
    66.         }
    67.         coneMesh.vertices    = vertices;
    68.     }
     
    David James likes this.
  7. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    612
  8. David James

    David James

    Joined:
    Jan 29, 2015
    Posts:
    61
    Thank ! :) I'm trying...