找回密码
 立即注册
查看: 10294|回复: 97

[脚本] 在unity3d中分别修改Cube每个面的贴图UV

[复制链接]
发表于 2015-12-2 16:42 | 显示全部楼层 |阅读模式

  关于UV贴图的文章我们在前面已经介绍过《如何实现Unity3d手动UV动画 》,本篇文章我们来看下修改一个Cube中每个面的贴图UV,也就是贴图中有多个矩形贴图,需要程序从贴图中读取一部分赋值给Cube每个面。
  看下最终效果图
  废话不多说,贴上代码
  using Unity Engine;
  using System.Collections;
  [ExecuteInEditMode]
  public class CustomUVS : MonoBehaviour {
  public Vector2 topPoint;
  public Vector2 bottomPoint;
  public Vector2 leftPoint;
  public Vector2 rightPoint;
  public Vector2 frontPoint;
  public Vector2 backPoint;
  private Mesh m_mesh;
  public enum CubeFaceType
  {
  Top,
  Bottom,
  Left,
  Right,
  Front,
  Back
  };
  // Use this for initialization
  void Start () {
  MeshFilter meshFilter = GetComponent();
  if (meshFilter == null) {
  Debug.LogError("Script needs MeshFilter component");
  return;
  }
  #if UNITY_EDITOR
  Mesh meshCopy = Mesh.Instantiate(meshFilter.sharedMesh) as Mesh; // Make a deep copy
  meshCopy.name = "Cube";
  m_mesh = meshFilter.mesh = meshCopy; // Assign the copy to the meshes
  #else
  m_mesh = meshFilter.mesh;
  #endif
  if (m_mesh == null || m_mesh.uv.Length != 24) {
  Debug.LogError("Script needs to be attached to built-in cube");
  return;
  }
  UpdateMeshUVS();
  }
  // Update is called once per frame
  void Update ()
  {
  #if UNITY_EDITOR
  UpdateMeshUVS();
  #endif
  }
  void UpdateMeshUVS()
  {
  Vector2[] uvs = m_mesh.uv;
  // Front
  SetFaceTexture(CubeFaceType.Front, uvs);
  // Top
  SetFaceTexture(CubeFaceType.Top, uvs);
  // Back
  SetFaceTexture(CubeFaceType.Back, uvs);
  // Bottom
  SetFaceTexture(CubeFaceType.Bottom, uvs);
  // Left
  SetFaceTexture(CubeFaceType.Left, uvs);
  // Right
  SetFaceTexture(CubeFaceType.Right, uvs);
  m_mesh.uv = uvs;
  }
  Vector2[] GetUVS(float originX, float originY)
  {
  Vector2[] uvs = new Vector2[4];
  uvs[0] = new Vector2(originX / 3.0f, originY / 3.0f);
  uvs[1] = new Vector2((originX + 1) / 3.0f, originY / 3.0f);
  uvs[2] = new Vector2(originX / 3.0f, (originY + 1) / 3.0f);
  uvs[3] = new Vector2((originX + 1) / 3.0f, (originY + 1) / 3.0f);
  return uvs;
  }
  void SetFaceTexture(CubeFaceType faceType, Vector2[] uvs)
  {
  if (faceType == CubeFaceType.Front) {
  Vector2[] newUVS = GetUVS(frontPoint.x, frontPoint.y);
  uvs[0] = newUVS[0];
  uvs[1] = newUVS[1];
  uvs[2] = newUVS[2];
  uvs[3] = newUVS[3];
  }else if (faceType == CubeFaceType.Back) {
  Vector2[] newUVS = GetUVS(backPoint.x, backPoint.y);
  uvs[10] = newUVS[0];
  uvs[11] = newUVS[1];
  uvs[6] = newUVS[2];
  uvs[7] = newUVS[3];
  }else if (faceType == CubeFaceType.Top) {
  Vector2[] newUVS = GetUVS(topPoint.x, topPoint.y);
  uvs[8] = newUVS[0];
  uvs[9] = newUVS[1];
  uvs[4] = newUVS[2];
  uvs[5] = newUVS[3];
  }else if (faceType == CubeFaceType.Bottom) {
  Vector2[] newUVS = GetUVS(bottomPoint.x, bottomPoint.y);
  uvs[12] = newUVS[0];
  uvs[14] = newUVS[1];
  uvs[15] = newUVS[2];
  uvs[13] = newUVS[3];
  }else if (faceType == CubeFaceType.Left) {
  Vector2[] newUVS = GetUVS(leftPoint.x, leftPoint.y);
  uvs[16] = newUVS[0];
  uvs[18] = newUVS[1];
  uvs[19] = newUVS[2];
  uvs[17] = newUVS[3];
  }else if (faceType == CubeFaceType.Right) {
  Vector2[] newUVS = GetUVS(rightPoint.x, rightPoint.y);
  uvs[20] = newUVS[0];
  uvs[22] = newUVS[1];
  uvs[23] = newUVS[2];
  uvs[21] = newUVS[3];
  }
  }
  }
  使用的贴图
  给一个Cube添加改图片材质。并添加CustomUVS.cs脚本。修改需要截取的区域原点
  注意:由于图片是3×3的,所以截取区域中按3等分截取。
  好了,本篇unity3d教程到此结束,下篇我们再会!
  资源地址: http://cg.silucg.com/dongman/unity3d/7996.html

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2016-11-25 19:45 | 显示全部楼层

膜拜中。。。。
发表于 2017-3-27 12:17 | 显示全部楼层
很不错
发表于 2017-3-27 12:26 | 显示全部楼层
好帖就是要顶
发表于 2017-3-27 12:37 | 显示全部楼层
难得一见的好帖
发表于 2017-3-27 12:58 | 显示全部楼层
说的非常好
发表于 2017-3-27 12:51 | 显示全部楼层
LZ真是人才
发表于 2017-5-12 09:21 | 显示全部楼层
楼主是超人
发表于 2017-5-12 09:18 | 显示全部楼层
真心顶
发表于 2017-5-12 09:12 | 显示全部楼层
难得一见的好帖
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-23 01:12 , Processed in 0.073025 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表