STARasGAMES/unity-main-scene-auto-loading

Custom path for MainSceneAutoLoadingSettings asset

Flipster77 opened this issue · 4 comments

Hi STARasGAMES, great package, I especially love how it restores the hierarchy state after exiting play mode.

It would be useful if the settings asset could be moved to a different folder and still be loaded.

At the moment the asset path is fixed as "Assets/MainSceneAutoLoadingSettings.asset", which means it has to be at the top level of the project. I would find it good for organisation if I could put it in a subfolder of Assets somewhere, and specify where it is.

The location could possibly be specified via a function of the Tools -> MainSceneAutoLoading menu

Just a suggestion, love the package!

Hey! Thanks for the feedback:)
Do you know how this could be implemented? All tutorials and documentation examples of using SettingsProvider show only fixed-path scenarios.

One way I'm thinking right now is to use AssetDatabase.FindAssets() method to somehow find asset if it is created https://docs.unity3d.com/ScriptReference/AssetDatabase.FindAssets.html.
If no asset in the project, then create it with a fixed path.

Hey STARasGAMES, thanks for implementing this improvement so quickly, I've tried it out and it works well.
To avoid having to find the asset each time when it's not at the default path, I've implemented saving the path to EditorPrefs and using that as the first path to check (or the DefaultAssetPath if there is no path saved to prefs).

In MainSceneAutoLoadingSettings, added this to the top:
private const string ASSET_PATH_KEY = "MainSceneAutoLoadingSettingsPath";

Then changed TryLoadAsset to the following:

        internal static bool TryLoadAsset(out MainSceneAutoLoadingSettings settings)
        {
            string assetPath = EditorPrefs.GetString(ASSET_PATH_KEY, DefaultAssetPath);

            // try to find at the default path
            settings = AssetDatabase.LoadAssetAtPath<MainSceneAutoLoadingSettings>(assetPath);
            if (settings != null)
                return true;

            // if no asset at default path try to find it in project's assets
            var assetGuid = AssetDatabase.FindAssets($"t:{typeof(MainSceneAutoLoadingSettings)}").FirstOrDefault();
            if (string.IsNullOrEmpty(assetGuid))
                return false;
            var path = AssetDatabase.GUIDToAssetPath(assetGuid);
            EditorPrefs.SetString(ASSET_PATH_KEY, path);
            settings = AssetDatabase.LoadAssetAtPath<MainSceneAutoLoadingSettings>(path);
            return settings != null;
        }

Also, I found that the Enabled setting wasn't serializing for me (disabling it wouldn't save it as disabled), so I added this to the end of MainSceneAutoLoadingSettingsEditor's OnInspectorGUI method:

            if (GUI.changed)
            {
                serializedObject.ApplyModifiedProperties();
            }

Enabled should serialize with that added.

Thanks again for the quick update!

Ahh yes! EditorPrefs simplifies this task. One thing is that EditorPrefs are shared across all projects, so I will add some project identification to ASSET_PATH_KEY. Don't think this will crucially change situation, but maybe somebody uses several editor instances at a time.

@Flipster77 Thanks for the suggestions! Changes are live