LoginSignup
13
9

More than 5 years have passed since last update.

Unityでエディタスクリプトから画像ファイルを読み込んで表示する

Posted at

ファイルの選択ダイアログで選択した画像をウィンドウに表示する。

任意のパスからテクスチャを読み込むメソッドがどれを使えばよくわからなかったので、バイナリデータとして読み込んで LoadImage で設定した。

result_ss.png

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);
        }
    }
}
13
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
9