Clean / Regular build

I have a “PlayerProfile” scriptable object that keeps the save data for my game. This consists of unlocked levels.

I also have a build controller script that erases the saved data from the editor so that the builds don’t have any unlocked levels from the beginning. This code runs normally when I make a clean build but doesn’t work when I use the regular build. What’s the reason for that and how can I make sure this data is erased for every single build?

With the IPreprocessBuildWithReport interface you can reset your scriptable object asset before building the player.

1 Like

We don’t see your script nor when or how it runs. That makes reasoning about it awfully hard. :wink:

1 Like

This is the Build Controller:

public class BuildControl : IPreprocessBuildWithReport
{
    public int callbackOrder => 0;
    public static string requiredScenesAssetFileName = "PLAYERSETTINGS"; //Asset filename

    public void OnPreprocessBuild(BuildReport report)
    {
        UnityEditor.EditorBuildSettingsScene[] buildSettingsScenes = UnityEditor.EditorBuildSettings.scenes;
        var playerSettings = Resources.Load<GameObject>(requiredScenesAssetFileName).GetComponent<PlayerSettings>();
        Debug.Log(playerSettings);
        playerSettings.ClearProfile();
    }
}

And this is the “ClearProfile” method from Player Settings:

public void ClearProfile()
    {
        completedLevels.Clear();
    }

“completedLevels” is the list that holds the record for completed levels.

You are not saving this asset. While in the editor, the already in-memory version is used which will have a cleared list. But your build uses the asset (the file).

Try calling AssetDatabase.SaveAssetIfDirty after clearing the list. If that doesn’t do it you will have to call EditorUtility.SetDirty beforehand.

I did that and it looks like it’s working as I intended. So the difference between a regular build and a clean build is that the clean build saves the assets before starting the build whereas the regular build just uses whatever is in memory, is that correct?

I don’t know but if that’s what you observe than at least for your case it’s correct.
No idea what clean build actually does, I’d think it would only wipe the directory but perhaps it also runs a full project save or AssetDatabase refresh during the “cleaning” process.