Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Assets/Scripts/IsoSpriteSorting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;


public class IsoSpriteSorting : MonoBehaviour
{
Expand Down Expand Up @@ -55,6 +57,8 @@ public enum SortType
public Vector3 SorterPositionOffset = new Vector3();
public Vector3 SorterPositionOffset2 = new Vector3();
public Renderer[] renderersToSort;
public bool useSortingGroup;
public SortingGroup sortingGroup;

private Transform t;

Expand All @@ -67,6 +71,7 @@ public void SetupStaticCache()

private void RefreshBounds()
{

cachedBounds = new Bounds2D(renderersToSort[0].bounds);
Comment on lines +74 to 75
Copy link

@GameChecker4 GameChecker4 Jun 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to this a UseSortingGroup check, to use sorting groups without selecting SpriteRenderer (like the Particle System or Spine Animation mesh). That fixes the sorting sorting error, but a full fix requires a change in IsoSpriteSorting.CalculateBoundsIntersection() to compare the objects without bounds with other (return true is a quick fix. I think the real calculation of bounds for these objects can be tricky, and not always the right solution)

Suggested change
cachedBounds = new Bounds2D(renderersToSort[0].bounds);
if (!UseSortingGroup)
cachedBounds = new Bounds2D(renderersToSort[0].bounds);

}

Expand Down Expand Up @@ -180,9 +185,17 @@ IEnumerator Start()
private void Setup()
{
t = transform; //This needs to be here AND in the Awake function
if (renderersToSort == null || renderersToSort.Length == 0)
if (useSortingGroup)
{
renderersToSort = GetComponentsInChildren<Renderer>();
sortingGroup = GetComponent<SortingGroup>();
}
else
{
if (renderersToSort == null || renderersToSort.Length == 0)
{
//SortingGroup groupsToSort = GetComponent<SortingGroup>();
renderersToSort = GetComponentsInChildren<Renderer>();
}
}
IsoSpriteSortingManager.RegisterSprite(this);
}
Expand Down
29 changes: 24 additions & 5 deletions Assets/Scripts/IsoSpriteSortingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public static void RegisterSprite(IsoSpriteSorting newSprite)
}
else
{

staticSpriteList.Add(newSprite);
newSprite.SetupStaticCache();
SetupStaticDependencies(newSprite);
Expand Down Expand Up @@ -177,7 +176,15 @@ private static void SetSortOrderBasedOnListOrder(List<IsoSpriteSorting> spriteLi
int count = spriteList.Count;
for (int i = 0; i < count; ++i)
{
spriteList[i].RendererSortingOrder = orderCurrent;
//
if (spriteList[i].useSortingGroup)
{
spriteList[i].sortingGroup.sortingOrder = orderCurrent;
}
else
{
spriteList[i].RendererSortingOrder = orderCurrent;
}
orderCurrent += 1;
}
}
Expand All @@ -187,7 +194,15 @@ private static void SetSortOrderNegative(List<IsoSpriteSorting> spriteList)
int startOrder = -spriteList.Count - 1;
for (int i = 0; i < spriteList.Count; ++i)
{
spriteList[i].RendererSortingOrder = startOrder + i;
if (spriteList[i].useSortingGroup)
{
spriteList[i].sortingGroup.sortingOrder = startOrder + i;
}
else
{
spriteList[i].RendererSortingOrder = startOrder + i;
}

}
}

Expand All @@ -203,6 +218,10 @@ public static void FilterListByVisibility(List<IsoSpriteSorting> fullList, List<
destinationList.Add(sprite);
sprite.forceSort = false;
}
else if(sprite.useSortingGroup)
{
destinationList.Add(sprite);
}
else
{
for (int j = 0; j < sprite.renderersToSort.Length; j++)
Expand All @@ -219,7 +238,7 @@ public static void FilterListByVisibility(List<IsoSpriteSorting> fullList, List<

private static void SortListSimple(List<IsoSpriteSorting> list)
{
list.Sort((a, b) =>
list.Sort((System.Comparison<IsoSpriteSorting>)((a, b) =>
{
if (!a || !b)
{
Expand All @@ -229,6 +248,6 @@ private static void SortListSimple(List<IsoSpriteSorting> list)
{
return IsoSpriteSorting.CompareIsoSorters(a, b);
}
});
}));
}
}