Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 26927e8

Browse files
Create ReferenceImageViewer2.cs
1 parent 4005a5a commit 26927e8

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// reference image viewer from external folder
2+
3+
using UnityEngine;
4+
using UnityEditor;
5+
using System.IO;
6+
7+
namespace UnityLibrary.Tools
8+
{
9+
public class ReferenceImageViewer2 : EditorWindow
10+
{
11+
private const string EditorPrefsKey = "ReferenceImageViewer_ImagePath";
12+
13+
[SerializeField]
14+
private string imagePath = "";
15+
[SerializeField]
16+
private Texture2D loadedImage;
17+
18+
[MenuItem("Window/Reference Image Viewer")]
19+
public static void ShowWindow()
20+
{
21+
GetWindow<ReferenceImageViewer2>("Reference Image Viewer");
22+
}
23+
24+
private void OnEnable()
25+
{
26+
// Delay the reload to ensure Unity's layout system is ready
27+
EditorApplication.delayCall += TryLoadImage;
28+
}
29+
30+
private void OnDisable()
31+
{
32+
if (!string.IsNullOrEmpty(imagePath))
33+
EditorPrefs.SetString(EditorPrefsKey, imagePath);
34+
else
35+
EditorPrefs.DeleteKey(EditorPrefsKey);
36+
}
37+
38+
private void TryLoadImage()
39+
{
40+
imagePath = EditorPrefs.GetString(EditorPrefsKey, "");
41+
if (!string.IsNullOrEmpty(imagePath) && File.Exists(imagePath))
42+
{
43+
LoadImageFromPath(imagePath);
44+
Repaint(); // Ensure it's drawn
45+
}
46+
}
47+
48+
void OnGUI()
49+
{
50+
GUILayout.Label("Reference Image Viewer", EditorStyles.boldLabel);
51+
52+
if (GUILayout.Button("Browse for Image"))
53+
{
54+
string path = EditorUtility.OpenFilePanel("Select Image", "", "png,jpg,jpeg");
55+
if (!string.IsNullOrEmpty(path))
56+
{
57+
imagePath = path;
58+
EditorPrefs.SetString(EditorPrefsKey, imagePath);
59+
LoadImageFromPath(imagePath);
60+
}
61+
}
62+
63+
// Fallback in case delayCall missed
64+
if (loadedImage == null && !string.IsNullOrEmpty(imagePath) && File.Exists(imagePath))
65+
{
66+
LoadImageFromPath(imagePath);
67+
Repaint();
68+
}
69+
70+
if (loadedImage != null)
71+
{
72+
GUILayout.Space(10);
73+
74+
float windowWidth = position.width - 20;
75+
float imageAspect = (float)loadedImage.width / loadedImage.height;
76+
77+
float displayWidth = windowWidth;
78+
float displayHeight = windowWidth / imageAspect;
79+
80+
if (displayHeight > position.height - 100)
81+
{
82+
displayHeight = position.height - 100;
83+
displayWidth = displayHeight * imageAspect;
84+
}
85+
86+
Rect imageRect = GUILayoutUtility.GetRect(displayWidth, displayHeight, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false));
87+
EditorGUI.DrawPreviewTexture(imageRect, loadedImage, null, ScaleMode.ScaleToFit);
88+
}
89+
}
90+
91+
private void LoadImageFromPath(string path)
92+
{
93+
try
94+
{
95+
byte[] fileData = File.ReadAllBytes(path);
96+
loadedImage = new Texture2D(2, 2, TextureFormat.RGBA32, false);
97+
if (!loadedImage.LoadImage(fileData))
98+
{
99+
Debug.LogError("Failed to load image.");
100+
loadedImage = null;
101+
}
102+
}
103+
catch (System.Exception e)
104+
{
105+
Debug.LogError($"Could not load image from path: {path}\n{e.Message}");
106+
loadedImage = null;
107+
}
108+
}
109+
}
110+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /