Last active
October 1, 2021 08:54
-
-
Save grapefrukt/051e351bfe0183a8eed6f7ffd74567b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEditor.Build; | |
using UnityEditor.Build.Reporting; | |
using UnityEditor.U2D; | |
using UnityEngine; | |
using UnityEngine.U2D; | |
internal class TextureCompressionToggler : IPreprocessBuildWithReport, IPostprocessBuildWithReport { | |
public int callbackOrder => 2; | |
public void OnPreprocessBuild(BuildReport report) { | |
Debug.Log("on preprocess"); | |
SetCompression(true); | |
} | |
public void OnPostprocessBuild(BuildReport report) { | |
Debug.Log("on postprocess"); | |
SetCompression(false); | |
} | |
[MenuItem("Edit/TextureCompressorToggler/Enable Compression For All Atlases")] | |
static void EnableCompression() { | |
SetCompression(true); | |
} | |
[MenuItem("Edit/TextureCompressorToggler/Disable Compression For All Atlases")] | |
static void DisableCompression() { | |
SetCompression(false); | |
} | |
static void SetCompression(bool compress) { | |
// Find all assets labelled with 'architecture' that are also texture2D | |
var guids = AssetDatabase.FindAssets("t:spriteAtlas"); | |
Debug.Log($"setting compression flags, for {guids.Length} assets"); | |
foreach (var guid in guids) { | |
var path = AssetDatabase.GUIDToAssetPath(guid); | |
var atlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(path); | |
SetCompression(atlas, compress); | |
} | |
} | |
static void SetCompression(SpriteAtlas atlas, bool compress) { | |
if (compress) { | |
atlas.SetPlatformSettings(new TextureImporterPlatformSettings { | |
textureCompression = TextureImporterCompression.Compressed, | |
}); | |
} else { | |
atlas.SetPlatformSettings( new TextureImporterPlatformSettings { | |
textureCompression = TextureImporterCompression.Uncompressed, | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment