Skip to content

Commit a7be947

Browse files
committed
fix: Fixed separate levels not reimporting when the configured fields in the project importer inspector was changed. This was fixed by introducing a new config file that gets generated by the project importer (.ldtkc)
1 parent 9ddc70a commit a7be947

28 files changed

+537
-3
lines changed

Assets/LDtkUnity/Editor/Builders/LDtkBuilderProject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LDtkUnity.Editor
44
{
5+
//todo: add support for runtime building startting from here. use the LDtkConfig object instead of the importers
56
internal sealed class LDtkProjectBuilder
67
{
78
private readonly LDtkProjectImporter _project;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using UnityEditor;
2+
using UnityEditor.AssetImporters;
3+
4+
namespace LDtkUnity.Editor
5+
{
6+
[CanEditMultipleObjects]
7+
[CustomEditor(typeof(LDtkConfigImporter))]
8+
internal sealed class LDtkConfigImporterEditor : ScriptedImporterEditor
9+
{
10+
public override void OnInspectorGUI()
11+
{
12+
const string msg = "This file is generated by the project importer.\n" +
13+
"It contains the configuration data from the project inspector.\n" +
14+
"It is only used for telling the levels to reimport when this is changed.";
15+
EditorGUILayout.HelpBox(msg, MessageType.Info);
16+
ApplyRevertGUI();
17+
}
18+
}
19+
}

Assets/LDtkUnity/Editor/CustomEditor/Importer/LDtkConfigImporterEditor.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using UnityEngine;
2+
3+
#if UNITY_2020_2_OR_NEWER
4+
using UnityEditor.AssetImporters;
5+
#else
6+
using UnityEditor.Experimental.AssetImporters;
7+
#endif
8+
9+
namespace LDtkUnity.Editor
10+
{
11+
[ScriptedImporter(LDtkImporterConsts.CONFIG_VERSION, LDtkImporterConsts.CONFIG_EXT, LDtkImporterConsts.CONFIG_ORDER)]
12+
internal sealed class LDtkConfigImporter : ScriptedImporter
13+
{
14+
public override void OnImportAsset(AssetImportContext ctx)
15+
{
16+
LDtkConfigData data = LDtkConfigData.ReadJson(assetPath);
17+
18+
LDtkConfig obj = ScriptableObject.CreateInstance<LDtkConfig>();
19+
obj._data = data;
20+
21+
ctx.AddObjectToAsset("main", obj, LDtkIconUtility.LoadListIcon());
22+
ctx.SetMainObject(obj);
23+
}
24+
}
25+
}

Assets/LDtkUnity/Editor/ScriptedImporter/LDtkConfigImporter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/LDtkUnity/Editor/ScriptedImporter/LDtkImporterConsts.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
{
33
internal static class LDtkImporterConsts
44
{
5-
public const int PROJECT_VERSION = 31;
6-
public const int LEVEL_VERSION = 15;
5+
public const int PROJECT_VERSION = 32;
6+
public const int LEVEL_VERSION = 16;
77
public const int TILESET_VERSION = 8;
8+
public const int CONFIG_VERSION = 0;
9+
810
public const string MINIMUM_JSON_VERSION = "1.5.0";
911
public const string EXPORT_APP_VERSION_REQUIRED = "1.5.3.1";
1012

1113
public const string PROJECT_EXT = "ldtk";
1214
public const string LEVEL_EXT = "ldtkl";
1315
public const string TILESET_EXT = "ldtkt";
16+
public const string CONFIG_EXT = "ldtkc";
1417

1518
public const int DEFAULT_PPU = 16;
1619
private const int SCRIPTED_IMPORTER_ORDER = 1000;
@@ -21,6 +24,7 @@ internal static class LDtkImporterConsts
2124
//Import order https://forum.unity.com/threads/understanding-import-order-of-native-unity-asset-types.1187845/#post-9171509
2225
//Important to reimport before prefabs (1500)
2326
//99 is the secret parallel import value, but doesnt appear to work. maybe in a future update
27+
public const int CONFIG_ORDER = 1093 - SCRIPTED_IMPORTER_ORDER;
2428
public const int TILESET_ORDER = 1094 - SCRIPTED_IMPORTER_ORDER;
2529
public const int PROJECT_ORDER = 1095 - SCRIPTED_IMPORTER_ORDER;
2630
public const int LEVEL_ORDER = 1099 - SCRIPTED_IMPORTER_ORDER;

Assets/LDtkUnity/Editor/ScriptedImporter/LDtkProjectImporter.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ protected override void Import()
167167
TryGenerateEnums(json);
168168
LDtkProfiler.EndSample();
169169

170+
LDtkProfiler.BeginSample("CreateConfigurationFile");
171+
GenerateConfigurationFile(json);
172+
LDtkProfiler.EndSample();
173+
170174
LDtkProfiler.BeginSample("BufferEditorCache");
171175
BufferEditorCache();
172176
LDtkProfiler.EndSample();
@@ -238,10 +242,34 @@ private void TryCreateTableOfContents(LdtkJson json)
238242
}
239243

240244
Toc = ScriptableObject.CreateInstance<LDtkTableOfContents>();
241-
Toc.name += Path.GetFileNameWithoutExtension(assetPath) + "_Toc";
245+
Toc.name += AssetName + "_Toc";
242246
Toc.Initialize(json);
243247
ImportContext.AddObjectToAsset("toc", Toc, LDtkIconUtility.LoadListIcon());
244248
}
249+
250+
private void GenerateConfigurationFile(LdtkJson json)
251+
{
252+
//only generate the file if separate levels is used
253+
if (!json.ExternalLevels) return;
254+
255+
LDtkConfigData config = new LDtkConfigData()
256+
{
257+
PixelsPerUnit = _pixelsPerUnit,
258+
CustomLevelPrefab = _customLevelPrefab,
259+
IntGridValueColorsVisible = _intGridValueColorsVisible,
260+
UseCompositeCollider = _useCompositeCollider,
261+
GeometryType = _geometryType,
262+
CreateBackgroundColor = _createBackgroundColor,
263+
CreateLevelBoundsTrigger = _createLevelBoundsTrigger,
264+
UseParallax = _useParallax,
265+
IntGridValues = _intGridValues,
266+
Entities = _entities,
267+
};
268+
string writePath = config.WriteJson(assetPath);
269+
270+
//importing the asset if it doesn't exist due to the asset database not refreshing this automatically
271+
AssetDatabase.ImportAsset(writePath);
272+
}
245273

246274
private void BufferEditorCache()
247275
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using UnityEngine;
2+
3+
namespace LDtkUnity.Editor
4+
{
5+
/// <summary>
6+
/// Purely a class to hold the LDtkConfigData as an import artifact
7+
/// </summary>
8+
internal sealed class LDtkConfig : ScriptableObject
9+
{
10+
public LDtkConfigData _data;
11+
}
12+
}

Assets/LDtkUnity/Editor/Utility/Artifacts/LDtkConfig.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using UnityEngine;
5+
6+
namespace LDtkUnity.Editor
7+
{
8+
//todo: could be used to spawn levels in runtime potentially
9+
[Serializable]
10+
internal struct LDtkConfigData
11+
{
12+
public int PixelsPerUnit;
13+
public GameObject CustomLevelPrefab;
14+
public bool IntGridValueColorsVisible;
15+
public bool UseCompositeCollider;
16+
public CompositeCollider2D.GeometryType GeometryType;
17+
public bool CreateBackgroundColor;
18+
public bool CreateLevelBoundsTrigger;
19+
public bool UseParallax;
20+
public LDtkAssetIntGridValue[] IntGridValues;
21+
public LDtkAssetEntity[] Entities;
22+
23+
internal string WriteJson(string projectAssetPath)
24+
{
25+
string writePath = GetPath(projectAssetPath);
26+
string json = JsonUtility.ToJson(this, true);
27+
byte[] byteArray = Encoding.UTF8.GetBytes(json);
28+
29+
LDtkPathUtility.TryCreateDirectoryForFile(writePath);
30+
31+
File.WriteAllBytes(writePath, byteArray);
32+
return writePath;
33+
}
34+
35+
internal static LDtkConfigData ReadJson(string assetPath)
36+
{
37+
if (!File.Exists(assetPath))
38+
{
39+
return new LDtkConfigData();
40+
}
41+
42+
byte[] bytes = File.ReadAllBytes(assetPath);
43+
string json = Encoding.UTF8.GetString(bytes);
44+
return JsonUtility.FromJson<LDtkConfigData>(json);
45+
}
46+
47+
internal static string GetPath(string projectAssetPath)
48+
{
49+
string dir = Path.GetDirectoryName(projectAssetPath);
50+
string importerAssetName = Path.GetFileNameWithoutExtension(projectAssetPath);
51+
return Path.Combine(dir, importerAssetName, $"{importerAssetName}_Config.{LDtkImporterConsts.CONFIG_EXT}");
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)