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;
        }
    }
}

你可能感兴趣的:(unity,uv,前端)