2d-extras icon indicating copy to clipboard operation
2d-extras copied to clipboard

RuleTile implementation does not honor Default GameObject set in editor

Open CodeMonkeyJC opened this issue 10 months ago • 2 comments

In the GetTileData method of the RuleTile script the gameObject is initially set to the default object (line: 206) in the RuleMatches if statement the "defaults" for the gameObject and collider type are always overwritten even if null. Lines 233/234.

Here is a solution i used: if (RuleMatches(rule, position, tilemap, ref transform)) { switch (rule.m_Output) { case TilingRuleOutput.OutputSprite.Single: case TilingRuleOutput.OutputSprite.Animation: tileData.sprite = rule.m_Sprites[0]; break; case TilingRuleOutput.OutputSprite.Random: var index = Mathf.Clamp( Mathf.FloorToInt(GetPerlinValue(position, rule.m_PerlinScale, 100000f) * rule.m_Sprites.Length), 0, rule.m_Sprites.Length - 1); tileData.sprite = rule.m_Sprites[index]; if (rule.m_RandomTransform != TilingRuleOutput.Transform.Fixed) transform = ApplyRandomTransform(rule.m_RandomTransform, transform, rule.m_PerlinScale, position); break; } tileData.transform = transform; tileData.gameObject ??= rule.m_GameObject; if (rule.m_ColliderType != m_DefaultColliderType) tileData.colliderType = rule.m_ColliderType; break; }

CodeMonkeyJC avatar Feb 05 '25 06:02 CodeMonkeyJC

Thanks! We will check this out and update the RuleTile.

ChuanXin-Unity avatar Feb 05 '25 07:02 ChuanXin-Unity

Hi, the current behaviour of the RuleTile is valid and correct.

  • If there is a Rule that matches, it will use the output from the Rule. A user may specifically want a null GameObject or collider type as set up for that Rule, which your changes would override.
  • If there are no Rules that matches, then it would use the defaults as given.

To reduce any errors from having unwanted null GameObjects in a Rule when there are defaults set, in the RuleTileEditor, we will create Rules with the defaults set. This currently is not happening right now, but will be fixed in a future version of the package.

Hope this clarifies the issue!

ChuanXin-Unity avatar Feb 21 '25 01:02 ChuanXin-Unity