• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by MalikDrako · Jan 09, 2013 at 01:52 AM · texturepropertydrawer

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();
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

8 Replies

· Add your reply
  • Sort: 
avatar image
1

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));
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image numberkruncher · Sep 22, 2014 at 05:45 PM 0
Share

Internally this would still use GUIStyle.

avatar image Edy numberkruncher · Jan 13, 2016 at 06:22 PM 0
Share

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.

avatar image
2

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);
     }
 
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BoraxKid · Nov 22, 2017 at 04:49 PM 0
Share

Perfect thank you! This solution works on Unity 2017.2.0f3

avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nsfnotthrowingaway · Nov 20, 2019 at 10:09 PM 0
Share

Thank you so much! This is the only solution that worked for me.

  • ‹
  • 1
  • 2

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assigning UV Map to model at runtime 0 Answers

Texture2D in ScriptableObject’s Property drawer experiences serious lag 3 Answers

Property Drawer To Rename Variable and Make Serializable 0 Answers

Texture on plane not displaying properly 0 Answers

DXF imported, Textures don't work 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges