UnityException: Could not find the requested Platform Texture Settings. This is incorrect, did initialization fail?

I get this error on Unity 2023.2.0b1
Please help

UnityException: Could not find the requested Platform Texture Settings. This is incorrect, did initialization fail?
UnityEditor.TextureImportPlatformSettings.CacheSerializedProperties (UnityEditor.SerializedProperty platformSettingsArray) (at <56badfef365e4bbf81aca5a8903d5a25>:0)
UnityEditor.TextureImporterInspector.<OnInspectorGUI>b__119_1 (UnityEditor.TextureImportPlatformSettings settings) (at <56badfef365e4bbf81aca5a8903d5a25>:0)
System.Collections.Generic.List`1[T].ForEach (System.Action`1[T] action) (at <3ead76266f1846f192cd3a42f27e86c3>:0)
UnityEditor.TextureImporterInspector.OnInspectorGUI () (at <56badfef365e4bbf81aca5a8903d5a25>:0)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass74_0.<CreateInspectorElementUsingIMGUI>b__0 () (at <56badfef365e4bbf81aca5a8903d5a25>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

This is a bug introduced sometime recently in the TexturePlatformSettings importer. Depending on your build target and assets, it will differ, but for me, either:

  • a platform was missing entirely from the .meta file, or
  • the forceMaximumCompressionQuality_BC6H_BC7 setting was missing from a platform.

I’ve created an editor script that fixes both automatically. NOTE: Find a texture that isn’t giving an error, open its .meta file, and change my Settings[] and Platforms[] variables to match what’s in yours!

public class FixTexturePlatformSettings : AssetPostprocessor
{
    private static readonly string[] Settings =
    {
        "  - serializedVersion: 3",
        "    buildTarget: {platform}",
        "    maxTextureSize: 2048",
        "    resizeAlgorithm: 0",
        "    textureFormat: -1",
        "    textureCompression: 1",
        "    compressionQuality: 50",
        "    crunchedCompression: 0",
        "    allowsAlphaSplitting: 0",
        "    overridden: 0",
        "    androidETC2FallbackOverride: 0",
        "    forceMaximumCompressionQuality_BC6H_BC7: 0"
    };

    private static readonly string[] Platforms =
    {
        "DefaultTexturePlatform",
        "Standalone",
        "Server"
    };

    void OnPostprocessTexture(Texture2D texture)
    {
        EditorCoroutineUtility.StartCoroutine(Fix($"{assetPath}.meta"), this);
    }

    private IEnumerator Fix(string metafile)
    {
        // Wait for .meta to be created
        while (!File.ReadAllText(metafile).Contains("platformSettings:"))
            yield return null;

        // Read .meta file
        string original = File.ReadAllText(metafile);
        StringBuilder meta = new(original);

        Debug.Log("[FixTPS] Checking platform settings...");
        foreach (string platform in Platforms)
        {
            if (meta.ToString().Contains(platform))
            {
                // Add missing setting to existing platforms
                if (!meta.ToString().Contains(Settings[^1]))
                {
                    int insertAt = meta.ToString().LastIndexOf(Settings[^2]) + Settings[^2].Length + 1;
                    meta.Insert(insertAt, Settings[^1] + '\n');

                    Debug.Log($"[FixTPS] Added missing setting to platform '{platform}'");
                }
            }
            else
            {
                // Add missing platforms
                string insertAfter = Settings[^1];

                if (meta.ToString().Contains("platformSettings: []"))
                {
                    meta.Replace("platformSettings: []", "platformSettings:");
                    insertAfter = "platformSettings:";

                    Debug.Log($"[FixTPS] Created platform settings array");
                }

                int insertAt = meta.ToString().LastIndexOf(insertAfter) + insertAfter.Length + 1;
                meta.Insert(insertAt, string.Join('\n', Settings).Replace("{platform}", platform) + '\n');

                Debug.Log($"[FixTPS] Added missing platform '{platform}'");
            }
        }

        // Save .meta file
        if (meta.ToString() != original)
        {
            File.WriteAllText(metafile, meta.ToString());
            AssetDatabase.Refresh();
        }
    }
}

The compiler throws errors:
The name 'EditorCoroutineUtility' does not exist in the current context The name 'File' does not exist in the current context

Import Editor Coroutine Package
then , implement using Unity.EditorCoroutines.Editor

Thank you! That worked!