Search Unity

How do I Create a sprite preview in inspector?

Discussion in '2D' started by GBCFraser, Feb 4, 2014.

  1. GBCFraser

    GBCFraser

    Joined:
    Apr 11, 2013
    Posts:
    94
    I'm trying to load my sprite into the inspector, to make an editor extension. The problem is that the sprite is part of a larger texture. I just want to draw the portion of the texture that holds a static sprite.
    How do I do this?

    Code (csharp):
    1.  
    2. public override void OnInspectorGUI() {
    3. //...
    4. Rect spriteSheetRect = GUILayoutUtility.GetRect( 32.0f, 32.0f, GUILayout.ExpandWidth( false ), GUILayout.ExpandHeight(false) );
    5. EditorGUI.DrawPreviewTexture(new Rect(0,0,50, 50),S_C.texture);
    6. //...
    7. }
     
    lucbloom likes this.
  2. GBCFraser

    GBCFraser

    Joined:
    Apr 11, 2013
    Posts:
    94
    FYI Solved it using the Texture2D.SetPixels, to make the sprite into its own texture. It sucks that I couldn't pass a sprite preview into the inspector.
     
    lucbloom likes this.
  3. FireSBurnsmuP

    FireSBurnsmuP

    Joined:
    Sep 12, 2013
    Posts:
    1
    (Sorry for necro, but info on this is pretty sparse)
    Could you, by chance, post a code sample using your said solution? That would greatly help the community (read: "me") in replicating your result. ^_^ Many thanks.
     
    lucbloom and Ziplock9000 like this.
  4. luislodosm

    luislodosm

    Joined:
    Apr 26, 2016
    Posts:
    26
    In custom inspector:
    Code (CSharp):
    1. public override void OnInspectorGUI()
    2. {
    3.     var texture = AssetPreview.GetAssetPreview(sprite);
    4.     GUILayout.Label(texture);
    5. }
     
    lucbloom, Sarkahn, cdr9042 and 2 others like this.
  5. Estecka

    Estecka

    Joined:
    Oct 11, 2013
    Posts:
    62
    If you need a preview that allow selecting a sprite (like in the script import settins or some material editors), you can use a specifc overload of
    EditorGUI.ObjectField
    . By feeding it the
    typeof(Sprite)
    parameter, the resulting field will be shaped like this :
    Sprite.png
     
  6. biodam

    biodam

    Joined:
    Jul 10, 2013
    Posts:
    17
  7. Avalin

    Avalin

    Joined:
    Oct 12, 2018
    Posts:
    98
    Pardon the necro, I'm having problems with this implementation. My sprite is part of a spritesheet, and when I get the texture it just shows each and every sprite in said spritesheet
     
  8. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Avalin

    Well you can load the sprite asset, it will contain all the subassets. First one is the root asset. Path (in this case) is your sprite asset path.
    Code (CSharp):
    1.  UnityEngine.Object[] asset = AssetDatabase.LoadAllAssetsAtPath(path);
    Then take sub assets from this array and make those into previews. And this is actually IMGUI / Editor scripting question... but anyway that should work.
     
    Last edited: Oct 2, 2020
  9. slgremory

    slgremory

    Joined:
    Jan 5, 2019
    Posts:
    5
    Sorry to necro this, but could you show me the exact code you used to get this output? It's precisely what I need and I haven't been able to get it to work after several hours of tinkering.
     
  10. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    I know you asked someone else, this is quite an old thread (and a wrong forum too) but I guess I could answer.

    Something like this?

    You would have this code in your custom editor's OnInspectorGUI method for example, and "sprite" is a field of type Sprite in your custom editor.
    Code (CSharp):
    1. // Square box style
    2. // drop your sprite here
    3. sprite = EditorGUILayout.ObjectField(sprite, typeof(Sprite), true,
    4. GUILayout.Height(48), GUILayout.Width(48)) as Sprite;

    It will create a field like this here, you can drop or browse a sprite and it will show it:

    upload_2022-8-22_16-15-44.png
     
    Daniellos and Rachan like this.
  11. NotlawGD

    NotlawGD

    Joined:
    Oct 24, 2022
    Posts:
    3
    Thanks for sharing this, exactly what I was looking for!
     
  12. NotlawGD

    NotlawGD

    Joined:
    Oct 24, 2022
    Posts:
    3
    Sorry for replying again, but I ran into an issue.

    I'm trying to get the same result by using UIElements and UIToolkit. Everything works except that the field isn't displaying a preview. It sets the value properly tho. Any idea on why this happens?

    This is what I'm trying to do:
    Code (CSharp):
    1. // Querying the field since the UI is created with UIToolkit.
    2.         ObjectField skillIconField = rightPane.Q<ObjectField>(name = "skill-icon-field");
    3.         // Binding the property I want to the field.
    4.         skillIconField.BindProperty(serializedSkill.FindProperty(nameof(Skill.skillData)).FindPropertyRelative(nameof(SkillData.Icon)));
    This is the result I'm getting:
    upload_2022-11-13_13-56-23.png
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
  14. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    781
    Thank you very much!!!
     
  15. NikolayHadzhiev

    NikolayHadzhiev

    Joined:
    Jan 20, 2018
    Posts:
    4
    For anybody looking at this old thread, let me save you some time: these solutions are deprecated, here's the right way:
    Code (CSharp):
    1.  
    2. EditorGUILayout.BeginHorizontal();
    3. Layout.PrefixLabel("Source Texture");
    4. texture = (Texture2D)EditorGUILayout.ObjectField(texture, typeof(Texture2D), allowSceneObjects: true);
    5. EditorGUILayout.EndHorizontal();
    6.  
    This results in a GUI like this:
     
    TheAshenWolf likes this.