Scale Polygon UV by code

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 :

using UnityEngine;
using System.Collections;

public class bar : MonoBehaviour {

    public float BarLong = 1.0f;
   
    void Update () {
        transform.localScale = new Vector3(BarLong, 1, 1);
    }
}

Anyone know how scale UVs?

thanks

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)

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.

using UnityEngine;
using System.Collections;

// 3:08 PM 11/25/2014 - super cheap and cheerful quad-based health bar.
//
// To use:
//    - Create a blank game object in your scene at the left edge of
//     where you want the health bar to appear.
//    - Add this script to the game object.
//    - To keep it "with" the camera, parent that object to the camera
//    - ???
//    - Profit!
//

public class CheapAndCheerfulHealthBar : MonoBehaviour
{
    // in world units
    public float width = 5.0f;
    public float height = 1.0f;

    GameObject leftQuad, rightQuad;

    // adjust this public property to change displayed bar
    public float percentage
    {
        set
        {
            float leftWidth = width * value;
            float rightWidth = width * (1.0f - value);
            leftQuad.transform.localPosition = Vector3.right * leftWidth * 0.5f;
            leftQuad.transform.localScale = new Vector3( leftWidth, height);
            rightQuad.transform.localPosition = Vector3.right * (width - rightWidth * 0.5f);
            rightQuad.transform.localScale = new Vector3( rightWidth, height);
        }
    }

    void Start ()
    {
        leftQuad = GameObject.CreatePrimitive( PrimitiveType.Quad);
        leftQuad.transform.parent = transform;
        rightQuad = GameObject.CreatePrimitive( PrimitiveType.Quad);
        rightQuad.transform.parent = transform;

        // <WIP> put your own colors on them here, or even materials...
        leftQuad.renderer.material.color = Color.green;
        rightQuad.renderer.material.color = Color.black;

        percentage = 1.0f;
    }

    void Update()
    {
        // This is just for show; you would get this value
        // from the data source you want to display.
        percentage = Mathf.PingPong( Time.time, 1.0f);
    }
}

Kurt