Search Unity

Feedback Overriding TileBase.StartUp is painful when the tilemap is not active

Discussion in '2D' started by Ladace, Dec 7, 2021.

  1. Ladace

    Ladace

    Joined:
    Dec 7, 2014
    Posts:
    40
    Hi, I'm making a PCG, which generates tilemaps before game starts.

    I wouldn't let the game logic run during the generation, so I set the level inactive, do some async generation, and set it active to initiate the game. But found the behavior of Tilemap is ill-designed imo:

    1. If `Tilemap` component is added to an inactive object, it won't initialize. When you access `layoutGrid`, it remains null.
    2. If you call `Tilemap::SetTile()`, it will immediately call `TileBase::StartUp()`, passing the uninitialized tilemap as an argument.
    3. The issue is that when I wanted to initialize the instanced gameobject in `StartUp()`, `Tilemap::GetCellCenterLocal()` always returns to Vector3.zero, because `layoutGrid` is not initialized!

    It's quite unintuitive to use. I'm trying to use some workarounds like caching the tiles that need to be initialized and initialize them after the tilemap is active, or maybe refresh all the tiles once the tilemap is activated. But this behavior is flawed (like many other parts in Unity) and I hope it get improved in the future.
     
  2. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    249
    Isn't it expected behavior that a component added to an inactive gameobject doesn't get OnEnable etc? None of what you describe seems incorrect. For myself, I wouldn't expect components on an inactive GO to work properly until the GO is set active and the components initialized. If you don't want the tiles to be visible why not just shut off the tilemap renderer? Perhaps I don't understand what you're trying to do...
     
  3. Ladace

    Ladace

    Joined:
    Dec 7, 2014
    Posts:
    40
    There is no TilemapRenderer. I'm purely using Tilemap as a container which spawns instanced GOs, which doesn't require TilemapRenderer.

    I ended up using a cache to temporarily store all the references to the GOs instanced from `SetTile()` if the Tilemap is inactive, and initialize them the moment the Tilemap gets activated.

    Maybe I need to post the code to demonstrate it clearer.
     
  4. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Sorry about this issue, we will check this out! If there are other behaviours that you think are wrong in Unity, do let us know!