Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- public class UVPreview : EditorWindow {
- private Mesh mesh;
- private Texture2D texture;
- private int selectedUV;
- private Rect rect;
- private Rect bounds;
- private float zoom = 1f;
- private Vector2 pan;
- private Event evt;
- private int triangle;
- private static Vector2 startSize = new Vector2(500, 560);
- private static Color edgeColor = new Color(0.3f, 0.8f, 1f, 1f);
- private static Color faceColor = new Color(0.3f, 0.8f, 1f, 0.25f);
- private static string[] uvLabels {
- get {
- return new string[] { "UV", "UV2", "UV3", "UV4" };
- }
- }
- private bool drawEdges {
- get {
- return EditorPrefs.GetBool("DrawEdges");
- }
- set {
- EditorPrefs.SetBool("DrawEdges", value);
- }
- }
- private bool drawFaces {
- get {
- return EditorPrefs.GetBool("DrawFaces");
- }
- set {
- EditorPrefs.SetBool("DrawFaces", value);
- }
- }
- [MenuItem("Window/UV Preview")]
- private static void Init() {
- var window = GetWindow<UVPreview>();
- window.titleContent = new GUIContent("UV Preview");
- window.wantsMouseMove = true;
- window.minSize = startSize;
- window.maxSize = startSize * 2;
- window.Show();
- }
- private void OnGUI() {
- EditorGUILayout.Separator();
- EditorGUILayout.BeginVertical("HelpBox");
- EditorGUILayout.LabelField("UV Preview", EditorStyles.boldLabel);
- EditorGUILayout.Separator();
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.BeginVertical(GUILayout.MaxWidth(200f));
- mesh = EditorGUILayout.ObjectField(mesh, typeof(Mesh), true) as Mesh;
- GUI.enabled = mesh;
- texture = EditorGUILayout.ObjectField(texture, typeof(Texture2D), true) as Texture2D;
- EditorGUILayout.EndVertical();
- EditorGUILayout.BeginVertical(GUILayout.MaxWidth(200f));
- selectedUV = GUILayout.Toolbar(selectedUV, uvLabels);
- if(GUILayout.Button("Set default position")) {
- zoom = 1f;
- pan = Vector2.zero;
- }
- EditorGUILayout.EndVertical();
- GUILayout.FlexibleSpace();
- EditorGUILayout.BeginVertical();
- drawEdges = EditorGUILayout.ToggleLeft("Edges", drawEdges, GUILayout.Width(50f));
- drawFaces = EditorGUILayout.ToggleLeft("Faces", drawFaces, GUILayout.Width(50f));
- EditorGUILayout.EndVertical();
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.EndVertical();
- GUI.enabled = true;
- Vector2[] uv = new Vector2[0];
- if(mesh) {
- if(!mesh.isReadable) {
- EditorGUILayout.HelpBox("Mesh is not readable, you can change this on import settings", MessageType.Error);
- return;
- }
- switch(selectedUV) {
- case 1:
- uv = mesh.uv2;
- break;
- case 2:
- uv = mesh.uv3;
- break;
- case 3:
- uv = mesh.uv4;
- break;
- default:
- uv = mesh.uv;
- break;
- }
- }
- else {
- EditorGUILayout.HelpBox("No mesh selected", MessageType.Info);
- return;
- }
- if(uv.Length == 0) {
- EditorGUILayout.HelpBox(string.Format("Mesh doesn't have UV{0}", selectedUV + 1), MessageType.Warning);
- return;
- }
- EditorGUILayout.BeginVertical("HelpBox");
- bounds = EditorGUILayout.GetControlRect(GUILayout.ExpandHeight(true));
- PanAndZoom();
- if(texture)
- GUI.DrawTexture(bounds, texture);
- DrawUV(uv, mesh.triangles, rect);
- EditorGUILayout.EndVertical();
- }
- private void DrawUV(Vector2[] uv, int[] tris, Rect rect) {
- for(triangle = 0; triangle < tris.Length; triangle += 3) {
- var vertice0 = uv[tris[triangle + 0]];
- var vertice1 = uv[tris[triangle + 1]];
- var vertice2 = uv[tris[triangle + 2]];
- vertice0 = RelativeUVPosition(vertice0);
- vertice1 = RelativeUVPosition(vertice1);
- vertice2 = RelativeUVPosition(vertice2);
- Handles.color = edgeColor;
- if(drawEdges)
- Handles.DrawAAPolyLine(vertice0, vertice1, vertice2, vertice0);
- Handles.color = faceColor;
- if(drawFaces)
- Handles.DrawAAConvexPolygon(vertice0, vertice1, vertice2);
- }
- }
- private void PanAndZoom() {
- rect = bounds;
- evt = Event.current;
- switch(evt.type) {
- case EventType.ScrollWheel:
- zoom += -evt.delta.y * 0.05f;
- zoom = Mathf.Clamp(zoom, 0.3f, 2f);
- evt.Use();
- break;
- case EventType.MouseDrag:
- if(bounds.Contains(evt.mousePosition)) {
- EditorGUIUtility.AddCursorRect(bounds, MouseCursor.Pan);
- pan += evt.delta;
- pan.x = Mathf.Clamp(pan.x, -bounds.width / 2f, bounds.width / 2f);
- pan.y = Mathf.Clamp(pan.y, -bounds.height / 2f, bounds.height / 2f);
- evt.Use();
- }
- break;
- }
- rect.position += pan;
- var center = rect.center;
- rect.size *= zoom;
- rect.center = center;
- }
- private Vector3 RelativeUVPosition(Vector2 vertice) {
- vertice.x *= rect.width;
- vertice.y *= rect.height;
- vertice.y *= -1f;
- vertice.y += rect.height;
- vertice += rect.position;
- vertice.x = Mathf.Clamp(vertice.x, bounds.xMin, bounds.xMax);
- vertice.y = Mathf.Clamp(vertice.y, bounds.yMin, bounds.yMax);
- return vertice;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement