Skip to content

Commit

Permalink
Started using guids instead of paths for edited prefabs
Browse files Browse the repository at this point in the history
This is done to properly handle cases when prefabs are moved or deleted in Play Mode.
  • Loading branch information
SolidAlloy committed Feb 23, 2021
1 parent 79e66ef commit fa82d60
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
12 changes: 6 additions & 6 deletions Editor/Prefab Handling/ChangedPrefabs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class ChangedPrefabs : IEnumerable<ValueTuple<string, string>>
{
private const string KeyName = nameof(ChangedPrefabs);

[SerializeField] private string[] _paths;
[SerializeField] private string[] _guids;
[SerializeField] private string[] _contents;

private static ChangedPrefabs _instance;
Expand All @@ -35,12 +35,12 @@ public static ChangedPrefabs Instance
}
}

public (string path, string content) this[int index]
public (string guid, string content) this[int index]
{
get => (_paths[index], _contents[index]);
get => (_guids[index], _contents[index]);
set
{
_paths[index] = value.path;
_guids[index] = value.guid;
_contents[index] = value.content;
}
}
Expand All @@ -49,7 +49,7 @@ public static void Initialize(int length)
{
_instance = new ChangedPrefabs
{
_paths = new string[length],
_guids = new string[length],
_contents = new string[length]
};
}
Expand Down Expand Up @@ -101,7 +101,7 @@ public Enumerator(ChangedPrefabs instance)

public bool MoveNext()
{
return ++_index < Instance._paths.Length;
return ++_index < Instance._guids.Length;
}

public void Reset() => _index = 0;
Expand Down
13 changes: 10 additions & 3 deletions Editor/Prefab Handling/PrefabFolderStripper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static void StripFoldersFromDependentPrefabs()
for (int i = 0; i < prefabsWithLabel.Length; i++)
{
string path = prefabsWithLabel[i];
ChangedPrefabs.Instance[i] = (path, File.ReadAllText(path));
ChangedPrefabs.Instance[i] = (AssetDatabase.AssetPathToGUID(path), File.ReadAllText(path));
StripFoldersFromPrefab(path, StripSettings.Build);
}

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

ChangedPrefabs.Instance[i] = (path, File.ReadAllText(path));
ChangedPrefabs.Instance[i] = (guid, File.ReadAllText(path));
StripFoldersFromPrefab(path, StripSettings.PlayMode);
}

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

private static void RevertChanges()
{
foreach ((string path, string content) in ChangedPrefabs.Instance)
foreach ((string guid, string content) in ChangedPrefabs.Instance)
{
string path = AssetDatabase.GUIDToAssetPath(guid);

// The asset might have been deleted in Play Mode. Additionally, event if the asset is deleted,
// AssetDatabase might still hold a reference to it, so a File.Exists check is needed.
if (string.IsNullOrEmpty(path) || ! File.Exists(path))
continue;

File.WriteAllText(path, content);
}

Expand Down

0 comments on commit fa82d60

Please sign in to comment.