Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
## [3.1.0] - 2023-01-16
### Added
- [AnimatedTileEditor] -Add field to change TileAnimationFlags (For 2022.2.x)
- [RuleTile] -Add RotatedMirror rule which checks neighbors using both the mirror and rotation Rule in addition to the standard rotation Rule

### Fixed
- [GameObjectBrush] Fix placement of GameObjects for Hexagon Layouts with Anchor
- [GameObjectBrush] Align rotation and flip to 2D View in Editor
- [RandomBrush] Use default color and transform when painting over with RandomBrush

### Changed
- [AnimatedTileEditor] Moved to Unity.2d.Tilemap.Extras.Editor
  • Loading branch information
Unity Technologies committed Jan 16, 2023
1 parent fd0d2f9 commit 80b535d
Show file tree
Hide file tree
Showing 14 changed files with 578 additions and 392 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ All notable changes to this package will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

## [3.1.0] - 2023-01-16
### Added
- [AnimatedTileEditor] -Add field to change TileAnimationFlags (For 2022.2.x)
- [RuleTile] -Add RotatedMirror rule which checks neighbors using both the mirror and rotation Rule in addition to the standard rotation Rule

### Fixed
- [GameObjectBrush] Fix placement of GameObjects for Hexagon Layouts with Anchor
- [GameObjectBrush] Align rotation and flip to 2D View in Editor
- [RandomBrush] Use default color and transform when painting over with RandomBrush

### Changed
- [AnimatedTileEditor] Moved to Unity.2d.Tilemap.Extras.Editor

## [3.0.3] - 2022-11-03
### Fixed
- [GridInformation] Implement IEquatable for GridInformationKey
Expand Down
4 changes: 4 additions & 0 deletions Documentation~/AnimatedTile.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ An Animated Tile runs through and displays a list of Sprites in sequence to crea
| __Start Time__ | The starting time of this Animated Tile. This allows you to start the Animation from a particular time. |
| __Start Frame__ | The starting frame of this Animated Tile. This allows you to start the Animation from a particular Sprite in the list of Animated Sprites. |
| __Collider Type__ | The [Collider](https://docs.unity3d.com/Manual/Collider2D.html) shape generated by the Tile. |
| __Flags__ | The Flags which control the Tile Animation. |
| Loop Once | The Tile Animation will loop through once and stop at the last Sprite of the animation. |
| Pause Animation | The Tile Animation will pause and not run. |
| Update Physics | The Tile Animation will update the Physics Shape in the TilemapCollider2D whenever it switches to the next Sprite in the animation. |

### Usage

Expand Down
Binary file modified Documentation~/images/AnimatedTileEditor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 61 additions & 50 deletions Editor/Brushes/GameObjectBrush/GameObjectBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal class HiddenGridLayout
/// <summary>
/// Anchor Point of the Instantiated GameObject in the cell when painting
/// </summary>
public Vector3 m_Anchor = new Vector3(0.5f, 0.5f, 0.5f);
public Vector3 m_Anchor = new Vector3(0.5f, 0.5f, 0.0f);
/// <summary>Size of the brush in cells. </summary>
public Vector3Int size { get { return m_Size; } set { m_Size = value; SizeUpdated(); } }
/// <summary>Pivot of the brush. </summary>
Expand Down Expand Up @@ -98,6 +98,16 @@ private void OnDisable()
DestroyImmediate(hiddenGrid);
}

private void OnValidate()
{
if (m_Size.x < 0)
m_Size.x = 0;
if (m_Size.y < 0)
m_Size.y = 0;
if (m_Size.z < 0)
m_Size.z = 0;
}

/// <summary>
/// Initializes the content of the GameObjectBrush.
/// </summary>
Expand Down Expand Up @@ -138,7 +148,7 @@ private void PaintCell(GridLayout grid, Vector3Int position, Transform parent, B
if (cell.gameObject == null)
return;

var existingGO = GetObjectInCell(grid, parent, position);
var existingGO = GetObjectInCell(grid, parent, position, m_Anchor);
if (existingGO == null)
{
SetSceneCell(grid, parent, position, cell.gameObject, cell.offset, cell.scale, cell.orientation, m_Anchor);
Expand Down Expand Up @@ -238,13 +248,9 @@ public override void Rotate(RotationDirection direction, GridLayout.CellLayout l
int newPivotY = direction == RotationDirection.Clockwise ? pivot.x : oldSize.x - pivot.x - 1;
pivot = new Vector3Int(newPivotX, newPivotY, pivot.z);

Matrix4x4 rotation = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, direction == RotationDirection.Clockwise ? 90f : -90f), Vector3.one);
Quaternion orientation = Quaternion.Euler(0f, 0f, direction == RotationDirection.Clockwise ? 90f : -90f);
Quaternion orientation = Quaternion.Euler(0f, 0f, direction != RotationDirection.Clockwise ? 90f : -90f);
foreach (BrushCell cell in m_Cells)
{
cell.offset = rotation * cell.offset;
cell.orientation = cell.orientation * orientation;
}
}

/// <summary>Flips the brush in the given axis.</summary>
Expand Down Expand Up @@ -276,14 +282,19 @@ public override void Pick(GridLayout gridLayout, GameObject brushTarget, BoundsI
foreach (Vector3Int pos in position.allPositionsWithin)
{
Vector3Int brushPosition = new Vector3Int(pos.x - position.x, pos.y - position.y, 0);
PickCell(pos, brushPosition, gridLayout, brushTarget != null ? brushTarget.transform : null);
PickCell(pos, brushPosition, gridLayout, brushTarget != null ? brushTarget.transform : null, true);
}
}

private void PickCell(Vector3Int position, Vector3Int brushPosition, GridLayout grid, Transform parent)
private void PickCell(Vector3Int position, Vector3Int brushPosition, GridLayout grid, Transform parent, bool withoutAnchor = false)
{
Vector3 cellCenter = grid.LocalToWorld(grid.CellToLocalInterpolated(position + m_Anchor));
GameObject go = GetObjectInCell(grid, parent, position);
var go = GetObjectInCell(grid, parent, position, m_Anchor);
if (go == null)
{
go = GetObjectInCell(grid, parent, position, Vector3.zero);
}
var anchorRatio = GetAnchorRatio(grid, m_Anchor);
var cellCenter = grid.LocalToWorld(grid.CellToLocalInterpolated(position) + grid.CellToLocalInterpolated(anchorRatio));

if (go != null)
{
Expand Down Expand Up @@ -376,23 +387,16 @@ private void FlipX()

foreach (Vector3Int oldPos in oldBounds.allPositionsWithin)
{
int newX = m_Size.x - oldPos.x - 1;
int toIndex = GetCellIndex(newX, oldPos.y, oldPos.z);
int fromIndex = GetCellIndex(oldPos);
var newX = m_Size.x - oldPos.x - 1;
var toIndex = GetCellIndex(newX, oldPos.y, oldPos.z);
var fromIndex = GetCellIndex(oldPos);
m_Cells[toIndex] = oldCells[fromIndex];
}

int newPivotX = m_Size.x - pivot.x - 1;
var newPivotX = m_Size.x - pivot.x - 1;
pivot = new Vector3Int(newPivotX, pivot.y, pivot.z);
Matrix4x4 flip = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(-1f, 1f, 1f));
Quaternion orientation = Quaternion.Euler(0f, 0f, -180f);

foreach (BrushCell cell in m_Cells)
{
Vector3 oldOffset = cell.offset;
cell.offset = flip * oldOffset;
cell.orientation = cell.orientation*orientation;
}
FlipCells(ref m_Cells, new Vector3(-1f, 1f, 1f));
}

private void FlipY()
Expand All @@ -402,21 +406,23 @@ private void FlipY()

foreach (Vector3Int oldPos in oldBounds.allPositionsWithin)
{
int newY = m_Size.y - oldPos.y - 1;
int toIndex = GetCellIndex(oldPos.x, newY, oldPos.z);
int fromIndex = GetCellIndex(oldPos);
var newY = m_Size.y - oldPos.y - 1;
var toIndex = GetCellIndex(oldPos.x, newY, oldPos.z);
var fromIndex = GetCellIndex(oldPos);
m_Cells[toIndex] = oldCells[fromIndex];
}

int newPivotY = m_Size.y - pivot.y - 1;
var newPivotY = m_Size.y - pivot.y - 1;
pivot = new Vector3Int(pivot.x, newPivotY, pivot.z);
Matrix4x4 flip = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(1f, -1f, 1f));
Quaternion orientation = Quaternion.Euler(0f, 0f, -180f);
foreach (BrushCell cell in m_Cells)

FlipCells(ref m_Cells, new Vector3(1f, -1f, 1f));
}

