- Home /
How can I open the Sprite Editor from script?
I wanted to make a tool that groups several related functions for our game together, to make things more accessible for artists, and one of these is editing the slicing of a sprite.
Normally you can open windows which appear in the menus from script, but the Sprite Editor is only available from a button in the inspector after you select a sprite or spritesheet asset.
Given a particular sprite or spritesheet asset, how can I make a button in an Editor script which opens the sprite editor window for that sprite?
Answer by Adam-Mechtley · Sep 08, 2016 at 01:02 PM
Hi @DinostabOMG! Unfortunately there is no public API for doing this. If you don't mind using reflection and being subject to a little volatility, you can do something like this:
using System.Linq;
using UnityEditor;
using UnityEngine;
void OnOpenSpriteEditor(Sprite sprite)
{
Selection.activeObject = sprite;
EditorWindow.GetWindow(typeof(EditorWindow).Assembly.GetTypes().Where(t => t.Name == "SpriteEditorWindow").FirstOrDefault());
}
using full namespace would be better
Type type = System.Type.GetType("UnityEditor.SpriteEditorWindow,UnityEditor");
EditorWindow.GetWindow(type );
The SpriteEditorWindow type has changed. This is how you get the type on Unity 2019.3:
GetWindow(Type.GetType("UnityEditor.U2D.Sprites.SpriteEditorWindow,Unity.2D.Sprite.Editor"));
The SpriteEditorWindow has change. This is how you get the type on Unity 2019.3 Type.GetType("UnityEditor.U2D.Sprites.SpriteEditorWindow,Unity.2D.Sprite.Editor"));