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 展开")){Unfold0("X");}if (GUILayout.Button("Planar-Y 展开")){Unfold0("Y");}if (GUILayout.Button("Planar-Z 展开")){Unfold0("Z");}GUILayout.EndHorizontal();GUILayout.Space(10);GUILayout.BeginHorizontal("box");if (GUILayout.Button("立方展开")){Unfold1();}GUILayout.EndHorizontal();GUILayout.Space(10);GUILayout.BeginHorizontal("box");if (GUILayout.Button("通过光图UV展开")){Unfold2();}GUILayout.EndHorizontal();}void Unfold0(string inStr){var objs = Selection.objects;if (inStr == "X"){foreach (var obj in objs){var go = obj as GameObject;Mesh mesh = go.GetComponent<MeshFilter>().sharedMesh;Vector3[] vertices = mesh.vertices;Vector2[] uvs = new Vector2[vertices.Length];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){var go = obj as GameObject;Mesh mesh = go.GetComponent<MeshFilter>().sharedMesh;Vector3[] vertices = mesh.vertices;Vector2[] uvs = new Vector2[vertices.Length];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){var go = obj as GameObject;Mesh mesh = go.GetComponent<MeshFilter>().sharedMesh;Vector3[] vertices = mesh.vertices;Vector2[] uvs = new Vector2[vertices.Length];for (int i = 0; i < uvs.Length; i++){uvs[i] = new Vector2(vertices[i].x, vertices[i].y);}mesh.uv = uvs;}}}void Unfold1(){var objs = Selection.objects;foreach (var obj in objs){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;for (int i = 0; i < normals.Length; i++){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);}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);}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; }}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;}}
}