Skip to content

Commit

Permalink
separate level file generation now links the parent and children, and…
Browse files Browse the repository at this point in the history
… with clean undo support. also positions properly of the levels has a certain layout
  • Loading branch information
Cammin committed Apr 26, 2024
1 parent db23043 commit ef3fa49
Show file tree
Hide file tree
Showing 2 changed files with 250 additions and 607 deletions.
48 changes: 45 additions & 3 deletions Assets/LDtkUnity/Editor/CustomEditor/LDtkComponentWorldEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ private void SpawnLevelsInWorlds()
SpawnLevelsInWorld(world);
}

//putting this here so that property modifications are grouped with the other actions
serializedObject.ApplyModifiedProperties();

Undo.IncrementCurrentGroup();
}

Expand Down Expand Up @@ -113,6 +116,13 @@ private void SpawnLevelsInWorld(LDtkComponentWorld worldComponent)
return;
}

LDtkProjectImporter importer = (LDtkProjectImporter)AssetImporter.GetAtPath(projectPath);
if (importer == null)
{
LDtkDebug.LogError($"This world \"{worldComponent.Identifier}\" could not load it's source project importer. Is this project imported properly?");
return;
}

LDtkProjectFile projectFile = AssetDatabase.LoadAssetAtPath<LDtkProjectFile>(projectPath);
if (projectFile == null)
{
Expand Down Expand Up @@ -140,16 +150,48 @@ private void SpawnLevelsInWorld(LDtkComponentWorld worldComponent)
}

//spawn all level objects
foreach (Level level in world.Levels)
LDtkLinearLevelVector vector = new LDtkLinearLevelVector();
WorldLayout layout = world.WorldLayout.HasValue ? world.WorldLayout.Value : WorldLayout.Free;

//doing this so the undo system treats the array exactly as it should.
SerializedProperty levelsProperty = serializedObject.FindProperty("<Levels>k__BackingField");
levelsProperty.arraySize = world.Levels.Length;

for (int i = 0; i < world.Levels.Length; i++)
{
Level level = world.Levels[i];
GameObject levelPrefab = new LDtkRelativeGetterLevelPrefab().GetRelativeAsset(level, projectPath);
if (levelPrefab == null)
{
continue;
}
Object levelInstance = PrefabUtility.InstantiatePrefab(levelPrefab, worldComponent.transform);

GameObject levelInstance = (GameObject)PrefabUtility.InstantiatePrefab(levelPrefab, worldComponent.transform);
Undo.RegisterCreatedObjectUndo(levelInstance, "Create level prefab instance");

LDtkComponentLevel levelComponent = levelInstance.GetComponent<LDtkComponentLevel>();

//positioning stuff
levelComponent.transform.position = level.UnityWorldSpaceCoord(layout, importer.PixelsPerUnit, vector.Scaler);
switch (layout)
{
case WorldLayout.LinearHorizontal:
vector.Next(level.PxWid);
break;
case WorldLayout.LinearVertical:
vector.Next(level.PxHei);
break;
}

//todo potentially hook up level neighbours?

//setup link to level for this element
levelsProperty.GetArrayElementAtIndex(i).objectReferenceValue = levelComponent;

//setup link to project
SerializedObject levelObj = new SerializedObject(levelComponent);
levelObj.FindProperty("<Parent>k__BackingField").objectReferenceValue = worldComponent;
levelObj.ApplyModifiedProperties();
}
}
}
Expand Down
Loading

0 comments on commit ef3fa49

Please sign in to comment.