Claire Rochelmeyer avatar image
Claire Rochelmeyer suggested

Unity PlayFabEdEx asset recreation and general improvements

This was reported in bugs and I still consider this a bug (should be fixed regardless of votes as it hinders production). See thread here

The main issue being is moving the PlayFabEdEx plugin out of the root assets folder (something that would be done in most if not all projects to keep folder structure manageable in large projects) causes the PlayFabEditorPrefsSO asset to be continuously recreated when opening the project or when Unity refreshes the Asset Database. This unhooks the original asset that has the login info from the Editor Window, and the newly created asset must first be found and deleted before the window locates the correct asset.

This is due to the Resources.LoadAll call failing while Unity is refreshing assets, something that happens each time the project is opened, importing new assets, or changing source control branches.

My initial changes only solved the issue when the project is initially opened, and still caused the issue when assets were being refreshed. The initial changes were 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
		if (_instance != null) return _instance;

                var guids = AssetDatabase.FindAssets("l:PlayFabEditorPrefs");
                if (guids.Length > 0)
			_instance = AssetDatabase.LoadAssetAtPath(
                        	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"});
                Debug.LogWarning("Created missing PlayFabEditorPrefsSO file");
                return _instance;

This was a better way of searching the project for an asset rather than relying on the Resources folder, so I still kept these changes after working out the final solution.

The core of the issue was that PlayFabEditorPrefsSO.Instance was being accessed from constructors that were marked with [InitializeOnLoad], specifically trying to access the stored values "curMainMenuIdx", "curSubMenuIdx" and "PanelIsShown" on PlayFabEditorPrefsSO. Other than the naming inconsistencies, this presented 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);

As these were the only values the PlayFabEditor was trying to access from the scriptable object, by moving the stored values out of it, it's no longer trying to create a new instance every time assets get updated.

On another point, the Unity plugin for PlayFab really needs an overhaul and to fix compile warnings for deprecated web request functions in Unity 2020+. Again this a bug, not a feature request, we shouldn't have to wait for people to search up and vote for a forum post for you to keep your plugin up to date and usable.

10 |1200

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

1 Comment

Justin Heasman avatar image
Justin Heasman commented

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 a Comment

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

Your Opinion Counts

Share your great idea, or help out by voting for other people's ideas.