初识Unity3D的UV贴图,
我们知道通过物体可以通过调节tiling和offset值来调节uv信息,但是这是不准确的。
例如:原图为这样
而拉伸之后调节tiling值只能适应一两个面的uv值是正确的,如图:
我们通过的代码控制之后就能适应每个面正确的uv贴图,如图:
下面直接上代码
using System.Collections.Generic;
using UnityEngine;
public class Uvtest2 : MonoBehaviour
{
public static List<Vector3> Vertices;//顶点
public static List<int> Triangles;//三角形索引
public static List<Vector2> Uvs;//uv信息
public static float VerticesCount;//顶点个数
public static float TrianglesCount;//三角形索引个数
public static float UVsCount;//uv个数
public static Vector3 Scales;//scale值
void Start()
{
SetUVStart(gameObject);
}
void Update()
{
if (Scales != gameObject.transform.localScale)
{
SetUV(this.gameObject);
Scales = gameObject.transform.localScale;
}
}
public void SetUVStart(GameObject gameObject)//得到控制uv需要的一些初始值
{
Mesh mh = gameObject.GetComponent<MeshFilter>().mesh;
Vertices = new List<Vector3>();
Triangles = new List<int>();
Uvs = new List<Vector2>();
VerticesCount = mh.vertices.Length;
UVsCount = mh.uv.Length;
TrianglesCount = mh.triangles.Length;
for (int i = 0; i < VerticesCount; i++)
{
Vertices.Add(mh.vertices[i]);
}
for (int i