question

Claire Rochelmeyer avatar image
Claire Rochelmeyer asked

Unity PlayFabEditorPrefsSO asset recreation if plugin is not in root Assets folder

Every time the project is opened, the PlayFabDataMenu recreates a new PlayFabEditorPrefsSO asset as the Resource.LoadAll fails to get the existing on while the editor is still loading. This is only a noticeable issue if the PlayFab extension is moved out of the root Assets folder (which, I imagine, is probably most situations to meet company project standards).

The solution we used to fix this was to tag the asset with a Label and search the AssetDatabase for that Label when necessary. This seems to be more reliable than loading Resources as it works when the project is opened in editor. The changes to the Instance property in PlayFabEditorPrefsSO are as follows:

public static PlayFabEditorPrefsSO Instance
{
    get
    {
        if (_instance != null) return _instance;


        var guids = AssetDatabase.FindAssets("l:PlayFabEditorPrefs");
        if (guids.Length > 0)
        {
            _instance = AssetDatabase.LoadAssetAtPath(
		AssetDatabase.GUIDToAssetPath(guids[0]),
		typeof(PlayFabEditorPrefsSO)) as PlayFabEditorPrefsSO;
        }
        if (_instance != null) return _instance;


        if (!Directory.Exists(Path.Combine(Application.dataPath, "PlayFabEditorExtensions/Editor")))
            Directory.CreateDirectory(Path.Combine(Application.dataPath, "PlayFabEditorExtensions/Editor"));


        _instance = CreateInstance<PlayFabEditorPrefsSO>();
        AssetDatabase.CreateAsset(_instance, "Assets/PlayFabEditorExtensions/Editor/PlayFabEditorPrefsSO.asset");
        AssetDatabase.SetLabels(_instance, new []{"PlayFabEditorPrefs"});
        AssetDatabase.SaveAssets();
        Debug.LogWarning("Created missing PlayFabEditorPrefsSO file");
        return _instance;
    }
}

Hope this helps someone, but even more, I hope this is taken on board and fixed in a later update :)

unity3d
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image
Seth Du answered

Thanks for the feedback and the workaround solution. This will be very helpful to the community developers. Meanwhile, I will inform the team about this requirement, and this thread will also be updated if there is feedback from the team.

It will be appreciated if you can send a thread in our Feature Request forum --https://community.playfab.com/spaces/24/index.html

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Claire Rochelmeyer avatar image Claire Rochelmeyer commented ·

Could you please mark my comment below as the Best Answer, as it will actually help others if they come here trying to find a solution as opposed to this comment.

Regards,

Claire

0 Likes 0 ·
Claire Rochelmeyer avatar image
Claire Rochelmeyer answered

On further inspection, the issue was still occurring, although less frequently. The issue still being that PlayFabEditorPrefsSO.Instance is still being accessed from constructors that are marked as [InitializeOnLoad]. While Unity is refreshing the Asset Database (usually just after changing git branches to one that has asset changes), AssetDatabase.LoadAssetAtPath is failing and the PlayFabEditorPrefsSO.Instance property is recreating the asset at the default path.

The culprits of this being classes trying to access the stored values "curMainMenuIdx", "curSubMenuIdx" and "PanelIsShown" on PlayFabEditorPrefsSO. Other than the naming inconsistencies, this presents a different issue in the way of source control issues, where simply navigating the PlayFab editor window (without making field changes) causes the asset to override these values, causing unnecessary differences in source control. These such serialized fields shouldn't be recorded in a scriptable object that isn't gitignored.

Incidentally, by removing the serialized fields removes the need of loading the asset instance from the AssetDatabase during a refresh, fixing the initial issue. As the formerly serialized fields do not need to be shared to other team members, I used EditorPrefs to save/set the values instead.

Added to the "menu and helper methods" region of PlayFabEditor:

private const string PanelShownKey = "PlayFabEdEx-PanelShown";

public static bool PanelShown
{
	get => EditorPrefs.GetBool(PanelShownKey, false);
	set => EditorPrefs.GetBool(PanelShownKey, value);
}

private const string MainMenuIndexKey = "PlayFabEdEx-CurrentMainMenuIndex";

public static int CurrentMainMenuIndex
{
	get => EditorPrefs.GetInt(MainMenuIndexKey, 0);
	set => EditorPrefs.SetInt(MainMenuIndexKey, value);
}

private const string SubMenuIndexKey = "PlayFabEdEx-CurrentSubMenuIndex";

public static int CurrentSubMenuIndex
{
	get => EditorPrefs.GetInt(SubMenuIndexKey, 0);
	set => EditorPrefs.SetInt(SubMenuIndexKey, value);
}
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Justin Heasman avatar image
Justin Heasman answered

We are getting this bug too. It is really annoying.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.