Eonirr

Untitled

May 19th, 2025
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. [CustomPropertyDrawer(typeof(TestSprite))]
  2. public class TestSpriteDrawer : PropertyDrawer
  3. {
  4. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  5. {
  6. float spriteHeight = 130f;
  7. float restHeight = EditorGUI.GetPropertyHeight(property, label, true);
  8. return spriteHeight + restHeight;
  9. }
  10.  
  11. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  12. {
  13. EditorGUI.BeginProperty(position, label, property);
  14.  
  15. SerializedProperty spriteProp = property.FindPropertyRelative("mySpriteTest");
  16.  
  17. Rect spriteRect = new Rect(position.x, position.y, 128, 128);
  18. Object newSprite = EditorGUI.ObjectField(spriteRect, spriteProp.objectReferenceValue, typeof(Sprite), false);
  19. if (newSprite != spriteProp.objectReferenceValue)
  20. {
  21. spriteProp.objectReferenceValue = newSprite;
  22. }
  23.  
  24. Sprite sprite = spriteProp.objectReferenceValue as Sprite;
  25. if (sprite != null)
  26. {
  27. Texture2D texture = AssetPreview.GetAssetPreview(sprite);
  28.  
  29. if (texture != null) GUI.DrawTexture(spriteRect, texture, ScaleMode.ScaleToFit);
  30. }
  31.  
  32. float offsetY = spriteRect.yMax + 4f;
  33. Rect restRect = new Rect(position.x, offsetY, position.width, position.height - offsetY);
  34.  
  35. EditorGUI.PropertyField(restRect, property, label, true);
  36.  
  37. EditorGUI.EndProperty();
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment