Unity-Technologies / 2d-extras

Fun 2D Stuff that we'd like to share!
Other
1.55k stars 343 forks source link

When does "RuleTile" spawns its gameObject? #155

Open positivedivergent opened 4 years ago

positivedivergent commented 4 years ago

Does someone knows when in scripts "RuleTile" spawns its gameObject, i could not find, I need to rewrite this part of the code. Help pleeease.

ChuanXin-Unity commented 4 years ago

The GameObject is spawned as part of the Tilemap component Update. This occurs when the RuleTile is added to the Tilemap, or when the Tilemap is instantiated in the Scene with such Tiles.

You can add a StartUp override to the RuleTile which will trigger when the GameObject is instantiated:

public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go)
{
    // Shift instantiated GameObject position
    if (go != null)
       go.transform.position = go.transform.position + Vector3.right;
}
positivedivergent commented 4 years ago

The GameObject is spawned as part of the Tilemap component Update. This occurs when the RuleTile is added to the Tilemap, or when the Tilemap is instantiated in the Scene with such Tiles.

You can add a StartUp override to the RuleTile which will trigger when the GameObject is instantiated:

public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go)
{
    // Shift instantiated GameObject position
    if (go != null)
       go.transform.position = go.transform.position + Vector3.right;
}

My problem is different, but your explanation was very clarifing. I was reffering to another "gameObject" (not the tile itself). The "gameObject" you can "put or refer to" inside of "one of the rules" that exists (if you create them) in a rule tile scriptable object. When, that (very important gameObject) is spawning on code? Thank you for your fast reply.

ChuanXin-Unity commented 4 years ago

Assuming that you are setting a RuleTile on the Tilemap, the flow would be:

If the RuleTiles have already been sent on the Tilemap, for example when a scene with a Tilemap is loaded, the instantiation of the GameObjects together with RuleTile.StartUp() will be called on the first Tilemap.Update().

MrMelog commented 10 months ago

Assuming that you are setting a RuleTile on the Tilemap, the flow would be:

  • Tilemap.SetTile() is called
  • Tilemap will call RuleTile.RefreshTile()
  • RuleTile will trigger the refresh for each neighbor tile with tilemap.RefreshTile()
  • Tilemap will trigger RuleTile.GetTileData()
  • RuleTile will check the matching rule and fill in the TileData
  • This includes the GameObject that the Tilemap will spawn for the Tile (TileData.gameObject)
  • When TileData is returned, the Tilemap component will internally handle the TileData
  • This includes instantiating of TileData.gameObject (This is internal!)
  • After instantiation, RuleTile.StartUp() will be called. Changes to the instantiated GameObject should be done here
  • Tile.SetTile() ends

If the RuleTiles have already been sent on the Tilemap, for example when a scene with a Tilemap is loaded, the instantiation of the GameObjects together with RuleTile.StartUp() will be called on the first Tilemap.Update().

That's insane thank you very much for this very important info! Thank you very much!