- Home /
How do I add neighbor rules for RuleTiles by script?
Not much hope for this but here we go.
So far I managed to successfully create a rule tile by script, add rules to it by script, and set the sprite for each of those rules also by script.
Here's the test code i'm making. It has a default sprite and 3 rules, each with its own sprite.
public static Sprite spriteDefault;
public static Sprite sprite1;
public static Sprite sprite2;
public static Sprite sprite3;
static void CreateRuleTile()
{
RuleTile ruleTile = ScriptableObject.CreateInstance("RuleTile") as RuleTile;
AssetDatabase.CreateAsset(ruleTile, "Assets/" + UnityEngine.Random.Range(0, 999).ToString() + "MyRuleTile.asset");
Debug.Log(AssetDatabase.GetAssetPath(ruleTile));
ruleTile.m_DefaultSprite = spriteDefault;
RuleTile.TilingRule rule01 = new RuleTile.TilingRule();
rule01.m_Sprites[0] = sprite1;
RuleTile.TilingRule rule02 = new RuleTile.TilingRule();
rule02.m_Sprites[0] = sprite2;
RuleTile.TilingRule rule03 = new RuleTile.TilingRule();
rule03.m_Sprites[0] = sprite3;
ruleTile.m_TilingRules.Add(rule01);
ruleTile.m_TilingRules.Add(rule02);
ruleTile.m_TilingRules.Add(rule03);
}
This create a nice little rule tile for me:
Now i'm trying to add the neighbor rules (the 8 little arrows we can set manually on the editor) by script but I have no idea how to do it.
Each rule has a property called m_Neighbors and when I hover over it it says RuleTile.TilingRule.Neighbor[] RuleTile.TilingRule.m_Neighbors
How can I create custom neighbor rules to add to each of my rules?
Didnt notice this question is almost a year old. If you found something on the subject, maybe you could share it?
Oh my goodness. Yet another year later, I just realized you meant that you wanted the solution to setting up neighbor rules. I'm very sorry, I'll share my findings here in case anyone in the future is curious about this.
First you declare or reference your rule tile.
RuleTile rTile = ScriptableObject.CreateInstance("RuleTile") as RuleTile;
Then you create each rule by declaring them.
RuleTile.TilingRule myRuleA = new RuleTile.TilingRule();
myRuleA.m_Sprites[0] = spriteList[33];//What sprite is assigned to this rule?
int[] myRuleArray = new int[8];
myRuleArray[0] = 2;//TopLeft
myRuleArray[1] = 1;//Top
myRuleArray[2] = 2;//TopRight
myRuleArray[3] = 1;//Left
myRuleArray[4] = 1;//Right
myRuleArray[5] = 2;//BottomLeft
myRuleArray[6] = 1;//Bottom
myRuleArray[7] = 1;//BottomRight
The array of rules can be more easily visualized if you put it like this:
2, 1, 2
1, 1,
2, 1, 1,
Where each number represents a position in the rules. The number sets what type of rule it is:
1 = GREEN CHECKMARK
2 = RED X
0 = EMPTY
Then finally, you assign your rule to the rule tile you declared or created:
rTile.m_TilingRules.Add(myRuleA);
$$anonymous$$y thought exactly - use a script rather than spending ages adding individual rules. Did you find a solution?
This! Done by me after I figured it out :)
Answer by metalted · Mar 16, 2020 at 10:06 PM
After some searching in the code i found that neighbors can be set using 2 functions in the TilingRule class. This little snippet will just set all the neighbors to one single type.
Dictionary<Vector3Int, int> dict = rule01.GetNeighbors();
List<Vector3Int> neighbors = rule01.m_NeighborPositions;
for(int i = 0; i < neighbors.Count; i++)
{
dict.Add(neighbors[i], RuleTile.TilingRuleOutput.Neighbor.This);
}
rule01.ApplyNeighbors(dict);
And this is the class that can be found in RuleTile.cs:
[Serializable]
public class TilingRule : TilingRuleOutput
{
/// <summary>
/// The matching Rule conditions for each of its neighboring Tiles.
/// </summary>
public List<int> m_Neighbors = new List<int>();
/// <summary>
/// * Preset this list to RuleTile backward compatible, but not support for HexagonalRuleTile backward compatible.
/// </summary>
public List<Vector3Int> m_NeighborPositions = new List<Vector3Int>()
{
new Vector3Int(-1, 1, 0),
new Vector3Int(0, 1, 0),
new Vector3Int(1, 1, 0),
new Vector3Int(-1, 0, 0),
new Vector3Int(1, 0, 0),
new Vector3Int(-1, -1, 0),
new Vector3Int(0, -1, 0),
new Vector3Int(1, -1, 0),
};
/// <summary>
/// The transform matching Rule for this Rule.
/// </summary>
public Transform m_RuleTransform;
public Dictionary<Vector3Int, int> GetNeighbors()
{
Dictionary<Vector3Int, int> dict = new Dictionary<Vector3Int, int>();
for (int i = 0; i < m_Neighbors.Count && i < m_NeighborPositions.Count; i++)
dict.Add(m_NeighborPositions[i], m_Neighbors[i]);
return dict;
}
public void ApplyNeighbors(Dictionary<Vector3Int, int> dict)
{
m_NeighborPositions = dict.Keys.ToList();
m_Neighbors = dict.Values.ToList();
}
public BoundsInt GetBounds()
{
BoundsInt bounds = new BoundsInt(Vector3Int.zero, Vector3Int.one);
foreach (var neighbor in GetNeighbors())
{
bounds.xMin = Mathf.Min(bounds.xMin, neighbor.Key.x);
bounds.yMin = Mathf.Min(bounds.yMin, neighbor.Key.y);
bounds.xMax = Mathf.Max(bounds.xMax, neighbor.Key.x + 1);
bounds.yMax = Mathf.Max(bounds.yMax, neighbor.Key.y + 1);
}
return bounds;
}
}
Your answer
Follow this Question
Related Questions
Tilemaps making feature 1 Answer
Can you add sprites to existing sprite sheet without breaking the tile pallet and in-game tiles 0 Answers
Spawning a specific Tile in a random position a certain distance from the player character 1 Answer
I can‘t assign a folder to my tile palette,I cannot assign a folder to my tile palette 0 Answers
Unity 2D TileMap Rule Tile Carpet 1 Answer