Hello Guest

Author Topic: Trouble getting sprite dimensions  (Read 5097 times)

asea19

  • Newbie
  • *
  • Posts: 5
    • View Profile
Trouble getting sprite dimensions
« on: February 14, 2013, 08:50:22 pm »
Hi, do GetBounds() and GetUntrimmedBounds() return the width and height of a sprite before or after the camera scales things?  Or neither?  I am using the tk2d camera and I have native resolution set to 1136x640 with a resolution override with wdith and height set to -1, Auto Scale set to Fit Visible and Fit Mode set to Center. 

After inspecting the output of these functions on a sprite that is 352 x 107 pixels.  here is what is returned: 'Center: (624.8, 189.9, 0.0), Extents: (624.8, 189.9, 0.0)' .  I am a bit confused!  I see the proportion is about 1.75 of the actual sprite's dimensions.  Is there any significance to this number?  I just need to get the pixel dimensions of the sprite after scaling has occurred so I can position them relatively within the viewport.  Any suggestions?  Thanks!
« Last Edit: February 14, 2013, 09:04:05 pm by asea19 »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Trouble getting sprite dimensions
« Reply #1 on: February 14, 2013, 10:03:08 pm »
With the tk2dCamera, there is no camera scale applied - it is the camera that is tweaked to get stuff to fit / moved around and so on.

Can you please check that the scale on your sprite (scale in the sprite inspector) is 1? sprite.GetBounds will scale by the applied scale.

asea19

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Trouble getting sprite dimensions
« Reply #2 on: February 14, 2013, 10:48:46 pm »
Quote
With the tk2dCamera, there is no camera scale applied - it is the camera that is tweaked to get stuff to fit / moved around and so on.

Interesting. When I add a resolution override on the tk2d Camera inspector, it looks like it is scaling all of the sprites.  That is why I assumed this.  Still a bit confused as to what you mean. 

So, regarding the scale in the sprite inspector.  You are right.  It is not 1,1,1.  It is actually .15.  I assume this has to do with how I am loading the sprites.  I am loading them at runtime and used this thread as a guide on how to do that: http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,1058.msg5282.html#msg5282

Could you glance at this and see what I might be doing wrong?  Thanks again. 

Code: [Select]
var gameObj:GameObject;
var tk_spr:tk2dSprite;
var tempTexture:Texture2D;

tempTexture = Resources.Load( "Textures/MENUS/"+spr_str, Texture2D );
var tSize:Rect = new Rect(0, 0, tempTexture.width, tempTexture.height);
var tAnch:Vector2 = new Vector2(0, tempTexture.height);

gameObj = tk2dSprite.CreateFromTexture( tempTexture, tk2dRuntime.SpriteCollectionSize.Explicit( tempTexture.width, tempTexture.height ), tSize, tAnch );
gameObj.name = spr_str.ToString();
gameObj.layer = LayerMask.NameToLayer("MENU");
tk_spr = gameObj.GetComponent(tk2dSprite);   
tk_spr.collection.transform.parent = gameObj.transform;
tk_spr.pixelPerfect = true;
      

BTW, I printed out tempTexture.width and tempTexture.height and they are the accurate pixel dimensions of the incoming texture.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Trouble getting sprite dimensions
« Reply #3 on: February 14, 2013, 11:59:16 pm »
it should be tk2dRuntime.SpriteCollectionSize.ForTk2dCamera()
That will create it with the correct size for later.
You don't need to set pixel perfect.

asea19

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Trouble getting sprite dimensions
« Reply #4 on: February 15, 2013, 01:14:36 am »
Thanks!  That fixed it.