- Home /
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.
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
Answer by Mike 3 · Jun 29, 2010 at 10:27 AM
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)
AssetDatabase.LoadAssetAtPath is the correct function to use - but your sample won't work, as AssetDatabase.LoadAssetAtPath does not take a path relative to the Assets folder, but relative to the project folder, and it requires the file extension. So it should be AssetDatabase.LoadAssetAtPath("Assets/Editor/EditorIcons.png").
Thanks guys, no it finally works for me. The whole Assets/Assets and from which point Unity counts the root or project folder is often confusing for me. I got it now working so that I can continue developing
Nice to hear it :D could you mark the question as answered so it doesn't pop up once a week on the front page? :D