-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathReferenceImageViewer.cs
More file actions
61 lines (52 loc) · 2.04 KB
/
ReferenceImageViewer.cs
File metadata and controls
61 lines (52 loc) · 2.04 KB
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
// simple image viewer inside unity editor
// use case: to keep reference image visible while working in single monitor
using UnityEngine;
using UnityEditor;
namespace UnityLibrary
{
public class ReferenceImageViewer : EditorWindow
{
Texture2D tex;
bool keepAspectRatio = false;
[MenuItem("Window/Tools/Reference Image Viewer")]
static void Init()
{
ReferenceImageViewer window = (ReferenceImageViewer)EditorWindow.GetWindow(typeof(ReferenceImageViewer));
window.titleContent = new GUIContent("ReferenceImageViewer");
window.Show();
}
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Image", GUILayout.Width(50));
tex = (Texture2D)EditorGUILayout.ObjectField(tex, typeof(Texture2D), true, GUILayout.MinWidth(100));
GUILayout.FlexibleSpace();
keepAspectRatio = EditorGUILayout.ToggleLeft("KeepAspect", keepAspectRatio);
EditorGUILayout.EndHorizontal();
if (tex != null)
{
int topOffset = 20;
// prep
var maxWidth = position.width;
var maxHeight = position.height - topOffset;
var imgWidth = (float)tex.width;
var imgHeight = (float)tex.height;
// calc
var widthRatio = maxWidth / imgWidth;
var heightRatio = maxHeight / imgHeight;
var bestRatio = Mathf.Min(widthRatio, heightRatio);
// output
var newWidth = imgWidth * bestRatio;
var newHeight = imgHeight * bestRatio;
if (keepAspectRatio == true)
{
EditorGUI.DrawPreviewTexture(new Rect(0, topOffset, newWidth, newHeight), tex);
}
else
{
EditorGUI.DrawPreviewTexture(new Rect(0, topOffset, maxWidth, maxHeight), tex);
}
}
}
}
}