11using System . Collections . Generic ;
2- using System . Linq ;
32using JetBrains . Annotations ;
43using 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.\n Useful 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