• 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
0

Answer by christoffer.enedahl.hellothere · Jul 24, 2015 at 10:20 AM

This script copies the sprite to a new texture, not optimized but seems to work. Caveat: The texture must be set as Read/Write Enabled in import settings.

 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);
     }
 
     public static void DrawTexture(Rect position, Sprite sprite)
     {
         if (Event.current.type != EventType.Repaint)
             return;
 
         //The texture must be set as Read/Write Enabled in import settings.
 
         Texture2D texture = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height, sprite.texture.format, false);
         Color[] pixels = sprite.texture.GetPixels((int)sprite.rect.x, (int)sprite.rect.y, (int)sprite.rect.width, (int)sprite.rect.height);
         texture.SetPixels(pixels);
         texture.Apply();
 
         s_TempStyle.normal.background = texture;
         s_TempStyle.Draw(position, GUIContent.none, false, false, false, false);
     }
 
 }
Comment
Add comment · 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
0

Answer by ecesis_llc · Jul 01, 2017 at 01:15 AM

Unity: 5.6.1f1

Couldn't get scriptableobject sprites to draw consistently in a property drawer. The default EditorGUI.ObjectField when large enough to show the icon would randomly flicker the sprite and render it with a delay after clicking in the inspector panel.

Bunch of helpful tips in this thread. In the end I changed the height of the ObjectField so the default icon is not drawn and use a GUIStyle to render the sprite as a background. Using this method I can mash the arrow key down through my scriptableobjects and the sprites show instantly with now weirdness in the inspector.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 [CustomPropertyDrawer(typeof(Sprite))]
 public class SpriteDrawer : PropertyDrawer {
 
     private static GUIStyle s_TempStyle = new GUIStyle();
     
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {                                        
         var ident = EditorGUI.indentLevel;
         EditorGUI.indentLevel = 0;
 
         Rect spriteRect;
         
         //create object field for the sprite
         spriteRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
         property.objectReferenceValue = EditorGUI.ObjectField(spriteRect, property.name, property.objectReferenceValue, typeof(Sprite), false);
 
         //if this is not a repain or the property is null exit now
         if (Event.current.type != EventType.Repaint || property.objectReferenceValue == null)
             return;
 
         //draw a sprite
         Sprite sp = property.objectReferenceValue as Sprite;
 
         spriteRect.y += EditorGUIUtility.singleLineHeight+4;
         spriteRect.width = 64;
         spriteRect.height = 64;        
         s_TempStyle.normal.background = sp.texture;
         s_TempStyle.Draw(spriteRect, GUIContent.none, false, false, false, false);
                 
         EditorGUI.indentLevel = ident;
     }
 
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return base.GetPropertyHeight(property, label) + 70f;
     }
 }
Comment
Add comment · 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
2

Answer by DrummerB · Aug 15, 2018 at 07:53 PM

Since 2017.3 you can override CanCacheInspectorGUI to fix this issue:

 public override bool CanCacheInspectorGUI(SerializedProperty property)
 {
     return false;
 }

More details here.

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 Nonym · Feb 25, 2019 at 04:06 PM 0
Share

Thanks, this is the best solution.

  • ‹
  • 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