Editor crash: EditorGUIUtility.Load in combination with GUI.drawTexture

What I want: Use icons (read bitmaps) within my Unity Editor Panel script so that I can display icon- instead text buttons.

What I am at: based on this board message: http://forum.unity3d.com/viewtopic.php?p=229676 I tried to replicate that but Unity crashes every time whenever I try it, I noticed also that often Unity doesn't really reload the script but caches it or just gets stuck.

Proof: Here is a screenshot of the project folder in Unity, booth icon png image and script are within the "Editor" folder.

alt text

This is the SoapEditor.cs script:

using UnityEditor;
using UnityEngine;
using System.Collections;

using System.Reflection;
using System;

public class SoapEditor : EditorWindow {
    [MenuItem("Soap/Soap Editor #e")]//shift + e
    static void Init() {
        SoapEditor win = (SoapEditor)EditorWindow.GetWindowWithRect(typeof(SoapEditor),new Rect(0, 0, 120, 240),true);
    }
    void OnGUI() {
        Texture2D tex = (Texture2D)EditorGUIUtility.Load("EditorIcons.png");
        GUI.DrawTexture(new Rect(0, 0, 16, 16), tex);
    }
}

Any working example would be nice, because I can't even starting working on my editor Interface because unity keeps crashing at this point and even deletes scenes because of inconsistent backups. Thanks in advance,

Hendrik

Most likely it's throwing an exception because something is null, and by the looks of your code, it's the Load bit - The function is for loading built in resources, and looks in a specific folder which isn't Assets/Editor

Instead, you should be using AssetDatabase.LoadAssetAtPath

This should stop it from crashing:

void OnGUI()
{
    Texture2D tex = AssetDatabase.LoadAssetAtPath("Assets/Editor/EditorIcons") as Texture2D;
    if (tex != null)
        GUI.DrawTexture(new Rect(0, 0, 16, 16), tex);
}

It takes a path relative to the project folder, and doesn't use extensions

Better still, you'd store tex once in your init function, then use it in OnGUI (though you'd still check for null in case it's been removed, etc - you can add a Debug.Log in an else if you want to make that clear)