Unity-UV展开工具

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class unfold : EditorWindow
{

    [MenuItem("Gq_Tools/展开")]
    public static void ShowWin()
    {
        EditorWindow.CreateInstance<unfold>().Show();
    }
    private void OnGUI()
    {
        GUILayout.Space(10);
        GUILayout.BeginHorizontal("box");
        GUILayout.Space(10);
        if (GUILayout.Button("Planar-X 展开"))//UI上画一个按钮
        {
            //MonoBehaviour.print("do");
            Unfold0("X");
        }
        if (GUILayout.Button("Planar-Y 展开"))//UI上画一个按钮
        {
            //MonoBehaviour.print("do");
            Unfold0("Y");
        }
        if (GUILayout.Button("Planar-Z 展开"))//UI上画一个按钮
        {
            //MonoBehaviour.print("do");
            Unfold0("Z");
        }
        GUILayout.EndHorizontal();
        GUILayout.Space(10);
        GUILayout.BeginHorizontal("box");
        if (GUILayout.Button("立方展开"))//UI上画一个按钮
        {
            //MonoBehaviour.print("do");
            Unfold1();
        }
        GUILayout.EndHorizontal();
        GUILayout.Space(10);
        GUILayout.BeginHorizontal("box");
        if (GUILayout.Button("通过光图UV展开"))//UI上画一个按钮  
        {
            //MonoBehaviour.print("do");
            Unfold2();
        }
        GUILayout.EndHorizontal();
    }
    //Planar Unfold
    void Unfold0(string inStr)
    {
        var objs = Selection.objects;
        if (inStr == "X")
        {
            foreach (var obj in objs)//for每个选中的物体
            {
                var go = obj as GameObject;
                Mesh mesh = go.GetComponent<MeshFilter>().sharedMesh;
                Vector3[] vertices = mesh.vertices;
                Vector2[] uvs = new Vector2[vertices.Length];

                //Planar Unfold
                for (int i = 0; i < uvs.Length; i++)
                {
                    uvs[i] = new Vector2(vertices[i].y, vertices[i].z);
                }
                mesh.uv = uvs;
            }
        }
        if (inStr == "Y")
        {
            foreach (var obj in objs)//for每个选中的物体  
            {
                var go = obj as GameObject;
                Mesh mesh = go.GetComponent<MeshFilter>().sharedMesh;
                Vector3[] vertices = mesh.vertices;
                Vector2[] uvs = new Vector2[vertices.Length];

                //Planar Unfold
                for (int i = 0; i < uvs.Length; i++)
                {
                    uvs[i] = new Vector2(vertices[i].x, vertices[i].z);
                }
                mesh.uv = uvs;
            }
        }
        if (inStr == "Z")
        {
            foreach (var obj in objs)//for每个选中的物体
            {
                var go = obj as GameObject;
                Mesh mesh = go.GetComponent<MeshFilter>().sharedMesh;
                Vector3[] vertices = mesh.vertices;
                Vector2[] uvs = new Vector2[vertices.Length];

                //Planar Unfold
                for (int i = 0; i < uvs.Length; i++)
                {
                    uvs[i] = new Vector2(vertices[i].x, vertices[i].y);
                }
                mesh.uv = uvs;
            }
        }
    }
    //Cubic Unfold
    void Unfold1()
    {
        var objs = Selection.objects;
        foreach (var obj in objs)//for每个选中的物体  
        {
            var go = obj as GameObject;
            Mesh mesh = go.GetComponent<MeshFilter>().sharedMesh;
            Vector3[] vertices = mesh.vertices;
            Vector2[] uvs = new Vector2[vertices.Length];
            Vector3[] normals = mesh.normals;
            //Cubic Unfold
            for (int i = 0; i < normals.Length; i++)
            {
                //X-Plane
                if (Mathf.Abs(normals[i].x) > Mathf.Abs(normals[i].y) && Mathf.Abs(normals[i].x) > Mathf.Abs(normals[i].z))
                {
                    uvs[i] = new Vector2(vertices[i].y, vertices[i].z);
                }
                //Y-Plane
                if (Mathf.Abs(normals[i].y) > Mathf.Abs(normals[i].x) && Mathf.Abs(normals[i].y) > Mathf.Abs(normals[i].z))
                {
                    uvs[i] = new Vector2(vertices[i].x, vertices[i].z);
                }
                //Z-Plane
                if (Mathf.Abs(normals[i].z) > Mathf.Abs(normals[i].x) && Mathf.Abs(normals[i].z) > Mathf.Abs(normals[i].y))
                {
                    uvs[i] = new Vector2(vertices[i].x, vertices[i].y);
                }
            }
            mesh.uv = uvs; 
        }
    }
    //use lightmap UV  
    void Unfold2()
    {
        var objs = Selection.objects;
        foreach (var obj in objs)//对于每个选中的物体 // 
        {
            var go = obj as GameObject;
            Mesh mesh = go.GetComponent<MeshFilter>().sharedMesh;
            mesh.uv = mesh.uv2;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值