Search Unity

Creating/Editing RuleTiles By Code

Discussion in '2D' started by blindgoat, Jul 27, 2018.

  1. blindgoat

    blindgoat

    Joined:
    Oct 24, 2012
    Posts:
    31
    I have a RuleTile that works great when using the SceneView to edit the Tilemap. All my rules work as expected.

    I have in-game Tilemap editing working with normal Tiles but I'm stumped on how to make it work with RuleTiles.

    I need the user to be able to adjust place a RuleTile wherever they click. I have a Tilemap that ONLY uses a single RuleTile. No standard Tile are used on this Tilemap.

    My current attempt is:
    Code (CSharp):
    1.  
    2. private Tilemap _tilemap; // assigned in CTOR
    3. private RuleTile _ruleTile; // assigned in CTOR
    4. [SerializeField] private RuleTile _ruleTileSO; // The RuleTile ScriptableObject is dragged here
    5.  
    6. private void DoStuff()
    7. {
    8.     _ruleTile = (RuleTile)_tilemap.GetTile(_pos);
    9.     if(_ruleTile == null)
    10.     {
    11.         _ruleTile = (RuleTile)ScriptableObject.Instantiate(_ruleTileSO);
    12.         _tilemap.SetTile(_pos, _ruleTile);
    13.         //_ruleTile.RefreshTile(_pos, _tilemap); // Doesn't compile
    14.     }
    15. }
    16.  
    The problem is
    because RefreshTile() expects an ITilemap and for some reason Tilemap doesn't implement ITilemap. Without refreshing the tile, it draws the RuleTile's default Sprite in every cell and never updates itself or neighbors based on the rules.

    I'm sure this is simple and I'm missing something but I'm not seeing it. I'm also confused why Tilemap wouldn't implement ITilemap.

    Any ideas? Thanks in advance!
     
  2. blindgoat

    blindgoat

    Joined:
    Oct 24, 2012
    Posts:
    31
    I'm still stumped and am unable to figure out how to refresh a RuleTile by code due to the fact that RefreshTile requires an ITilemap.
     
  3. edwardrowe

    edwardrowe

    Joined:
    Feb 11, 2014
    Posts:
    52
    We use the RefreshAllTiles function on Tilemap to do this after all manipulations of tiles are complete. It may be slow for your use case, though.
     
    blindgoat likes this.
  4. blindgoat

    blindgoat

    Joined:
    Oct 24, 2012
    Posts:
    31
    Thanks @edwardrowe. I'm not sure how I missed that :) It works for now! I'll let you know if I ever figure out how to only call refresh on the tile that was updated.