Skip to content

Commit

Permalink
fix: Fixed separate levels not reimporting when the configured fields…
Browse files Browse the repository at this point in the history
… in the project importer inspector was changed. This was fixed by introducing a new config file that gets generated by the project importer (.ldtkc)
  • Loading branch information
Cammin committed Dec 8, 2024
1 parent 9ddc70a commit a7be947
Show file tree
Hide file tree
Showing 28 changed files with 537 additions and 3 deletions.
1 change: 1 addition & 0 deletions Assets/LDtkUnity/Editor/Builders/LDtkBuilderProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LDtkUnity.Editor
{
//todo: add support for runtime building startting from here. use the LDtkConfig object instead of the importers
internal sealed class LDtkProjectBuilder
{
private readonly LDtkProjectImporter _project;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using UnityEditor;
using UnityEditor.AssetImporters;

namespace LDtkUnity.Editor
{
[CanEditMultipleObjects]
[CustomEditor(typeof(LDtkConfigImporter))]
internal sealed class LDtkConfigImporterEditor : ScriptedImporterEditor
{
public override void OnInspectorGUI()
{
const string msg = "This file is generated by the project importer.\n" +
"It contains the configuration data from the project inspector.\n" +
"It is only used for telling the levels to reimport when this is changed.";
EditorGUILayout.HelpBox(msg, MessageType.Info);
ApplyRevertGUI();
}
}
}

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

25 changes: 25 additions & 0 deletions Assets/LDtkUnity/Editor/ScriptedImporter/LDtkConfigImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using UnityEngine;

#if UNITY_2020_2_OR_NEWER
using UnityEditor.AssetImporters;
#else
using UnityEditor.Experimental.AssetImporters;
#endif

namespace LDtkUnity.Editor
{
[ScriptedImporter(LDtkImporterConsts.CONFIG_VERSION, LDtkImporterConsts.CONFIG_EXT, LDtkImporterConsts.CONFIG_ORDER)]
internal sealed class LDtkConfigImporter : ScriptedImporter
{
public override void OnImportAsset(AssetImportContext ctx)
{
LDtkConfigData data = LDtkConfigData.ReadJson(assetPath);

LDtkConfig obj = ScriptableObject.CreateInstance<LDtkConfig>();
obj._data = data;

ctx.AddObjectToAsset("main", obj, LDtkIconUtility.LoadListIcon());
ctx.SetMainObject(obj);
}
}
}

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 @@ -2,15 +2,18 @@
{
internal static class LDtkImporterConsts
{
public const int PROJECT_VERSION = 31;
public const int LEVEL_VERSION = 15;
public const int PROJECT_VERSION = 32;
public const int LEVEL_VERSION = 16;
public const int TILESET_VERSION = 8;
public const int CONFIG_VERSION = 0;

public const string MINIMUM_JSON_VERSION = "1.5.0";
public const string EXPORT_APP_VERSION_REQUIRED = "1.5.3.1";

public const string PROJECT_EXT = "ldtk";
public const string LEVEL_EXT = "ldtkl";
public const string TILESET_EXT = "ldtkt";
public const string CONFIG_EXT = "ldtkc";

public const int DEFAULT_PPU = 16;
private const int SCRIPTED_IMPORTER_ORDER = 1000;
Expand All @@ -21,6 +24,7 @@ internal static class LDtkImporterConsts
//Import order https://forum.unity.com/threads/understanding-import-order-of-native-unity-asset-types.1187845/#post-9171509
//Important to reimport before prefabs (1500)
//99 is the secret parallel import value, but doesnt appear to work. maybe in a future update
public const int CONFIG_ORDER = 1093 - SCRIPTED_IMPORTER_ORDER;
public const int TILESET_ORDER = 1094 - SCRIPTED_IMPORTER_ORDER;
public const int PROJECT_ORDER = 1095 - SCRIPTED_IMPORTER_ORDER;
public const int LEVEL_ORDER = 1099 - SCRIPTED_IMPORTER_ORDER;
Expand Down
30 changes: 29 additions & 1 deletion Assets/LDtkUnity/Editor/ScriptedImporter/LDtkProjectImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ protected override void Import()
TryGenerateEnums(json);
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("CreateConfigurationFile");
GenerateConfigurationFile(json);
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("BufferEditorCache");
BufferEditorCache();
LDtkProfiler.EndSample();
Expand Down Expand Up @@ -238,10 +242,34 @@ private void TryCreateTableOfContents(LdtkJson json)
}

Toc = ScriptableObject.CreateInstance<LDtkTableOfContents>();
Toc.name += Path.GetFileNameWithoutExtension(assetPath) + "_Toc";
Toc.name += AssetName + "_Toc";
Toc.Initialize(json);
ImportContext.AddObjectToAsset("toc", Toc, LDtkIconUtility.LoadListIcon());
}

private void GenerateConfigurationFile(LdtkJson json)
{
//only generate the file if separate levels is used
if (!json.ExternalLevels) return;

LDtkConfigData config = new LDtkConfigData()
{
PixelsPerUnit = _pixelsPerUnit,
CustomLevelPrefab = _customLevelPrefab,
IntGridValueColorsVisible = _intGridValueColorsVisible,
UseCompositeCollider = _useCompositeCollider,
GeometryType = _geometryType,
CreateBackgroundColor = _createBackgroundColor,
CreateLevelBoundsTrigger = _createLevelBoundsTrigger,
UseParallax = _useParallax,
IntGridValues = _intGridValues,
Entities = _entities,
};
string writePath = config.WriteJson(assetPath);

//importing the asset if it doesn't exist due to the asset database not refreshing this automatically
AssetDatabase.ImportAsset(writePath);
}

private void BufferEditorCache()
{
Expand Down
12 changes: 12 additions & 0 deletions Assets/LDtkUnity/Editor/Utility/Artifacts/LDtkConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using UnityEngine;

namespace LDtkUnity.Editor
{
/// <summary>
/// Purely a class to hold the LDtkConfigData as an import artifact
/// </summary>
internal sealed class LDtkConfig : ScriptableObject
{
public LDtkConfigData _data;
}
}
11 changes: 11 additions & 0 deletions Assets/LDtkUnity/Editor/Utility/Artifacts/LDtkConfig.cs.meta

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

54 changes: 54 additions & 0 deletions Assets/LDtkUnity/Editor/Utility/Artifacts/LDtkConfigData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.IO;
using System.Text;
using UnityEngine;

namespace LDtkUnity.Editor
{
//todo: could be used to spawn levels in runtime potentially
[Serializable]
internal struct LDtkConfigData
{
public int PixelsPerUnit;
public GameObject CustomLevelPrefab;
public bool IntGridValueColorsVisible;
public bool UseCompositeCollider;
public CompositeCollider2D.GeometryType GeometryType;
public bool CreateBackgroundColor;
public bool CreateLevelBoundsTrigger;
public bool UseParallax;
public LDtkAssetIntGridValue[] IntGridValues;
public LDtkAssetEntity[] Entities;

internal string WriteJson(string projectAssetPath)
{
string writePath = GetPath(projectAssetPath);
string json = JsonUtility.ToJson(this, true);
byte[] byteArray = Encoding.UTF8.GetBytes(json);

LDtkPathUtility.TryCreateDirectoryForFile(writePath);

File.WriteAllBytes(writePath, byteArray);
return writePath;
}

internal static LDtkConfigData ReadJson(string assetPath)
{
if (!File.Exists(assetPath))
{
return new LDtkConfigData();
}

byte[] bytes = File.ReadAllBytes(assetPath);
string json = Encoding.UTF8.GetString(bytes);
return JsonUtility.FromJson<LDtkConfigData>(json);
}

internal static string GetPath(string projectAssetPath)
{
string dir = Path.GetDirectoryName(projectAssetPath);
string importerAssetName = Path.GetFileNameWithoutExtension(projectAssetPath);
return Path.Combine(dir, importerAssetName, $"{importerAssetName}_Config.{LDtkImporterConsts.CONFIG_EXT}");
}
}
}

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 @@ -58,6 +58,20 @@ public static string[] GatherLevelDependencies(string levelPath)
//in this case, it's:
//- LDtkIntGridValue assets (so tilemaps tile references are updated properly),
//- Entity prefabs, and level prefab (so that prefabs in the import result are updated, because normally they aren't after editing a prefab)

//levels are not updated with the new/removed dependencies when changed in the project importer inspector. therefore, we need to at least specifically depend on the project meta data.
//paths.Add(projectPath + ".meta");
//NEW DEVELOPMENT: we should only depend on the meta file of the project, but not the source asset.
//We ended up choosing to generate a new file. Here's how its referenced.
string pathToProjectConfig = LDtkConfigData.GetPath(projectPath);
if (File.Exists(pathToProjectConfig))
{
paths.Add(pathToProjectConfig);
}
else
{
LDtkDebug.LogWarning($"Could not find the level's project configuration file from {levelPath}. This will make levels not update properly when the project is changed in the importer inspector.");
}

//Within the above types of assets we want to depend on, we only want to depend on assets that are used in a particular level as to further prevent unnecessary reimports.
DugDependencyDataLevel depends = new DugDependencyDataLevel();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"PixelsPerUnit": 16,
"CustomLevelPrefab": {
"instanceID": 0
},
"IntGridValueColorsVisible": true,
"UseCompositeCollider": true,
"GeometryType": 0,
"CreateBackgroundColor": true,
"CreateLevelBoundsTrigger": false,
"UseParallax": true,
"IntGridValues": [
{
"_key": "IntGrid_1",
"_asset": {
"instanceID": 22226
}
}
],
"Entities": []
}

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
@@ -0,0 +1,33 @@
{
"PixelsPerUnit": 16,
"CustomLevelPrefab": {
"instanceID": 0
},
"IntGridValueColorsVisible": true,
"UseCompositeCollider": true,
"GeometryType": 0,
"CreateBackgroundColor": true,
"CreateLevelBoundsTrigger": false,
"UseParallax": true,
"IntGridValues": [
{
"_key": "IntGrid_1",
"_asset": {
"instanceID": 29410
}
},
{
"_key": "IntGrid_2",
"_asset": {
"instanceID": 28118
}
},
{
"_key": "IntGrid_3",
"_asset": {
"instanceID": 24110
}
}
],
"Entities": []
}

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

Loading

0 comments on commit a7be947

Please sign in to comment.