Skip to content

Instantly share code, notes, and snippets.

@t-mat
Last active April 6, 2024 07:17
Show Gist options
  • Save t-mat/fb5c8a063af478b0567c72d9b8f17bdd to your computer and use it in GitHub Desktop.
Save t-mat/fb5c8a063af478b0567c72d9b8f17bdd to your computer and use it in GitHub Desktop.
[Unity] Report all TextureImporter settings as CSV
//
// This Unity Editor script reports all TextureImporter settings as CSV file.
//
// Usage:
// - Invoke "UnityEditor > Help > TextureImporterReporter > Report all TextureImporter settings as CSV"
// - Script creates report CSV file and reports its file name.
//
// License
// -------
// SPDX-FileCopyrightText: Copyright (c) Takayuki Matsuoka
// SPDX-License-Identifier: CC0-1.0
//
#define TEST_TEXTURE_IMPORTER_REPORTER
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEngine;
#if TEST_TEXTURE_IMPORTER_REPORTER
internal static class TestTextureImporterReporter_MenuItem {
private const string MenuBasePath = "Help/TextureImporterReporter";
[MenuItem(MenuBasePath + "/Report all TextureImporter settings as CSV")]
private static void ReportAllTextureImporterSettingsAsCSV() {
string[] targetDirectories =
{
"Assets",
// "Packages", // Enable this line if you need to check Packages/**
};
// As for actual platform names, see:
// https://docs.unity3d.com/ScriptReference/TextureImporter.GetPlatformTextureSettings.html
string[] platformNames =
{
TextureImporterReporter.VirtualDefaultPlatformName,
"Standalone",
// "Web",
"iPhone",
"Android",
// "WebGL",
// "Windows Store Apps",
"PS4",
"XboxOne",
"Nintendo Switch",
// "tvOS",
};
StringBuilder sb = ReportAllTextureImporterSettingsAsCSVToSb(targetDirectories, platformNames);
string dateTime = System.DateTime.Now.ToString("yyyy-MM-ddTHHmmss"); // ISO-8601
string filename = $"{Application.dataPath}/../TextureImporterReport_{dateTime}.csv";
System.IO.File.WriteAllText(filename, sb.ToString());
Debug.Log($"Saved: {filename}");
}
private static StringBuilder ReportAllTextureImporterSettingsAsCSVToSb(
string[]? targetDirectories,
string[]? platformNames
) {
StringBuilder sb = new();
sb.AppendLine($"path,category,name,type,value");
foreach (
TextureImporterReporter.TextureImporterReport textureImporterReport
in TextureImporterReporter.ReportAllTextureImporters(targetDirectories, platformNames)
) {
string path = textureImporterReport.Path;
sb.AppendLine($"path={path}");
{
const string category = "TextureImporter";
TextureImporter target = textureImporterReport.TextureImporter;
IEnumerable<(string, Type, object)> properties = GetAllPublicInstanceProperties(target);
foreach ((string name, Type type, object value) in properties) {
sb.AppendLine($",{category},name={name},type={type},\"value={value}\"");
}
}
{
const string category = "TextureImporterSettings";
TextureImporterSettings target = textureImporterReport.TextureImporterSettings;
IEnumerable<(string, Type, object)> properties = GetAllPublicInstanceProperties(target);
foreach ((string name, Type type, object value) in properties) {
sb.AppendLine($",{category},name={name},type={type},\"value={value}\"");
}
}
{
Dictionary<string, TextureImporterPlatformSettings> dic =
textureImporterReport.TextureImporterPlatformSettingsDic;
foreach ((string platform, TextureImporterPlatformSettings settings) in dic) {
string category = $"Platform={platform}";
IEnumerable<(string, Type, object)> properties = GetAllPublicInstanceProperties(settings);
foreach ((string name, Type type, object value) in properties) {
sb.AppendLine($",{category},name={name},type={type},\"value={value}\"");
}
}
}
}
return sb;
}
private static IEnumerable<(string, Type, object)> GetAllPublicInstanceProperties(object obj) {
const BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance;
PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bindingAttr);
Array.Sort(propertyInfos, ComparisonFunc);
foreach (PropertyInfo propertyInfo in propertyInfos) {
string name = propertyInfo.Name;
Type type = propertyInfo.PropertyType;
object value = propertyInfo.GetValue(obj);
yield return (name, type, value);
}
yield break;
int ComparisonFunc(PropertyInfo a, PropertyInfo b) => string.Compare(a.Name, b.Name, StringComparison.Ordinal);
}
}
#endif // TEST_TEXTURE_IMPORTER_REPORTER
internal static class TextureImporterReporter {
public static IEnumerable<TextureImporterReport> ReportAllTextureImporters(
string[]? targetDirectories = null,
string[]? platformNames = null,
string? query = null
) {
query ??= "t:texture";
string[]? guids = targetDirectories is null
? AssetDatabase.FindAssets(query)
: AssetDatabase.FindAssets(query, targetDirectories);
if (guids is null) {
yield break;
}
foreach (string guid in guids) {
string? path = AssetDatabase.GUIDToAssetPath(guid);
if (path is null) {
continue;
}
TextureImporterReport? textureImporterReport = ReportTextureImporterAtPath(path, platformNames);
if (textureImporterReport is null) {
continue;
}
yield return textureImporterReport;
}
}
public static TextureImporterReport? ReportTextureImporterAtPath(
string path,
string[]? platformNames = null
) {
if (AssetImporter.GetAtPath(path) is not TextureImporter textureImporter) {
return null;
}
return new TextureImporterReport(path, textureImporter, platformNames);
}
public class TextureImporterReport {
public readonly string Path;
public readonly TextureImporter TextureImporter;
public readonly TextureImporterSettings TextureImporterSettings;
public readonly Dictionary<string, TextureImporterPlatformSettings> TextureImporterPlatformSettingsDic;
public TextureImporterReport(string path, TextureImporter textureImporter, string[]? platformNames) {
Path = path;
TextureImporter = textureImporter;
TextureImporterSettings = new TextureImporterSettings();
TextureImporterPlatformSettingsDic = new Dictionary<string, TextureImporterPlatformSettings>();
textureImporter.ReadTextureSettings(TextureImporterSettings);
string[] platforms = platformNames ?? FallbackPlatformNames;
foreach (string platform in platforms) {
TextureImporterPlatformSettings? v = platform == VirtualDefaultPlatformName
? textureImporter.GetDefaultPlatformTextureSettings()
: textureImporter.GetPlatformTextureSettings(platform);
if (v is not null) {
TextureImporterPlatformSettingsDic[platform] = v;
}
}
}
}
// Virtual platform name for GetDefaultPlatformTextureSettings
public const string VirtualDefaultPlatformName = "_Default";
public static readonly string[] FallbackPlatformNames =
{
VirtualDefaultPlatformName, // Virtual platform name for GetDefaultPlatformTextureSettings
"Standalone",
"Web",
"iPhone",
"Android",
"WebGL",
"Windows Store Apps",
"PS4",
"XboxOne",
"Nintendo Switch",
"tvOS",
};
}
#endif // UNITY_EDITOR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment