Is there a way to get a sprite's current width/height in game units?

I am trying to write a component that spawns a sprite somewhere within another sprite’s display area. Doesn’t have to be pixel perfect, the object’s bounding box will be good enough, but not the bounding box as expanded by diagonal rotation, so renderer.bounds doesn’t really work for me.

I would also like to be able to locate points relative to a sprite. So, for example, I’d like to be able to figure out where a sprite’s top right corner is in world space. I don’t know how I could do that without the world-unit width/height data.

You have the position of the sprite itself then you can use

Sprite sp = GetComponent<SpriteRenderer>().sprite;
Vector3 [] array = SpriteLocalToWorld(sp);

where:

Vector3[] SpriteLocalToWorld(Sprite sp) 
{
    Vector3 pos = transform.position;
    Vector3 [] array = new Vector3[2];
    //top left
    array[0] = pos + sp.bounds.min;
    // Bottom right
    array[1] = pos + sp.bounds.max;
    return array;
}

In this case the first value of the array is the top left corner and the second value is the bottom right corner. You can extend the method to provide the remaining two corners but you already have all the info you need.

In order to get a point within the sprite you need to do:

 float x = Random.Range(array[0].x, array[1].x);
 float y = Random.Range(array[0].y , array[1].y);
 float z = array[0].z + 0.1f;

the z is just for the sprite to be in front.

Well that should do it

Try this instead, to cover rotation as well.

                public static Vector2 GetPixelSize(this SpriteRenderer spriteRenderer, Camera camera = null)
                {
                    if (spriteRenderer == null) return Vector2.zero;
    
                    if (spriteRenderer.sprite == null) return Vector2.zero;
    
                    float pixelsPerUnit = spriteRenderer.sprite.pixelsPerUnit;
    
                    // Get Up and Right offsets from the size
                    float offsetRight = spriteRenderer.sprite.rect.size.x / 2f / pixelsPerUnit;
                    float offsetUp = spriteRenderer.sprite.rect.size.y / 2f / pixelsPerUnit;
    
                    Vector2 localRight = Vector2.right * offsetRight;
                    Vector2 localUp = Vector2.up * offsetUp;
    
                    // Go to world
                    Vector2 worldRight = spriteRenderer.transform.TransformPoint(localRight);
                    Vector2 worldUp = spriteRenderer.transform.TransformPoint(localUp);
                    Vector2 worldCenter = spriteRenderer.transform.position;
    
                    // Go to pixels
                    Vector2 coordsRight = GetPixelCoordinates(worldRight, camera);
                    Vector2 coordsUp = GetPixelCoordinates(worldUp, camera);
                    Vector2 coordsCenter = GetPixelCoordinates(worldCenter, camera);
    
                    // Get sizes
                    float pixelsRight = Vector2.Distance(coordsCenter, coordsRight);
                    float pixelsUp = Vector2.Distance(coordsCenter, coordsUp);
    
                    Vector2 itemSize = Vector2.right * pixelsRight * 2+ Vector2.up * pixelsUp * 2;
    
                    return itemSize;
                }
   
                public static Vector2 GetPixelCoordinates(this Transform transform, Camera camera = null)
                {
                    if (transform == null) return Vector2.zero;
    
                    return GetPixelCoordinates(transform.position, camera);
                }
    
                private static Vector2 GetPixelCoordinates(Vector3 position, Camera camera)
                {
                    if (camera == null)
                        camera = Camera.main;
    
                    if (camera == null) return Vector2.zero;
    
                    return camera.WorldToScreenPoint(position);
                }