Skip to content

Commit

Permalink
Fix fetching and initializing Toolbox features when assets are updating
Browse files Browse the repository at this point in the history
  • Loading branch information
arimger committed Mar 28, 2024
1 parent c6bbdd3 commit dc98d89
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Assets/Editor Toolbox/Editor/ToolboxAssetProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAsset
if (ToolboxManager.Settings &&
ToolboxManager.SettingsGuid == AssetDatabase.AssetPathToGUID(assetPath))
{
ToolboxManager.InitializeSettings(null);
ToolboxManager.TryInitializeSettings(null);
}

return AssetDeleteResult.DidNotDelete;
Expand Down
60 changes: 43 additions & 17 deletions Assets/Editor Toolbox/Editor/ToolboxManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IO;
using System.Collections;
using System.IO;
using Unity.EditorCoroutines.Editor;
using UnityEditor;
using UnityEngine;

Expand Down Expand Up @@ -87,35 +89,62 @@ private static void ManageSceneViewCore(IToolboxSceneViewSettings settings)
ToolboxEditorSceneView.UpdateSettings(settings);
}


[InitializeOnLoadMethod]
internal static bool InitializeSettings()
private static string GetSettingsFileGuid()
{
var guids = AssetDatabase.FindAssets("t:" + settingsType);
string targetGuid = null;
//try to find a settings file in a non-package directory
foreach (var guid in guids)
for (var i = guids.Length - 1; i >= 0; i--)
{
var guid = guids[i];
var path = AssetDatabase.GUIDToAssetPath(guid);
targetGuid = guid;
if (path.StartsWith("Assets"))
{
guids[0] = guid;
break;
}
}

if (InitializeSettings(guids.Length > 0 ? guids[0] : null))
return targetGuid;
}

[InitializeOnLoadMethod]
internal static void InitializeToolbox()
{
IsInitializing = true;
if (TryInitializeSettings())
{
IsInitializing = false;
IsInitialized = true;
return true;
return;
}
else

EditorCoroutineUtility.StartCoroutineOwnerless(InitializeSettingsAsync());
static IEnumerator InitializeSettingsAsync()
{
ToolboxEditorLog.KitInitializationMessage();
return false;
yield return null;
yield return new WaitWhile(() => EditorApplication.isUpdating);
if (TryInitializeSettings())
{
IsInitialized = true;
}
else
{
IsInitialized = false;
ToolboxEditorLog.KitInitializationMessage();
}

IsInitializing = false;
}
}

internal static bool InitializeSettings(string settingsGuid)
internal static bool TryInitializeSettings()
{
var settingsGuid = GetSettingsFileGuid();
return TryInitializeSettings(settingsGuid);
}

internal static bool TryInitializeSettings(string settingsGuid)
{
SettingsGuid = settingsGuid;
SettingsPath = AssetDatabase.GUIDToAssetPath(settingsGuid);
Expand Down Expand Up @@ -152,15 +181,14 @@ internal static void ReimportSettings()
AssetDatabase.ImportAsset(path);
}


[SettingsProvider]
internal static SettingsProvider SettingsProvider()
{
var provider = new SettingsProvider("Project/Editor Toolbox", SettingsScope.Project);

void ReintializeProvider()
{
InitializeSettings();
InitializeToolbox();

//rebuild the settings provider right after initialization
provider.OnDeactivate();
Expand Down Expand Up @@ -226,11 +254,9 @@ void ReintializeProvider()
return provider;
}


internal static bool IsInitialized { get; private set; }

internal static bool IsInitializing { get; private set; }
internal static ToolboxEditorSettings Settings { get; private set; }

internal static string SettingsPath { get; private set; }
internal static string SettingsGuid { get; private set; }
}
Expand Down

0 comments on commit dc98d89

Please sign in to comment.