Search Unity

2D Rule tiles problem

Discussion in '2D' started by nonolo012, Oct 23, 2019.

  1. nonolo012

    nonolo012

    Joined:
    Oct 23, 2019
    Posts:
    3
    Is there anyway i can make this rule tiles to interact with another rule tiles ?
    I want to make the ground have dirt and snow in order so i can't use random and dont know how to make two rule tiles that have different sprite can interact with each other.

    giphy.gif

    Many thanks.
     
  2. diliupg

    diliupg

    Joined:
    Jan 23, 2018
    Posts:
    45
    I don't think you can do that. But why not incorporate both tiles in the same rue tile? The logic will be a bit complicated but you can get it right eventually.
     
  3. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    You could make use of Custom Rule Tiles to code up a new rule for matching with Tiles of the same type but with different characteristics.

    Here is an example:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Tilemaps;
    4.  
    5. [CreateAssetMenu]
    6. public class ExampleCustomRuleTile : RuleTile<ExampleCustomRuleTile.Neighbor>
    7. {
    8.     public bool snow;
    9.  
    10.     public class Neighbor : RuleTile.TilingRule.Neighbor
    11.     {
    12.         public const int SameTerrain = 3;
    13.         public const int DifferentTerrain = 4;
    14.     }
    15.  
    16.     public override bool RuleMatch(int neighbor, TileBase tile)
    17.     {
    18.         var customRule = tile as ExampleCustomRuleTile;
    19.         switch (neighbor)
    20.         {
    21.             case Neighbor.SameTerrain:
    22.                 return customRule && this.snow == customRule.snow;
    23.             case Neighbor.DifferentTerrain:
    24.                 return customRule && this.snow != customRule.snow;
    25.         }
    26.         return base.RuleMatch(neighbor, tile);
    27.     }
    28. }
    29.  
    You can use the new rules to match transitions between the snow and the dirt tiles?
     
    miracalious and nonolo012 like this.
  4. nonolo012

    nonolo012

    Joined:
    Oct 23, 2019
    Posts:
    3
    Idk how it works...
     
  5. nonolo012

    nonolo012

    Joined:
    Oct 23, 2019
    Posts:
    3
    Thanks but how can i place snow instead of dirt and dirt instead of snow ? (i'm still new so... XD)
     
  6. miracalious

    miracalious

    Joined:
    Feb 15, 2015
    Posts:
    2
    I am trying to come up with a custom Hexagonal rule tile that can factor in various surrounding tile types and am trying to override the HexagonalRuleTile class using the above methodology, but can't seem to bring up the regular hex fields in the inspector. I would very much like to see more documentation and examples on this subject.
     
  7. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    The concept should be the same, except that the new class should derive from the HexagonalRuleTile instead. The Editor for this new class should also derive from the HexgonalRuleTileEditor as well.

    Code (CSharp):
    1. using System;
    2. using UnityEditor;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5.  
    6. [CreateAssetMenu]
    7. public class ExampleCustomHexagonalRuleTile : HexagonalRuleTile<ExampleCustomHexagonalRuleTile.Neighbor>
    8. {
    9.     public bool desert;
    10.  
    11.     public class Neighbor : RuleTile.TilingRule.Neighbor
    12.     {
    13.         public const int SameTerrain = 3;
    14.         public const int DifferentTerrain = 4;
    15.     }
    16.  
    17.     public override bool RuleMatch(int neighbor, TileBase tile)
    18.     {
    19.         var customRule = tile as ExampleCustomHexagonalRuleTile;
    20.         switch (neighbor)
    21.         {
    22.             case Neighbor.SameTerrain:
    23.                 return customRule != null && this.desert == customRule.desert;
    24.             case Neighbor.DifferentTerrain:
    25.                 return customRule != null && this.desert != customRule.desert;
    26.         }
    27.         return base.RuleMatch(neighbor, tile);
    28.     }
    29. }
    30.  
    31. [CustomEditor(typeof(ExampleCustomHexagonalRuleTile))]
    32. public class ExampleCustomHexagonalRuleTileEditor : HexagonalRuleTileEditor
    33. {
    34. }