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?
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.
(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.
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 :
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
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.
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.
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.
// Square box style
// drop your sprite here
sprite = EditorGUILayout.ObjectField(sprite, typeof(Sprite), true,
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:
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:
// Querying the field since the UI is created with UIToolkit.
ObjectField skillIconField = rightPane.Q<ObjectField>(name = "skill-icon-field");
// Binding the property I want to the field.
skillIconField.BindProperty(serializedSkill.FindProperty(nameof(Skill.skillData)).FindPropertyRelative(nameof(SkillData.Icon)));