I want to use ProBuilder to scale and offset its UV’s so that I don’t have to create different materials for every object. This is something ProBuilder can do through its UV editor interface but I want to do it with a component that I attach to the gameObject, so it’s quick and easy to modify.
How can I scale and offset UV’s through script?
Thanks for any help.
I work on very simple meshes, so I’m far from being good at this, but first of all, I think you need to study how UVs are stored in ProBuilder.
I think you should start with building a simple quad, opening the UV editor and running this editor code:
List<Vector4> uvs = new List<Vector4>();
pbMesh.GetUVs(0, uvs); // pbMesh is your ProBuilderMesh object.
foreach (Vector4 uv in uvs)
{
Debug.Log(uv);
}
so you can see how it’s made.
Then, I don’t know if this is the proper or best way to do it, but so far it’s been working fine for me:
List<Vector4> UV = new List<Vector4>(); // new UVs
// First, set UVs to manual. pbMesh is your ProBuilderMesh object.
foreach(Face f in pbMesh.faces)
{
f.manualUV = true;
}
//Then, build your UVs structure
UV.Add(new Vector4(someX, someY)); // z and w coordinates are unused.
// (...) and so on
// Apply UVs
pbMesh.SetUVs(0, UV);
pbMesh.Refresh();