-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathLDtkConfigData.cs
More file actions
73 lines (64 loc) · 2.52 KB
/
LDtkConfigData.cs
File metadata and controls
73 lines (64 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
namespace LDtkUnity.Editor
{
//todo: could be used in future plans to spawn levels in runtime
[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 bool ScaleEntities;
public LDtkAssetIntGridValue[] IntGridValues;
public LDtkAssetEntity[] Entities;
internal string WriteJson(string projectAssetPath, out bool isChanged)
{
string writePath = GetPath(projectAssetPath);
string json = EditorJsonUtility.ToJson(this, true);
byte[] byteArray = Encoding.UTF8.GetBytes(json);
isChanged = false;
LDtkPathUtility.TryCreateDirectoryForFile(writePath);
//Only write if the contents are actually changed! Otherwise, it's been observed to pollute source control
//It's not good practice to write files to disk during a scripted importer, but it works for now.
if (File.Exists(writePath))
{
byte[] existingBytes = File.ReadAllBytes(writePath);
if (existingBytes.SequenceEqual(byteArray))
{
return writePath;
}
}
isChanged = true;
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);
LDtkConfigData data = new LDtkConfigData();
EditorJsonUtility.FromJsonOverwrite(json, data);
return data;
}
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}");
}
}
}