Search Unity

Sprite shape bounds not set properly until visible in camera

Discussion in '2D' started by ETGgames, May 11, 2022.

  1. ETGgames

    ETGgames

    Joined:
    Jul 10, 2015
    Posts:
    101
    I have a sprite shape that gets spawned in the awake of some monobehaviour. Now that sprite shape is quite long, it goes way off camera. There are 2 issues.

    The first issue is that the sprite shape is invisible until I change the camera size to something bigger (not necessarily to fully encapsulate the sprite shape, just some arbitrary size that encapsulates most of it I think)
    The sprite shape is definitely there, the geometry collider I generated for it is the right shape and interacts with other physics objects.

    And secondly, the renderer bounds are completely incorrect until I zoom out in the same way (I'm assuming both issues are related to the renderer not loading until more of the sprite shape is in view)

    Once the sprite shape is visible, the renderer.bounds are correct.

    Something interesting to note is that accessing the bounds with renderer.GetBounds()[0] gives the correct size, but wrong center, even before zooming out and making the sprite shape visible.

    I've tried all the 'refresh' methods I can think of

    // SpriteShapeController.RefreshSpriteShape();
    // SpriteShapeController.UpdateSpriteShapeParameters();
    // SpriteShapeController.BakeCollider();
    // SpriteShapeController.BakeMesh().Complete();
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    You basically can't reliably use the renderer bounds for anything.

    If you need particular bounds, track it yourself. Some options:

    - make a script with dimension fields in it, add it to the GameObject
    - make invisible child GameObjects at the bounds and query their position
    - something else?
     
  3. ETGgames

    ETGgames

    Joined:
    Jul 10, 2015
    Posts:
    101
    Hmm that's annoying. What's the point of the GetBounds method on SpriteShapeRenderer if it's just plain wrong? Surely that's a bug.

    Also, that doesn't address the other issue of the sprite shape not showing until enough of it is in camera view.

    I guess one thing I could do is use the geometrycollider that I have generated for my sprite shapes, and get the collider2d.bounds of that instead...
     
  4. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    Could you please let us know the version of SpriteShape you use ? I believe there were issues related to Bounds before and has been fixed in recent versions. Could you please post a repro project and report a bug ? Thanks.
     
  5. ETGgames

    ETGgames

    Joined:
    Jul 10, 2015
    Posts:
    101

    I'm on Sprite shape version 7.0.4 and unity 2021.3.0f1 apple silicon

    I've figure out a workaround for the bound issue by using the bounds of the geometry collider. And then there is the issue of the sprite shape not rendering until more of it is in camera view. I can't seem to make a repro project tho, so my workaround is just quickly making the camera big to render the sprite shape, then setting it back to the desired size.
     
    Last edited: May 13, 2022
  6. ETGgames

    ETGgames

    Joined:
    Jul 10, 2015
    Posts:
    101
    @Venkify I have filed a bug report about the issue of the sprite shape not rendering until more of it is in camera view. It contains a repro project.

    https://unity3d.atlassian.net/servicedesk/customer/portal/2/IN-16430

    Would appreciate someone looks into it as it's very important for my game :)
     
  7. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
  8. ETGgames

    ETGgames

    Joined:
    Jul 10, 2015
    Posts:
    101
    The bug has been reported fixed, but I'm still getting this issue on android il2cpp builds with unity 2023.1.15f1 and sprite shape package 10.0.2
    :(

    This is literally the code I have in order to get around the issue.


    var cam = GetComponentInChildren<Camera>();
    var sizeBefore = cam.orthographicSize;
    cam.orthographicSize = 1000;
    yield return new WaitForEndOfFrame();
    cam.orthographicSize = sizeBefore;


    it has to be called every time the user clicks undo or redo in my level builder game (as well as on init). And it produces a little camera glitch that the user can see, but there ain't much else to be done.
     
    Last edited: Oct 13, 2023