Unity-UV展开工具

csharp 复制代码
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;
        }
    }
}
相关推荐
豐儀麟阁贵5 分钟前
8.5在方法中抛出异常
java·开发语言·前端·算法
zengyuhan50335 分钟前
Windows BLE 开发指南(Rust windows-rs)
前端·rust
醉方休38 分钟前
Webpack loader 的执行机制
前端·webpack·rust
前端老宋Running1 小时前
一次从“卡顿地狱”到“丝般顺滑”的 React 搜索优化实战
前端·react.js·掘金日报
隔壁的大叔1 小时前
如何自己构建一个Markdown增量渲染器
前端·javascript
用户4445543654261 小时前
Android的自定义View
前端
WILLF1 小时前
HTML iframe 标签
前端·javascript
枫,为落叶1 小时前
Axios使用教程(一)
前端
小章鱼学前端1 小时前
2025 年最新 Fabric.js 实战:一个完整可上线的图片选区标注组件(含全部源码).
前端·vue.js
ohyeah1 小时前
JavaScript 词法作用域、作用域链与闭包:从代码看机制
前端·javascript