• 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;
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
  • 相关阅读:
    【ASE入门学习】ASE入门系列二十三——顶点偏移
    腾讯云部署springboot服务
    Rider 2023:打造高效.NET项目的智能IDE,让开发更简单mac/win版
    SAP-MM-查找采购订单的创建和修改日期
    人工智能AI绘画,Stable Diffusion保姆级教程,小白也可以掌握SD使用
    springboot项目中配置redis
    【OpenMv】颜色模式之Lab
    [userfaultfd] 2019-BalsnCTF_KrazyNote
    Design Experience
    如何选择一个好的简历模板
  • 原文地址:https://blog.csdn.net/qq_43687127/article/details/134160796