Search Unity

Scale Polygon UV by code

Discussion in 'Scripting' started by GlitchInTheMatrix, Nov 25, 2014.

  1. GlitchInTheMatrix

    GlitchInTheMatrix

    Joined:
    Apr 12, 2010
    Posts:
    285
    Hi guys,

    Im trying to Scale an Quad and scale the UVs at the same time, something like a Progress Bar.

    Im basically scale the object by code and need to scale the UVs as well with the same value.

    this is the base code :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class bar : MonoBehaviour {
    5.  
    6.     public float BarLong = 1.0f;
    7.    
    8.     void Update () {
    9.         transform.localScale = new Vector3(BarLong, 1, 1);
    10.     }
    11. }
    Anyone know how scale UVs?

    thanks
     
  2. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
    You'd have to access the Mesh from the MeshFilter of the surface that the image is being drawn upon to access the UVs, and you'll likely want to access the Texture of the Material used by the Renderer that is responsible for rendering the image with the Mesh's data.

    From there, you can modify the UVs to traverse along the texture-coordinate values (you'll probably want to do a Lerp from the top-left to the top-right UV coordinates of the image as well as the bottom-left to the bottom-right UV coordinates of the image with the same Lerp percentage value for each pair of UVs)
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    If you want to do it super-cheap-and-cheerful, here's one that does it with a pair of quads, scaled and placed immediately adjacent to each other.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. // 3:08 PM 11/25/2014 - super cheap and cheerful quad-based health bar.
    6. //
    7. // To use:
    8. //    - Create a blank game object in your scene at the left edge of
    9. //     where you want the health bar to appear.
    10. //    - Add this script to the game object.
    11. //    - To keep it "with" the camera, parent that object to the camera
    12. //    - ???
    13. //    - Profit!
    14. //
    15.  
    16. public class CheapAndCheerfulHealthBar : MonoBehaviour
    17. {
    18.     // in world units
    19.     public float width = 5.0f;
    20.     public float height = 1.0f;
    21.  
    22.     GameObject leftQuad, rightQuad;
    23.  
    24.     // adjust this public property to change displayed bar
    25.     public float percentage
    26.     {
    27.         set
    28.         {
    29.             float leftWidth = width * value;
    30.             float rightWidth = width * (1.0f - value);
    31.             leftQuad.transform.localPosition = Vector3.right * leftWidth * 0.5f;
    32.             leftQuad.transform.localScale = new Vector3( leftWidth, height);
    33.             rightQuad.transform.localPosition = Vector3.right * (width - rightWidth * 0.5f);
    34.             rightQuad.transform.localScale = new Vector3( rightWidth, height);
    35.         }
    36.     }
    37.  
    38.     void Start ()
    39.     {
    40.         leftQuad = GameObject.CreatePrimitive( PrimitiveType.Quad);
    41.         leftQuad.transform.parent = transform;
    42.         rightQuad = GameObject.CreatePrimitive( PrimitiveType.Quad);
    43.         rightQuad.transform.parent = transform;
    44.  
    45.         // <WIP> put your own colors on them here, or even materials...
    46.         leftQuad.renderer.material.color = Color.green;
    47.         rightQuad.renderer.material.color = Color.black;
    48.  
    49.         percentage = 1.0f;
    50.     }
    51.  
    52.     void Update()
    53.     {
    54.         // This is just for show; you would get this value
    55.         // from the data source you want to display.
    56.         percentage = Mathf.PingPong( Time.time, 1.0f);
    57.     }
    58. }
    59.  
    Kurt