Skip to content

Commit c68b54e

Browse files
committed
refactor: LDtkComponentLevel now precalculates it's BorderRect and BorderBounds during the import process and serializes them into the component
1 parent 43ec000 commit c68b54e

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

Assets/LDtkUnity/Runtime/Components/LDtkComponentLevel.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public sealed class LDtkComponentLevel : MonoBehaviour
2424
[field: Tooltip("The size of this level in Unity units.")]
2525
[field: SerializeField] public Vector2 Size { get; private set; }
2626

27+
[field: Tooltip("The world-space rectangle of this level.\nUseful for getting a level's bounds for a camera, for example.")]
28+
[field: SerializeField] public Rect BorderRect { get; private set; }
29+
30+
[field: Tooltip("The world-space bounds of this level.\nUseful for getting a level's bounds for a camera, for example.")]
31+
[field: SerializeField] public Bounds BorderBounds { get; private set; }
32+
2733
#endregion
2834

2935
[field: Header("Redundant Fields")]
@@ -93,7 +99,7 @@ public sealed class LDtkComponentLevel : MonoBehaviour
9399

94100
/// <summary>
95101
/// A static collection of all active level GameObjects in the scene during runtime.<br/>
96-
/// This list will actively update as level GameObjects are set active/inactive.
102+
/// This list will actively update as level GameObjects are set active/inactive. So only query this during Start and not Awake.
97103
/// </summary>
98104
[PublicAPI] public static IReadOnlyCollection<LDtkComponentLevel> Levels => Lvls;
99105

@@ -140,20 +146,10 @@ internal void OnImport(Level lvl, LDtkLevelFile file, LDtkComponentLayer[] layer
140146

141147
//custom
142148
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);
143151
Json = file;
144152
Parent = world;
145153
}
146-
147-
/// <value>
148-
/// The world-space rectangle of this level. <br/>
149-
/// Useful for getting a level's bounds for a camera, for example.
150-
/// </value>
151-
[PublicAPI] public Rect BorderRect => new Rect(transform.position, Size);
152-
153-
/// <value>
154-
/// The world-space bounds of this level. <br/>
155-
/// Useful for getting a level's bounds for a camera, for example.
156-
/// </value>
157-
[PublicAPI] public Bounds BorderBounds => new Bounds(transform.position + (Vector3)(Vector2.one * Size * 0.5f), Size);
158154
}
159155
}

0 commit comments

Comments
 (0)