Skip to content

Commit

Permalink
sample: Made a better example for ladders in GridVania
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammin committed Oct 20, 2024
1 parent 008c7a6 commit c827c0d
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 6 deletions.
48 changes: 48 additions & 0 deletions Assets/LDtkUnity/Samples~/Common/Scripts/ExampleImportedLadder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using LDtkUnity;
using UnityEngine;

namespace Samples
{
/// <summary>
/// This example demonstrates how resizing an LDtk entity can dynamically adjust the size of an object.
/// </summary>
public class ExampleImportedLadder : MonoBehaviour, ILDtkImportedEntity
{
[SerializeField] private SpriteRenderer _renderer = null;
[SerializeField] private BoxCollider2D _collider = null;

public void OnLDtkImportEntity(EntityInstance entityInstance)
{
if (_renderer == null)
{
Debug.LogWarning($"LDtk Sample: An entity's referenced renderer component was null. This can happen when importing the examples for the first time. Try reimporting again to fix the samples.\n{name}", gameObject);
return;
}
if (_collider == null)
{
Debug.LogWarning($"LDtk Sample: An entity's referenced collider component was null. This can happen when importing the examples for the first time. Try reimporting again to fix the samples.\n{name}", gameObject);
return;
}

//The importer will normally scale the GameObject based on how much it was resized in LDtk.
//But we don't want that in this instance, so set the scale back to one.
transform.localScale = Vector3.one;

//The resize factor is the entity instance size divided by the entity definition size.
//Alternatively if the base prefab was never scaled, the y scale of the entity GameObject can be used too.
float yScale = entityInstance.Height / (float)entityInstance.Definition.Height;

_renderer.size = new Vector2(_renderer.size.x, yScale);
_collider.size = new Vector2(_collider.size.x, yScale);

//Because the entity's pivot is in the top left, the sprite's pivot was configured accordingly to have the corresponding pivot.
//However, it's not possible to configure a pivot for a BoxCollider2D, so the colliders offset needs to be manually determined.
_collider.offset = new Vector2(_collider.offset.x, -yScale * 0.5f);
}

public int GetPostprocessOrder()
{
return -1;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/LDtkUnity/Samples~/Common/Sprites/Ladder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
143 changes: 143 additions & 0 deletions Assets/LDtkUnity/Samples~/Common/Sprites/Ladder.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Transform:
m_GameObject: {fileID: 392067290705676212}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.5, y: -0.5, z: 0}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
Expand Down Expand Up @@ -76,11 +76,11 @@ SpriteRenderer:
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_Sprite: {fileID: 21300000, guid: a0ec3126b00c562449ef4e99a4da11fc, type: 3}
m_Sprite: {fileID: 21300000, guid: e53a6a46125489046aa60333dd75bf44, type: 3}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_DrawMode: 2
m_Size: {x: 1, y: 1}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
Expand All @@ -97,6 +97,8 @@ GameObject:
m_Component:
- component: {fileID: 5922710633718950015}
- component: {fileID: 2594250568553743851}
- component: {fileID: 4694737798454055876}
- component: {fileID: 8922856363199415460}
m_Layer: 0
m_Name: GridVania_Ladder
m_TagString: Untagged
Expand Down Expand Up @@ -136,3 +138,63 @@ MonoBehaviour:
_setSortingOrder: 1
_setOpacity: 1
_setEntityColor: 1
--- !u!114 &4694737798454055876
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6381717676898315408}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0b0571a2f3dca1a4692e7ec77ada60c9, type: 3}
m_Name:
m_EditorClassIdentifier:
_renderer: {fileID: 2400257763338206285}
_collider: {fileID: 8922856363199415460}
--- !u!61 &8922856363199415460
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6381717676898315408}
m_Enabled: 1
serializedVersion: 3
m_Density: 1
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_ForceSendLayers:
serializedVersion: 2
m_Bits: 4294967295
m_ForceReceiveLayers:
serializedVersion: 2
m_Bits: 4294967295
m_ContactCaptureLayers:
serializedVersion: 2
m_Bits: 4294967295
m_CallbackLayers:
serializedVersion: 2
m_Bits: 4294967295
m_IsTrigger: 1
m_UsedByEffector: 0
m_CompositeOperation: 0
m_CompositeOrder: 0
m_Offset: {x: 0.5, y: -0.5}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0, y: 0}
oldSize: {x: 0, y: 0}
newSize: {x: 0, y: 0}
adaptiveTilingThreshold: 0
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
m_Size: {x: 1, y: 1}
m_EdgeRadius: 0
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Transform:
m_GameObject: {fileID: 392067290705676212}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.5, y: -0.5, z: 0}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
Expand Down Expand Up @@ -76,11 +76,11 @@ SpriteRenderer:
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_Sprite: {fileID: 21300000, guid: a0ec3126b00c562449ef4e99a4da11fc, type: 3}
m_Sprite: {fileID: 21300000, guid: e53a6a46125489046aa60333dd75bf44, type: 3}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_DrawMode: 2
m_Size: {x: 1, y: 1}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
Expand All @@ -97,6 +97,8 @@ GameObject:
m_Component:
- component: {fileID: 5922710633718950015}
- component: {fileID: 2594250568553743851}
- component: {fileID: 6246956699239339519}
- component: {fileID: 5032531129016477869}
m_Layer: 0
m_Name: LevelsVania_Ladder
m_TagString: Untagged
Expand Down Expand Up @@ -136,3 +138,63 @@ MonoBehaviour:
_setSortingOrder: 1
_setOpacity: 1
_setEntityColor: 1
--- !u!114 &6246956699239339519
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6381717676898315408}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0b0571a2f3dca1a4692e7ec77ada60c9, type: 3}
m_Name:
m_EditorClassIdentifier:
_renderer: {fileID: 2400257763338206285}
_collider: {fileID: 5032531129016477869}
--- !u!61 &5032531129016477869
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6381717676898315408}
m_Enabled: 1
serializedVersion: 3
m_Density: 1
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_ForceSendLayers:
serializedVersion: 2
m_Bits: 4294967295
m_ForceReceiveLayers:
serializedVersion: 2
m_Bits: 4294967295
m_ContactCaptureLayers:
serializedVersion: 2
m_Bits: 4294967295
m_CallbackLayers:
serializedVersion: 2
m_Bits: 4294967295
m_IsTrigger: 0
m_UsedByEffector: 0
m_CompositeOperation: 0
m_CompositeOrder: 0
m_Offset: {x: 0.5, y: -0.5}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0, y: 0}
oldSize: {x: 0, y: 0}
newSize: {x: 0, y: 0}
adaptiveTilingThreshold: 0
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
m_Size: {x: 1, y: 1}
m_EdgeRadius: 0

0 comments on commit c827c0d

Please sign in to comment.