Skip to content

Commit 6c83962

Browse files
committed
feat: Added PixelsPerUnit value to LDtkComponentLevel. Added new public method to recalculate the level border values in case the level is moved
1 parent c68b54e commit 6c83962

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

Assets/LDtkUnity/Editor/Builders/LDtkBuilderLevel.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,7 @@ private void CreateLevelComponent()
225225

226226
private void PopulateLevelComponent()
227227
{
228-
Vector2 size = ((Vector2)_level.UnityPxSize / _project.PixelsPerUnit);
229-
230-
_levelComponent.OnImport(_level, _lvlFile, _layerComponents, _fieldsComponent, _worldComponent, size, _iidComponent);
228+
_levelComponent.OnImport(_level, _lvlFile, _layerComponents, _fieldsComponent, _worldComponent, _iidComponent, _project.PixelsPerUnit);
231229
}
232230

233231
private bool TryAddFields()

Assets/LDtkUnity/Runtime/Components/LDtkComponentLevel.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using JetBrains.Annotations;
43
using UnityEngine;
54
// ReSharper disable UnusedAutoPropertyAccessor.Global
@@ -30,6 +29,9 @@ public sealed class LDtkComponentLevel : MonoBehaviour
3029
[field: Tooltip("The world-space bounds of this level.\nUseful for getting a level's bounds for a camera, for example.")]
3130
[field: SerializeField] public Bounds BorderBounds { get; private set; }
3231

32+
[field: Tooltip("Project Pixels Per Unit")]
33+
[field: SerializeField] public int PixelsPerUnit { get; private set; }
34+
3335
#endregion
3436

3537
[field: Header("Redundant Fields")]
@@ -118,12 +120,26 @@ private void OnDisable()
118120
{
119121
Lvls.Remove(this);
120122
}
123+
124+
/// <summary>
125+
/// If you moved your level, use this to correct it
126+
/// </summary>
127+
[PublicAPI]
128+
public void RecalculateRects()
129+
{
130+
BorderRect = new Rect(transform.position, Size);
131+
BorderBounds = new Bounds(transform.position + new Vector3(Size.x * 0.5f, Size.y * 0.5f, 0), Size);
132+
}
121133

122134
#endregion
123135

124-
internal void OnImport(Level lvl, LDtkLevelFile file, LDtkComponentLayer[] layers, LDtkFields fields, LDtkComponentWorld world, Vector2 unitySize, LDtkIid iid)
136+
internal void OnImport(Level lvl, LDtkLevelFile file, LDtkComponentLayer[] layers, LDtkFields fields, LDtkComponentWorld world, LDtkIid iid, int ppu)
125137
{
126-
Neighbours = lvl.Neighbours.Select(neighbour => new LDtkNeighbour(neighbour)).ToArray();
138+
Neighbours = new LDtkNeighbour[lvl.Neighbours.Length];
139+
for (int i = 0; i < lvl.Neighbours.Length; i++)
140+
{
141+
Neighbours[i] = new LDtkNeighbour(lvl.Neighbours[i]);
142+
}
127143
LevelBgColor = lvl.UnityLevelBgColor;
128144
//LevelBgPos = lvl.LevelBgPos; //todo
129145
BgRelPath = lvl.BgRelPath;
@@ -145,11 +161,11 @@ internal void OnImport(Level lvl, LDtkLevelFile file, LDtkComponentLayer[] layer
145161
UseAutoIdentifier = lvl.UseAutoIdentifier;
146162

147163
//custom
148-
Size = unitySize;
149-
BorderRect = new Rect(transform.position, unitySize);
150-
BorderBounds = new Bounds(transform.position + new Vector3(unitySize.x * 0.5f, unitySize.y * 0.5f, 0), unitySize);
151164
Json = file;
152165
Parent = world;
166+
PixelsPerUnit = ppu;
167+
Size = PxSize / ppu;
168+
RecalculateRects();
153169
}
154170
}
155171
}

0 commit comments

Comments
 (0)