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

Make a timer with visual effects

Discussion in 'General Graphics' started by JoseCuevasH, Nov 10, 2017.

  1. JoseCuevasH

    JoseCuevasH

    Joined:
    Oct 2, 2017
    Posts:
    9
    Hi to everyone!

    Im creating a quizz game but i need a timer, i already have the timer script and what i need is make it more friendly i mean, for example i want to make a timer like this image:
    https://www.freepik.com/index.php?goto=74&idfoto=796723

    but i dont know how to fill it arround the numbers (The time will be inside of the timer) as the image that the colour red, yellow, blue that fill the timer img.

    ¿Do you know how can i do it?

    Thanks so much!
     
  2. ifurkend

    ifurkend

    Joined:
    Sep 4, 2012
    Posts:
    350
    I assume you mean the area of the clock face sector increases gradually. I would suggest you make a circle mesh in Blender, 3Ds or Maya and unwrap its UV to make sure all triangles are laid flat horizontally side by side. Export it in FBX format to your project folder and create new material with Particle/Alpha Blended shader and texture (clamp wrap mode and point filter mode) that is horizontally divided by white and black in the middle. Finally it's just the question of applying SetTextureOffset and SetColor (_TintColor) to your material.
     
  3. JoseCuevasH

    JoseCuevasH

    Joined:
    Oct 2, 2017
    Posts:
    9
    Thanks so much, i found a videotutorial to do it, to everyone who have the same doubt as me, you can see how to do it i this video:


    Thanks Ifurkend for your help!
     
  4. ifurkend

    ifurkend

    Joined:
    Sep 4, 2012
    Posts:
    350
    Didn't know that UI image provides radial filling.

    I just want to correct myself about scrolling texture UV of a circle mesh, this will never give the proper conical scrolling result because of the limitation of UV mapping on a single triangle. To fix that I instead limit the position of the vertices which are procedurally spawned.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [ExecuteInEditMode]
    5. public class clockFace : MonoBehaviour {
    6.     public float radius = 1f;
    7.     [Range(1,360)]
    8.     public int division = 36;
    9.     [Range(0,1)]
    10.     public float progress = 1;
    11.     int progressTris;
    12.     MeshFilter mf;
    13.     Mesh mesh;
    14.     Vector3[] vertices;
    15.     int[] triangles;
    16.     Vector3[] normals;
    17.     Vector2[] uvs;
    18.     void Start () {
    19.         mesh = new Mesh();
    20.         mf = GetComponent<MeshFilter>();
    21.         mf.mesh = mesh;
    22.         vertices = new Vector3[division + 2];
    23.         vertices[division + 1] = this.transform.localPosition;
    24.         for (int i = 0; i < division + 1; i++) {
    25.             vertices[i] = Quaternion.AngleAxis(360f/division * i, Vector3.back) * new Vector3(0,radius,0);
    26.         }
    27.         mesh.vertices = vertices;
    28.      
    29.         triangles = new int[division * 3];
    30.         for (int i = 0; i < division * 3; i++) {
    31.             for (int j = 0; j < division + 1; j++) {
    32.                 if (i % 3 == 0) {
    33.                     triangles[i] = i / 3;
    34.                 } else if (i % 3 == 1) {
    35.                     triangles[i] = (i - 1) / 3 + 1;
    36.                 } else {
    37.                     triangles[i] = division + 1;
    38.                 }
    39.             }
    40.         }
    41.         mesh.triangles = triangles;
    42.      
    43.         normals = new Vector3[division + 2];
    44.         for (int i = 0; i < division + 2; i++) {
    45.             normals[i] = Vector3.back;
    46.         }
    47.         mesh.normals = normals;
    48.      
    49.         uvs = new Vector2[division + 2];
    50.         uvs[division + 1] = new Vector2(1f, 0f);
    51.         for (int i = 0; i < division + 1; i++) {
    52.             uvs[i] = new Vector2((float)(i)/(float)(division), 1f);
    53.         }
    54.         mesh.uv = uvs;
    55.     }
    56.  
    57.     void Update () {
    58.         for (int i = 0; i < division + 1; i++) {
    59.             int limiter = (int)(Mathf.Clamp(i, 0, division * progress));
    60.             vertices[i] = Quaternion.AngleAxis(360f/division * limiter, Vector3.back) * new Vector3(0,radius,0);
    61.         }
    62.         mesh.vertices = vertices;
    63.     }
    64. }
    I use this as a personal assignment for practicing procedural mesh. Also it gives me an excuse to complain to Unity for not updating the example codes in the manual from the now obsoleted UnityScript (Java) to C#.