Skip to content

Instantly share code, notes, and snippets.

@reinsteam
Created September 2, 2015 13:12
Show Gist options
  • Save reinsteam/08c3711f43eaa2b183ba to your computer and use it in GitHub Desktop.
Save reinsteam/08c3711f43eaa2b183ba to your computer and use it in GitHub Desktop.
Simple previewer for texture properties in Unity
using UnityEngine;
public class PreviewTexture2D : PropertyAttribute
{
public PreviewTexture2D()
{ }
}
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(PreviewTexture2D))]
public class PreviewTexture2DDrawer : PropertyDrawer
{
static private int BorderWidth = 8;
static private bool SwitchAlpha = false;
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
var buttonName = SwitchAlpha ? "Alpha" : "RGB";
if (GUILayout.Button (buttonName, "MiniButton"))
{
SwitchAlpha = !SwitchAlpha;
}
var rect = GUILayoutUtility.GetAspectRect (1.0f);
rect.x += BorderWidth;
rect.y += BorderWidth;
rect.width -= BorderWidth * 2;
rect.height -= BorderWidth * 2;
var texture = property.objectReferenceValue as Texture;
if (SwitchAlpha)
{
EditorGUI.DrawTextureAlpha(rect, texture);
}
else
{
EditorGUI.DrawPreviewTexture(rect, texture);
}
EditorGUI.EndProperty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment