Hello Guest

Author Topic: Bounds and Size analogues to SpriteRenderer?  (Read 10509 times)

Kein

  • Newbie
  • *
  • Posts: 6
    • View Profile
Bounds and Size analogues to SpriteRenderer?
« on: July 12, 2017, 08:22:02 pm »
I'm looking for something that will return the same or similar values as SpriteRenderer's:
.sprite.rect.size;
.sprite.pixelsPerUnit;

I can get bounds with GetBounds and even though it returns different format I can wok with it through hoops. But what about PPU data?

My goal is to calculate height of a [current]sprite in displayed pixels. I did some search but nothing really came up.
« Last Edit: July 19, 2017, 12:47:01 am by Kein »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Bounds and Size analogues to SpriteRenderer?
« Reply #1 on: July 21, 2017, 09:08:34 pm »
Hi there,

If you get renderer.bounds, that gives you the bounds in world units. Multiply by by PPU and you get number of pixels.

Sprite.GetGounds() returns the sprite bounds in local space - assuming the sprite isn't scaled when drawn - multiply that by PPU and you get pixels.


Kein

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Bounds and Size analogues to SpriteRenderer?
« Reply #2 on: July 21, 2017, 11:17:38 pm »
The problem is that I do it all runtime, I don't have PPU data. If I had PPU value I wouldn't need to ask how to get PPU :P

The example script for SpriteRenderer I wanted to adopt for tk2DSprite: http://answers.unity3d.com/questions/1042119/getting-a-sprites-size-in-pixels.html
« Last Edit: July 22, 2017, 01:15:44 am by Kein »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Bounds and Size analogues to SpriteRenderer?
« Reply #3 on: July 23, 2017, 06:11:25 pm »
Ok there is some misconception here about ppu. The ppu is an almost arbitrary mapping that tells unity how big to create the sprite geometry. It really doesn't matter what it is - where you'll find it does matter is to get a camera working in a pixel size.

Lets say you create sprites at 1ppu, and create a tk2dCamera with 1ppu (pixels per meter on the tk2dCamera). Then your sprite bounds = pixel size, and you don't have to care about the actual ppu. If you use the same mapping for everyhting, the actual pixel size doesn't really matter.

If you explain what it is you're trying to do exactly - eg. are you trying to position something in pixel units, etc. I can advise you how to go about it the best way.

Kein

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Bounds and Size analogues to SpriteRenderer?
« Reply #4 on: July 23, 2017, 08:30:36 pm »
The project I'm "working" with uses tk2dSprite. I don't have much of an access and freedom, I'm limited to runtime scripting only. My goal is to get center coordinates of a gameobjects that has tk2d spriterenderer/animator attached, calculate the current sprite display height and adjust the offset of custom displayed element to be below or above the current sprite bounds.
The issue is with second part of getting display height in pixels and using half of it as an offset.

UPD: no longer relevant, used this to get display pixel size: https://forum.unity3d.com/threads/how-to-scale-gameobject-to-pixel-width-and-height.130382/#post-880163

However, if you can tel me how to get the EXACT sprite center by Y in world coordinates - that would be swell. The gameobject .y position is not always correct relatively to its sprite/mesh
« Last Edit: July 24, 2017, 06:14:58 am by Kein »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Bounds and Size analogues to SpriteRenderer?
« Reply #5 on: July 24, 2017, 09:16:19 pm »
gameObject.renderer.bounds.center should give you the exact center of the sprite in world coordinates.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Bounds and Size analogues to SpriteRenderer?
« Reply #6 on: July 24, 2017, 09:17:37 pm »
I mentioned this before, but it seems like it may help you - if you use 1 pixel per world unit (and you're not using unity physics)... you can then just think in pixels and skip all the conversions between spaces. If you don't use physics there is no real downside to this

Kein

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Bounds and Size analogues to SpriteRenderer?
« Reply #7 on: July 25, 2017, 05:41:38 am »
As I said I'm limited to runtime only so I don't know pixel per world unit value. In fact, I made this thread to get them.\

Okay, so here is a problem:
http://answers.unity3d.com/questions/1384005/how-to-a-scale-worldtoscreenpoint-parts-of-the-gui.html

As I understand, the bounds and following conversion I do there gives me not the display size of a sprite in pixels but NATIVE one on my 1080p. I'm back to square one - I need to find a way to calculate  current displayed pixel size of a sprite that won't be dependent on the screen resolution.

It really Saddens me that  tk2sprite does not have simple .rect and .PPU like SpriteRenderer.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Bounds and Size analogues to SpriteRenderer?
« Reply #8 on: July 26, 2017, 04:54:28 pm »
I have no objections to adding a rect function - and I am perfectly happy to do it for the next update, but I think there is something

The PPU on the other hand is a completely arbitrary made up by Unity. You still need a camera thats set up somehow, and for consistencies sake I assume you'd use the same pixel per world unit value everywhere in your project. Do you tend to use different ppu values for different sprites for unity sprites?

I need to find a way to calculate  current displayed pixel size of a sprite that won't be dependent on the screen resolution.
This implies you just need your "constant" ppu that you're using throughout the whole project, the world space bounds * that constant ppu value will be your "pixel size". In fact the world space bounds will be constant as well, you don't even need to multiply by ppu to position things, the world bounds and positions work just fine.

That is my main issue with the pixels per unit value in unity - all it says is how big the sprite is. It has no bearing with actual pixel size, or how many pixels it maps to on screen / world space.

I may be missing something crucial here... please explain if you can, use the support email if you prefer. If you can send me a repro case I will be more than happy to look.

Kein

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Bounds and Size analogues to SpriteRenderer?
« Reply #9 on: July 29, 2017, 08:51:52 am »
  I may be missing something crucial here... please explain if you can, use the support email if you prefer. If you can send me a repro case I will be more than happy to look.
Just trying to adapt my script to correctly display GUI element when using WorldToScreenpoint. Right now I'm using custom Matrix for GUI itself but it is not applicable when using WorldToScreenpoint so I need to offset position of GUI element, based on sprite dimension data, manually for different than native resolution.

Hmm.

May be I'm going the wrong way about it. OK, so let's say I'm generating a simple white dummy Texture2D at runtime. Now, I have a list of objects that meet specific criteria and have tk2dSprite component attached. I make a dummy newGameObject, make it a child of TargetGameObject, attach  to newGameObject tk2dSprite comp. and my script, which uses tk2d to generate simple sprite rectangle and then I use local scale of parent to display spoken sprite beneath or on top of main sprite thus eliminating the issue of screen resolution scaling.

Is something like that doable runtime with tk2d? I'm currently looking at http://2dtoolkit.com/forum/index.php/topic,5417.msg24593.html#msg24593
« Last Edit: July 29, 2017, 09:51:19 am by Kein »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Bounds and Size analogues to SpriteRenderer?
« Reply #10 on: July 31, 2017, 10:35:53 pm »
Code: [Select]
Is something like that doable runtime with tk2d? I'm currently looking at http://2dtoolkit.com/forum/index.php/topic,5417.msg24593.html#msg24593That's probably not what you want - that batches up sprites in one object, but

Code: [Select]
May be I'm going the wrong way about it. OK, so let's say I'm generating a simple white dummy Texture2D at runtime. Now, I have a list of objects that meet specific criteria and have tk2dSprite component attached. I make a dummy newGameObject, make it a child of TargetGameObject, attach  to newGameObject tk2dSprite comp. and my script, which uses tk2d to generate simple sprite rectangle and then I use local scale of parent to display spoken sprite beneath or on top of main sprite thus eliminating the issue of screen resolution scaling.
I don't really follow - there are too many ambiguous bits in there for me to try and follow through and understand exactly what you are trying to achieve. If you can create a simple test case for this and post it here I'll be happy to look.

FWIW - It sounds like what you want (if i understand correctly and have made the correct assumptions) should be doable quite easily.