- Home /
How to draw a Sprite on OnDrawGizmos()
Hello,
I'm trying to build a method for draw a single sprite on the scene, only for editing and I don't want to use SpriteRenderer, seems to work fine with textures with 1 pixelPerUnit and integer positions, but with float values it behaves weird.
[void DrawSprite(Sprite sprite, Vector3 position) {
Rect dstRect = new Rect (position.x - sprite.bounds.max.x,
position.y + sprite.bounds.max.y,
sprite.bounds.size.x,
-sprite.bounds.size.y);
Rect srcRect = new Rect (sprite.rect.x / sprite.texture.width,
sprite.rect.y / sprite.texture.height,
sprite.rect.width / sprite.texture.width,
sprite.rect.height / sprite.texture.height);
Graphics.DrawTexture(dstRect,
sprite.texture,
srcRect,
0,0,0,0);
}
void OnDrawGizmosSelected() {
DrawSprite(this.sprite, transform.position);
}][1]
[1]: /storage/temp/47793-drawspritemov.zip
drawspritemov.zip
(396.0 kB)
Comment