Skip to content

Commit

Permalink
perf: additional performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunderscore committed Nov 29, 2024
1 parent 3918c20 commit d765815
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
3 changes: 0 additions & 3 deletions Editor/API/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ public BuildContext(GameObject obj, string assetRootPath, bool isClone = true)

if (assetRootPath != null)
{
// Ensure the target directory exists
Directory.CreateDirectory(assetRootPath);

AssetSaver = new AssetSaver(assetRootPath, avatarName);
}
else
Expand Down
55 changes: 44 additions & 11 deletions Editor/API/Serialization/AssetSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,55 @@ internal AssetSaver(string generatedAssetsRoot, string avatarName, int assetsPer

if (AssetDatabase.IsValidFolder(subAssetPath))
{
using var _2 = new ProfilerScope("DeleteFolder");
AssetDatabase.DeleteAsset(rootPath);
}

using var _ = new ProfilerScope("Asset editing");
try
{
AssetDatabase.StartAssetEditing();

// Ensure directory exists recursively
var pathParts = subAssetPath.Split('/');
var currentDir = pathParts[0];

for (int i = 1; i < pathParts.Length; i++)
{
var nextDir = currentDir + "/" + pathParts[i];
if (!AssetDatabase.IsValidFolder(nextDir))
{
if (Directory.Exists(nextDir))
{
// Sometimes the asset database can be unaware of a folder on disk, so refresh it.
// This is quite expensive, so only do it if necessary.
Debug.Log("Force refresh due to " + nextDir);
using var _2 = new ProfilerScope("AssetDatabase.Refresh");
AssetDatabase.Refresh();
}
else
{
using var _2 = new ProfilerScope("CreateFolder");
AssetDatabase.CreateFolder(currentDir, pathParts[i]);
}
}

currentDir = nextDir;
}

var rootAssetPath = AssetDatabase.GenerateUniqueAssetPath(rootPath + "/" + avatarName + ".asset");
_rootAsset = ScriptableObject.CreateInstance<GeneratedAssets>();
AssetDatabase.CreateAsset(_rootAsset, rootAssetPath);
_currentSubContainer = CreateAssetContainer();

// Ensure directory exists recursively
if (!AssetDatabase.IsValidFolder(subAssetPath))
_assetCount = 0;
}
finally
{
Directory.CreateDirectory(subAssetPath);
AssetDatabase.Refresh();
using var _2 = new ProfilerScope("StopAssetEditing");

AssetDatabase.StopAssetEditing();
}

var rootAssetPath = AssetDatabase.GenerateUniqueAssetPath(rootPath + "/" + avatarName + ".asset");
_rootAsset = ScriptableObject.CreateInstance<GeneratedAssets>();
AssetDatabase.CreateAsset(_rootAsset, rootAssetPath);
_currentSubContainer = CreateAssetContainer();

_assetCount = 0;
}

public void SaveAsset(UnityEngine.Object? obj)
Expand Down

0 comments on commit d765815

Please sign in to comment.