Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
-Add ApplyGameObjectTransform to RuleTile to handle original GameObje…
…ct's transform and Tilemap orientation issues
  • Loading branch information
ChuanXin-Unity committed Jun 1, 2020
1 parent 18cae29 commit f5f7a8f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions Editor/Tiles/RuleTile/RuleTileEditor.cs
Expand Up @@ -249,6 +249,7 @@ public override void OnInspectorGUI()
tile.m_DefaultSprite = EditorGUILayout.ObjectField("Default Sprite", tile.m_DefaultSprite, typeof(Sprite), false) as Sprite;
tile.m_DefaultGameObject = EditorGUILayout.ObjectField("Default Game Object", tile.m_DefaultGameObject, typeof(GameObject), false) as GameObject;
tile.m_DefaultColliderType = (Tile.ColliderType)EditorGUILayout.EnumPopup("Default Collider", tile.m_DefaultColliderType);
tile.m_ApplyGameObjectTransform = EditorGUILayout.Toggle("Apply GameObject Transform", tile.m_ApplyGameObjectTransform);

DrawCustomFields(false);

Expand Down
34 changes: 32 additions & 2 deletions Runtime/Tiles/RuleTile/RuleTile.cs
Expand Up @@ -43,6 +43,11 @@ public class RuleTile : TileBase
/// The Default Collider Type set when creating a new Rule.
/// </summary>
public Tile.ColliderType m_DefaultColliderType = Tile.ColliderType.Sprite;
/// <summary>
/// When instantiating GameObjects from any Rules, apply the original GameObject's Transform onto
/// the Rule's Transform to get the final Transform for the instantiated GameObject.
/// </summary>
public bool m_ApplyGameObjectTransform = false;

public virtual int m_RotationAngle => 90;
public int m_RotationCount => 360 / m_RotationAngle;
Expand Down Expand Up @@ -293,6 +298,31 @@ public override bool StartUp(Vector3Int location, ITilemap tilemap, GameObject i
if (RuleMatches(rule, location, tilemap, ref transform))
{
transform = orientMatrix * transform;
if (m_ApplyGameObjectTransform)
{
transform *= rule.m_GameObject.transform.localToWorldMatrix;
if (tmpMap.orientation == Tilemap.Orientation.YX
|| tmpMap.orientation == Tilemap.Orientation.ZX
|| tmpMap.orientation == Tilemap.Orientation.ZY)
{
var inverseRotation = Matrix4x4.identity;
inverseRotation.m00 = -inverseRotation.m00;
inverseRotation.m11 = -inverseRotation.m11;
transform *= inverseRotation;
if (tmpMap.orientation == Tilemap.Orientation.YX)
{
transform.m23 *= -1;
}
else if (tmpMap.orientation == Tilemap.Orientation.ZX)
{
transform.m13 *= -1;
}
else
{
transform.m03 *= -1;
}
}
}

// Converts the tile's translation, rotation, & scale matrix to values to be used by the instantiated Game Object
gameObjectTranslation = new Vector3(transform.m03, transform.m13, transform.m23);
Expand Down Expand Up @@ -702,9 +732,9 @@ public virtual Vector3Int GetMirroredPosition(Vector3Int position, bool mirrorX,
return position;
}

public virtual Vector3Int GetOffsetPosition(Vector3Int location, Vector3Int offset)
public virtual Vector3Int GetOffsetPosition(Vector3Int position, Vector3Int offset)
{
return location + offset;
return position + offset;
}

public virtual Vector3Int GetOffsetPositionReverse(Vector3Int position, Vector3Int offset)
Expand Down

0 comments on commit f5f7a8f

Please sign in to comment.