private static void FlipCells(ref BrushCell[] cells, Vector3 scale)
{
foreach (BrushCell cell in cells)
{
Vector3 oldOffset = cell.offset;
cell.offset = flip * oldOffset;
cell.orientation = cell.orientation * orientation;
cell.scale = Vector3.Scale(cell.scale, scale);
}
}

Expand Down Expand Up @@ -515,7 +521,7 @@ public int GetCellIndexWrapAround(int x, int y, int z)
return (x % m_Size.x) + m_Size.x * (y % m_Size.y) + m_Size.x * m_Size.y * (z % m_Size.z);
}

private GameObject GetObjectInCell(GridLayout grid, Transform parent, Vector3Int position)
private GameObject GetObjectInCell(GridLayout grid, Transform parent, Vector3Int position, Vector3 anchor)
{
int childCount;
GameObject[] sceneChildren = null;
Expand All @@ -529,16 +535,14 @@ private GameObject GetObjectInCell(GridLayout grid, Transform parent, Vector3Int
{
childCount = parent.childCount;
}
var anchorCellOffset = Vector3Int.FloorToInt(m_Anchor);
var cellSize = grid.cellSize;
anchorCellOffset.x = cellSize.x == 0 ? 0 : anchorCellOffset.x;
anchorCellOffset.y = cellSize.y == 0 ? 0 : anchorCellOffset.y;
anchorCellOffset.z = cellSize.z == 0 ? 0 : anchorCellOffset.z;

var anchorRatio = GetAnchorRatio(grid, anchor);
var anchorWorld = grid.CellToLocalInterpolated(anchorRatio);
for (var i = 0; i < childCount; i++)
{
var child = sceneChildren == null ? parent.GetChild(i) : sceneChildren[i].transform;
if (position == grid.WorldToCell(child.position) - anchorCellOffset)
var childCell = grid.LocalToCell(grid.WorldToLocal(child.position) - anchorWorld);
if (position == childCell)
return child.gameObject;
}
return null;
Expand All @@ -557,8 +561,9 @@ private bool ValidateCellPosition(Vector3Int position)

internal void SizeUpdated(bool keepContents = false)
{
OnValidate();
Array.Resize(ref m_Cells, sizeCount);
BoundsInt bounds = new BoundsInt(Vector3Int.zero, m_Size);
var bounds = new BoundsInt(Vector3Int.zero, m_Size);
foreach (Vector3Int pos in bounds.allPositionsWithin)
{
if (keepContents || m_Cells[GetCellIndex(pos)] == null)
Expand Down Expand Up @@ -589,26 +594,32 @@ private static void SetSceneCell(GridLayout grid, Transform parent, Vector3Int p
}

Undo.RegisterCreatedObjectUndo(instance, "Paint GameObject");
var anchorRatio = GetAnchorRatio(grid, anchor);

instance.transform.position = grid.LocalToWorld(grid.CellToLocalInterpolated(position) + grid.CellToLocalInterpolated(anchorRatio));
instance.transform.localRotation = orientation;
instance.transform.localScale = scale;
instance.transform.Translate(offset);
}

private static Vector3 GetAnchorRatio(GridLayout grid, Vector3 cellAnchor)
{
var cellSize = grid.cellSize;
var cellStride = cellSize + grid.cellGap;
cellStride.x = Mathf.Approximately(0f, cellStride.x) ? 1f : cellStride.x;
cellStride.y = Mathf.Approximately(0f, cellStride.y) ? 1f : cellStride.y;
cellStride.z = Mathf.Approximately(0f, cellStride.z) ? 1f : cellStride.z;
var anchorRatio = new Vector3(
anchor.x * cellSize.x / cellStride.x,
anchor.y * cellSize.y / cellStride.y,
anchor.z * cellSize.z / cellStride.z
cellAnchor.x * cellSize.x / cellStride.x,
cellAnchor.y * cellSize.y / cellStride.y,
cellAnchor.z * cellSize.z / cellStride.z
);
instance.transform.position = grid.LocalToWorld(grid.CellToLocalInterpolated(position + anchorRatio));
instance.transform.localRotation = orientation;
instance.transform.localScale = scale;
instance.transform.Translate(offset);
return anchorRatio;
}

private void ClearSceneCell(GridLayout grid, Transform parent, Vector3Int position)
{
GameObject erased = GetObjectInCell(grid, parent, new Vector3Int(position.x, position.y, position.z));
var erased = GetObjectInCell(grid, parent, position, m_Anchor);
if (erased != null)
Undo.DestroyObjectImmediate(erased);
}
Expand Down
Loading

0 comments on commit 80b535d

Please sign in to comment.