ファイルの選択ダイアログで選択した画像をウィンドウに表示する。
任意のパスからテクスチャを読み込むメソッドがどれを使えばよくわからなかったので、バイナリデータとして読み込んで LoadImage
で設定した。
using UnityEditor;
using UnityEngine;
using System.IO;
public class ImagePreview : EditorWindow
{
Texture2D texture = null;
[MenuItem("Editor/Image Preview")]
static public void Init()
{
EditorWindow.GetWindow<ImagePreview>();
}
void OnGUI()
{
if (GUILayout.Button("Load...", GUILayout.Width(100)))
{
string path = EditorUtility.OpenFilePanel("Select png", "", "png");
if (path.Length != 0)
{
texture = new Texture2D(1, 1);
texture.LoadImage(File.ReadAllBytes(path));
texture.filterMode = FilterMode.Point;
}
}
if (texture != null)
{
EditorGUI.DrawPreviewTexture(new Rect(25, 40, 256, 256), texture);
}
}
}