Hello Guest

Author Topic: Simply setting a sprite width/height - how?  (Read 6762 times)

vargonian

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Simply setting a sprite width/height - how?
« on: May 06, 2013, 06:39:41 am »
This is something that I've learned and forgotten several times over.  It's great that I can set the scale of a sprite, but I want to know the best way to actually set its width and height.  There is no width nor height nor size property.  So in the past I've manipulated that scale of the sprite to get it to the exact width that I want based on the size of the original texture.  I always end up needing to create a separate script which serves this purpose, with a Width and Height property which perform this semi-cumbersome math.

Is there a better way?  I need my sprites of a specific size, but in my case I'm using a single pixel sprite that I am stretching into a square.  What's the best way to set the exact size of a sprite?

Zakwil

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Zakstudio
Re: Simply setting a sprite width/height - how?
« Reply #1 on: May 06, 2013, 07:21:30 am »
Maybe you have the same problem I got, check the answer unikron gave me : http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,1620.0.html

(I'll check it tonight).

vargonian

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Simply setting a sprite width/height - how?
« Reply #2 on: May 06, 2013, 07:33:28 am »
That seems like it might be workable.  It's really a shame that there isn't an intuitive "SetPixelSize" or something straightforward like that.  I mean... setting the sprite size seems like such an extremely common operation.  I'm not sure why it needs to be so cumbersome.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Simply setting a sprite width/height - how?
« Reply #3 on: May 06, 2013, 12:01:37 pm »
I can add a function to doing this to a normal "Sprite", but it isn't in the core as it can cause serious confusion when used with custom anchors, etc. The anchor and/or custom anchor is positioned in the sprite, so any dimension scaling is performed around that. Its fine when its one of the edges of the sprite, but when its custom you're gonna get weird rescaling.
And then add the fact that you can have a hierarchy with transform.localScale affecting it...

But thats not to say you can't have some code to do it :)
Here is a class extension to the base sprite class. Save it and drop it somewhere in your project to use it.

Code: [Select]
using UnityEngine;
using System.Collections;

public static class tk2dBaseSpriteExtension {

public static void SetSize(this tk2dBaseSprite sprite, Vector2 pixelSize) {
tk2dSpriteDefinition def = sprite.CurrentSprite;
Vector2 requestedSize = new Vector2( def.texelSize.x * pixelSize.x, def.texelSize.y * pixelSize.y );
Vector3 dimensions = def.untrimmedBoundsData[1];
Vector3 scale = new Vector3( requestedSize.x / dimensions.x, requestedSize.y / dimensions.y, 1 );
sprite.scale = scale;
}

public static Vector2 GetSourcePixelSize( this tk2dBaseSprite sprite ) {
tk2dSpriteDefinition def = sprite.CurrentSprite;
Vector3 dims = def.untrimmedBoundsData[1];
return new Vector2( dims.x / def.texelSize.x, dims.y / def.texelSize.y );
}
}

vargonian

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Simply setting a sprite width/height - how?
« Reply #4 on: May 06, 2013, 07:01:58 pm »
That's beautiful, thank you. :) 

The fact that other people aren't asking about this a lot makes me wonder if I'm just approaching this the wrong way, but I'm not sure.  Maybe most people just design for a specific resolution and create all their sprite textures at exactly the size they need.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Simply setting a sprite width/height - how?
« Reply #5 on: May 06, 2013, 07:36:14 pm »
Out of curiosity, how is it that you work?

vargonian

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Simply setting a sprite width/height - how?
« Reply #6 on: May 07, 2013, 06:29:38 am »
I generally try to make few assumptions about the size of the play space as I'm designing a game.  I want to be able to tweak the dimensions of the game board (for example) and have all the sprites adjust their sizes automatically.

As an example, imagine I'm making a game with a board like chess.  A chess board is 8 squares x 8 squares.  But as I'm designing this hypothetical game, I want to make the board dimensions a variable that I can change at any time.  Perhaps I decide that 10x10 is better.  I want to be able to change the size to 10x10 squares and have all the sprites adjust their sizes and positions automatically. 

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Simply setting a sprite width/height - how?
« Reply #7 on: May 07, 2013, 09:50:09 am »
But can't you do that by getting Untrimmed Bounds on the sprite already? You can make this work without modifying the physical size of sprites by simply changing the camera parameters.

Just something to think about :)

vargonian

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Simply setting a sprite width/height - how?
« Reply #8 on: May 09, 2013, 05:31:27 am »
Hmm, it's possible... in fact it might be easier than I thought.  Although I imagine I might change the UI layout for a tablet versus smartphone... but maybe not!  Hmm... thanks for giving me some ideas!