- Home /
Drawing a texture in a custom PropertyDrawer
I am trying to create a custom property drawer for a type that should be displayed as a 2D image, but when I use EditorGUI.DrawPreviewTexture or GUI.DrawTexture to draw the image, it disappears after a short time, and reappears for a short time when the inspector is updated. How do I properly draw a texture in a PropertyDrawer?
Minimal code:
[CustomPropertyDrawer (typeof (testObj))]
public class testObjDrawer : PropertyDrawer {
int tSize = 128;
Texture2D texture;
public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label) {
if (texture == null)
InitializeTexture();
EditorGUI.BeginProperty (pos, label, prop);
pos = EditorGUI.PrefixLabel (pos, GUIUtility.GetControlID(FocusType.Passive), label);
EditorGUI.DrawPreviewTexture(pos, texture);
EditorGUI.EndProperty ();
}
private void InitializeTexture() {
texture = new Texture2D(tSize, tSize,TextureFormat.RGB24,false);
//tex = new Texture2D(tSize, tSize);
Color32[] clr = new Color32[tSize * tSize];
for (int i = 0; i < clr.Length; i++)
clr[i] = Color.black;
texture.SetPixels32(clr);
texture.Apply();
}
}
Answer by MalikDrako · Jan 15, 2013 at 12:54 AM
I'm starting to think this is a bug, because it did the same thing when I tried a conversion of the EditorWindow example for EditorGUI.DrawPreviewTexture. I did find a workaround involving GUIStyle
GUIStyle style = new GUIStyle();
style.normal.background = texture;
EditorGUI.LabelField(position, GUIContent.none, style);
Unity has confirmed this is a bug and is working on a fix
Rawr... thanks for the info. Any inspiration how I could easily replace GUI.DrawTextureWithTexCoords?
You could use GetPixels()
and store it in a local texture
Here we are a year and a half later and the bug is still not fixed.
Answer by Simon Says · May 25, 2014 at 05:14 AM
I found out that the texture doesn't flicker if the PropertyDrawer is drawn by a custom Editor. If you know where your PropertyDrawer is going to be used, simply make an empty stub deriving from the Editor for that object, like this:
[CustomEditor(typeof(ScriptableObject))]
public class ScriptableObjectInspector : Editor
{
// Empty stub so a custom PropertyDrawer with textures would work.
// (Workaround for Unity bug #514655.)
}
That's the closest I got to drawing textures with custom texture coordinates (e.g. sprites).
However, remember to put your drawing calls behind:
if (Event.current.type == EventType.Repaint) {
EditorGUI.DrawPreviewTexture(rect, texture);
}
Or you'll get garbage in the supplied position rectangle. The documentation only mentions it for Graphics.DrawTexture(), but it is also needed in this case.
Thank you so much! This is the only solution that worked for me.
Answer by numberkruncher · Sep 19, 2014 at 09:05 PM
I had this issue and came up with a similar workaround to @MalikDrako except without allocating GUIStyle
's each time OnGUI events occur.
This aims to function as a drop-in replacement for GUI.DrawTexture
:
public static class GUIHelper {
private static GUIStyle s_TempStyle = new GUIStyle();
public static void DrawTexture(Rect position, Texture2D texture) {
if (Event.current.type != EventType.Repaint)
return;
s_TempStyle.normal.background = texture;
s_TempStyle.Draw(position, GUIContent.none, false, false, false, false);
}
}
Perfect thank you! This solution works on Unity 2017.2.0f3
Answer by Edy · Sep 22, 2014 at 05:07 PM
Two alternatives to DrawPreviewTexture that don't use GUIStyle and use the ScaleToFit scaling method: scale down the texture keeping the aspect ratio only when position is smaller than the texture rect.
// ScaleToFit method, left-aligned at the position of the prefix label
EditorGUI.LabelField(position, new GUIContent(texture));
// ScaleToFit method, horizontally centered, bottom-aligned, shadow effect
EditorGUI.DropShadowLabel(position, new GUIContent(texture));
I guess that everything use GUIStyle at some point, but I meant that you don't have to create or configure a GUIStyle object explicitly.