Skip to content

Commit fa82d60

Browse files
committed
Started using guids instead of paths for edited prefabs
This is done to properly handle cases when prefabs are moved or deleted in Play Mode.
1 parent 79e66ef commit fa82d60

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

Editor/Prefab Handling/ChangedPrefabs.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class ChangedPrefabs : IEnumerable<ValueTuple<string, string>>
1414
{
1515
private const string KeyName = nameof(ChangedPrefabs);
1616

17-
[SerializeField] private string[] _paths;
17+
[SerializeField] private string[] _guids;
1818
[SerializeField] private string[] _contents;
1919

2020
private static ChangedPrefabs _instance;
@@ -35,12 +35,12 @@ public static ChangedPrefabs Instance
3535
}
3636
}
3737

38-
public (string path, string content) this[int index]
38+
public (string guid, string content) this[int index]
3939
{
40-
get => (_paths[index], _contents[index]);
40+
get => (_guids[index], _contents[index]);
4141
set
4242
{
43-
_paths[index] = value.path;
43+
_guids[index] = value.guid;
4444
_contents[index] = value.content;
4545
}
4646
}
@@ -49,7 +49,7 @@ public static void Initialize(int length)
4949
{
5050
_instance = new ChangedPrefabs
5151
{
52-
_paths = new string[length],
52+
_guids = new string[length],
5353
_contents = new string[length]
5454
};
5555
}
@@ -101,7 +101,7 @@ public Enumerator(ChangedPrefabs instance)
101101

102102
public bool MoveNext()
103103
{
104-
return ++_index < Instance._paths.Length;
104+
return ++_index < Instance._guids.Length;
105105
}
106106

107107
public void Reset() => _index = 0;

Editor/Prefab Handling/PrefabFolderStripper.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private static void StripFoldersFromDependentPrefabs()
7474
for (int i = 0; i < prefabsWithLabel.Length; i++)
7575
{
7676
string path = prefabsWithLabel[i];
77-
ChangedPrefabs.Instance[i] = (path, File.ReadAllText(path));
77+
ChangedPrefabs.Instance[i] = (AssetDatabase.AssetPathToGUID(path), File.ReadAllText(path));
7878
StripFoldersFromPrefab(path, StripSettings.Build);
7979
}
8080

@@ -106,7 +106,7 @@ private static void StripFoldersFromAllPrefabs()
106106
string guid = prefabGUIDs[i];
107107
string path = AssetDatabase.GUIDToAssetPath(guid);
108108

109-
ChangedPrefabs.Instance[i] = (path, File.ReadAllText(path));
109+
ChangedPrefabs.Instance[i] = (guid, File.ReadAllText(path));
110110
StripFoldersFromPrefab(path, StripSettings.PlayMode);
111111
}
112112

@@ -141,8 +141,15 @@ private static void StripFoldersFromPrefab(string prefabPath, StrippingMode stri
141141

142142
private static void RevertChanges()
143143
{
144-
foreach ((string path, string content) in ChangedPrefabs.Instance)
144+
foreach ((string guid, string content) in ChangedPrefabs.Instance)
145145
{
146+
string path = AssetDatabase.GUIDToAssetPath(guid);
147+
148+
// The asset might have been deleted in Play Mode. Additionally, event if the asset is deleted,
149+
// AssetDatabase might still hold a reference to it, so a File.Exists check is needed.
150+
if (string.IsNullOrEmpty(path) || ! File.Exists(path))
151+
continue;
152+
146153
File.WriteAllText(path, content);
147154
}
148155

0 commit comments

Comments
 (0)