Skip to content

Commit 6e96315

Browse files
committed
feat: Added layer sorting option
1 parent a8042c8 commit 6e96315

File tree

7 files changed

+94
-10
lines changed

7 files changed

+94
-10
lines changed

Assets/LDtkUnity/Editor/Builders/LDtkBuilderEntity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public LDtkBuilderEntity(LDtkProjectImporter project, Level level, LDtkComponent
2424

2525
public LDtkComponentEntity[] BuildEntityLayerInstances()
2626
{
27-
SortingOrder.Next();
27+
SortingOrder.Next(Layer.Identifier);
2828

2929
LDtkFieldParser.CacheRecentBuilder(this);
3030

Assets/LDtkUnity/Editor/Builders/LDtkBuilderIntGridValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public LDtkBuilderIntGridValue(LDtkProjectImporter importer, Level level, LDtkCo
1717
public void BuildIntGridValues()
1818
{
1919
RoundTilemapPos();
20-
SortingOrder.Next();
20+
SortingOrder.Next(Layer.Identifier);
2121

2222
LayerDefinition layerDef = Layer.Definition;
2323
IntGridValueDefinition[] intGridValueDefs = layerDef.IntGridValues;

Assets/LDtkUnity/Editor/Builders/LDtkBuilderLevel.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using UnityEngine;
1+
using System.Linq;
2+
using UnityEngine;
23

34
namespace LDtkUnity.Editor
45
{
@@ -102,8 +103,9 @@ private void BuildLevelProcess()
102103
AddIidComponent();
103104
LDtkProfiler.EndSample();
104105

105-
LDtkProfiler.BeginSample("new LDtkSortingOrder");
106-
_sortingOrder = new LDtkSortingOrder();
106+
LDtkProfiler.BeginSample("LDtkSortingOrder");
107+
var sortingOrders = _project.LayerSortingOrders.ToDictionary(x => x.Layer, x => x.Order);
108+
_sortingOrder = new LDtkSortingOrder(sortingOrders);
107109
LDtkProfiler.EndSample();
108110

109111
LDtkProfiler.BeginSample("BuildLayerInstances");

Assets/LDtkUnity/Editor/Builders/LDtkBuilderTileset.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public LDtkBuilderTileset(LDtkProjectImporter project, Level level, LDtkComponen
2121

2222
private void ConstructNewTilemap()
2323
{
24-
SortingOrder.Next();
24+
SortingOrder.Next(Layer.Identifier);
2525

2626
string tilemapName = Layer.IsTilesLayer ? "Tiles" : "AutoLayer";
2727

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1-
namespace LDtkUnity.Editor
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
4+
namespace LDtkUnity.Editor
25
{
36
internal sealed class LDtkSortingOrder
47
{
58
public int SortingOrderValue { get; private set; } = 0;
9+
private readonly Dictionary<string, int> _overrides;
10+
private int _autoValue = 0;
11+
12+
public LDtkSortingOrder(Dictionary<string, int> overrides = null)
13+
{
14+
_overrides = overrides;
15+
}
616

7-
public void Next()
17+
public void Next(string layerIdentifier = null)
818
{
9-
SortingOrderValue--;
19+
if (layerIdentifier != null && _overrides != null && _overrides.TryGetValue(layerIdentifier, out int order))
20+
{
21+
SortingOrderValue = order;
22+
Debug.Log(order);
23+
return;
24+
}
25+
26+
SortingOrderValue = --_autoValue;
1027
}
1128
}
1229
}

Assets/LDtkUnity/Editor/CustomEditor/SectionDrawers/LDtkSectionMain.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ internal sealed class LDtkSectionMain : LDtkSectionDrawer
5757
text = "Scale Entities",
5858
tooltip = "Apply a scale factor to entity prefab instances if they were resized in LDtk."
5959
};
60+
private static readonly GUIContent CustomSortingOrders = new GUIContent
61+
{
62+
text = "Custom Sorting Orders",
63+
tooltip = "Enable this to customize the sorting order for specific layers. If disabled, layers will automatically decrement starting from 0."
64+
};
6065

6166
protected override string GuiText => "Main";
6267
protected override string GuiTooltip => "This is the importer menu.\n" +
@@ -107,10 +112,56 @@ protected override void DrawDropdownContent()
107112
DrawField(CreateLevelBoundsTrigger, LDtkProjectImporter.CREATE_LEVEL_BOUNDS_TRIGGER);
108113
DrawField(UseParallax, LDtkProjectImporter.USE_PARALLAX);
109114
DrawField(ScaleEntities, LDtkProjectImporter.SCALE_ENTITIES);
115+
116+
DrawLayerSortingOrders();
110117

111118
Editor.DrawDependenciesProperty();
112119
}
113120

121+
private void DrawLayerSortingOrders()
122+
{
123+
SerializedProperty customProp = DrawField(CustomSortingOrders, LDtkProjectImporter.CUSTOM_SORTING_ORDERS);
124+
125+
if (!customProp.boolValue)
126+
{
127+
return;
128+
}
129+
130+
SerializedProperty sortingOrderArray = SerializedObject.FindProperty(LDtkProjectImporter.LAYER_SORTING_ORDERS);
131+
132+
// Sync array with data
133+
LayerDefinition[] layers = _data.Defs.Layers;
134+
if (sortingOrderArray.arraySize != layers.Length)
135+
{
136+
sortingOrderArray.arraySize = layers.Length;
137+
}
138+
139+
for (int i = 0; i < layers.Length; i++)
140+
{
141+
SerializedProperty element = sortingOrderArray.GetArrayElementAtIndex(i);
142+
SerializedProperty layerNameProp = element.FindPropertyRelative("Layer");
143+
SerializedProperty orderProp = element.FindPropertyRelative("Order");
144+
145+
string layerName = layers[i].Identifier;
146+
147+
// If name is different or empty (newly created), set it and default order
148+
if (layerNameProp.stringValue != layerName)
149+
{
150+
layerNameProp.stringValue = layerName;
151+
// Default auto generated value: -1, -2, etc. (starts at 0 and decrements)
152+
orderProp.intValue = -(i + 1);
153+
}
154+
155+
EditorGUILayout.BeginHorizontal();
156+
using (new EditorGUI.DisabledScope(true))
157+
{
158+
EditorGUILayout.TextField(layerNameProp.stringValue);
159+
}
160+
EditorGUILayout.PropertyField(orderProp, GUIContent.none);
161+
EditorGUILayout.EndHorizontal();
162+
}
163+
}
164+
114165
private void PixelsPerUnitField()
115166
{
116167
GUIContent content = new GUIContent(PixelsPerUnit)

Assets/LDtkUnity/Editor/ScriptedImporter/LDtkProjectImporter.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ internal sealed class LDtkProjectImporter : LDtkJsonImporter<LDtkProjectFile>
3838
public const string ENUM_GENERATE = nameof(_enumGenerate);
3939
public const string ENUM_PATH = nameof(_enumPath);
4040
public const string ENUM_NAMESPACE = nameof(_enumNamespace);
41+
public const string CUSTOM_SORTING_ORDERS = nameof(_customSortingOrders);
42+
public const string LAYER_SORTING_ORDERS = nameof(_layerSortingOrders);
4143

4244

4345
/// <summary>
@@ -63,6 +65,16 @@ internal sealed class LDtkProjectImporter : LDtkJsonImporter<LDtkProjectFile>
6365
[SerializeField] private string _enumPath = null;
6466
[SerializeField] private string _enumNamespace = string.Empty;
6567

68+
[SerializeField] private bool _customSortingOrders = false;
69+
70+
[Serializable]
71+
public struct LayerSortingOrder
72+
{
73+
public string Layer;
74+
public int Order;
75+
}
76+
[SerializeField] private LayerSortingOrder[] _layerSortingOrders = Array.Empty<LayerSortingOrder>();
77+
6678

6779
public LDtkProjectFile JsonFile => _jsonFile;
6880
public bool IntGridValueColorsVisible => _intGridValueColorsVisible;
@@ -74,6 +86,8 @@ internal sealed class LDtkProjectImporter : LDtkJsonImporter<LDtkProjectFile>
7486
public bool CreateLevelBoundsTrigger => _createLevelBoundsTrigger;
7587
public bool UseParallax => _useParallax;
7688
public bool ScaleEntities => _scaleEntities;
89+
public bool CustomSortingOrders => _customSortingOrders;
90+
public LayerSortingOrder[] LayerSortingOrders => _customSortingOrders ? _layerSortingOrders : Array.Empty<LayerSortingOrder>();
7791

7892
//all of these are wiped after the entire import is done
7993
private LDtkArtifactAssets _artifacts;
@@ -424,4 +438,4 @@ public Sprite GetBackgroundArtifact(Level level)
424438
return asset;
425439
}
426440
}
427-
}
441+
}

0 commit comments

Comments
 (0)