Created
September 2, 2015 13:12
-
-
Save reinsteam/08c3711f43eaa2b183ba to your computer and use it in GitHub Desktop.
Simple previewer for texture properties in Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class PreviewTexture2D : PropertyAttribute | |
{ | |
public PreviewTexture2D() | |
{ } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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