Skip to content

Instantly share code, notes, and snippets.

@shana
Last active November 19, 2020 15:37
Show Gist options
  • Save shana/220662fad562128eb84aa6f53ffe5d1b to your computer and use it in GitHub Desktop.
Save shana/220662fad562128eb84aa6f53ffe5d1b to your computer and use it in GitHub Desktop.
Addressables analysis rule that raises warnings for textures that aren't marked as addressables, given certain conditions
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.AddressableAssets.Build.AnalyzeRules;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
public class UnusedHDTexturesRule : AnalyzeRule
{
[InitializeOnLoadMethod]
private static void RegisterRule()
{
AnalyzeSystem.RegisterNewRule<UnusedHDTexturesRule>();
}
public override bool CanFix => false;
// implement this to run logic to fix whatever issues this rule has
//public override void FixIssues(AddressableAssetSettings settings)
//{
// base.FixIssues(settings);
//}
public override List<AnalyzeResult> RefreshAnalysis(AddressableAssetSettings settings)
{
ClearAnalysis();
// get a list of all the GUIDs of all adddressable assets that have the label "HD"
var addressableAssets = settings.groups
.Where(x => x != null)
.SelectMany(x => x.entries)
.Where(x => x.labels.Contains("HD"))
.Select(x => new GUID(x.guid));
string[] scenePaths = EditorBuildSettings.scenes.Select(x => x.path).ToArray();
var results = scenePaths
// for each scene, get its dependencies
.SelectMany(AssetDatabase.GetDependencies)
// look for the character texture dependencies
.Where(x => x.StartsWith("Assets/Textures/Characters") &&
!x.Contains("_256") &&
AssetDatabase.GetMainAssetTypeAtPath(x) == typeof(Texture2D))
// get the guid of the texture
.Select(x => new GUID(AssetDatabase.AssetPathToGUID(x)))
// check whether the texture is set as an addressable asset
.Except(addressableAssets)
// return a list of all HD character textures that aren't marked as addressables
.Select(x => new AnalyzeResult
{
resultName = AssetDatabase.GUIDToAssetPath(x.ToString()) + kDelimiter + x,
severity = MessageType.Warning
})
.ToList();
if (results.Count == 0)
results.Add(new AnalyzeResult { resultName = ruleName + " - No issues found." });
return results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment