Reputation: 12621
This question only relates to the Unity game engine.
Here's a visual example of a so-called Unity "sprite" (Unity Sprite class) which is a thin 3D object.
AABB shown (green lines). In Unity, live AABB off the gpu is given via .bounds
(doco). If new to CG, explanation of AABB.
(Note, "local" in Unity (doco, doco) means: in the quaternion of the parent object; "world" means in the quaternion of the scene object.)
Now! For any Unity engineers reading:
Normally in Unity, with a visible object, when you want to get the Unity Bounds,
if you want them in world space, renderer.bounds
returns the Unity Bounds in world space.
if you want them in local space, mesh.bounds
returns the Unity Bounds in local space.
This is a basic of Unity and you do it all the time. However. With the new sprite, that is to say SpriteRenderer
, there's no real access to the mesh as far as I know.
(You can actually get the .triangles, but it would be quite a chore to reconstruct the winding and size from there.)
How then, can you get the local Unity Bounds for a Sprite?
(Note that sprite.bounds
itself is just the same renderer.bounds
, that doesn't help.)
Upvotes: 4
Views: 2935
Reputation: 378
I think you're looking for SpriteRenderer.sprite.bounds
.
public class LogBounds : MonoBehaviour {
void OnGUI () {
var r = GetComponent<SpriteRenderer>();
GUILayout.Label("SpriteRenderer.bounds center " + r.bounds.center + " size " + r.bounds.size);
GUILayout.Label("SpriteRenderer.sprite.bounds center " + r.sprite.bounds.center + " size " + r.sprite.bounds.size);
}
}
Note that the values for SpriteRenderer.sprite.bounds do not change as the transform's properties are changed -- those values are in local coordinates.
Upvotes: 6