【m】框架大更新

This commit is contained in:
2025-10-31 11:18:23 +08:00
parent ae6e7c804b
commit 8e1d52ddbf
1883 changed files with 213934 additions and 640 deletions

View File

@@ -0,0 +1,67 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using YooAsset.Editor;
public class SchemaTools
{
/// <summary>
/// 通用扫描快捷方法
/// </summary>
public static List<ReportElement> ScanAssets(string[] scanAssetList, System.Func<string, ReportElement> scanFun, int unloadAssetLimit = int.MaxValue)
{
int scanNumber = 0;
int progressCount = 0;
int totalCount = scanAssetList.Length;
List<ReportElement> results = new List<ReportElement>(totalCount);
EditorTools.ClearProgressBar();
foreach (string assetPath in scanAssetList)
{
scanNumber++;
progressCount++;
EditorTools.DisplayProgressBar("扫描中...", progressCount, totalCount);
var scanResult = scanFun.Invoke(assetPath);
if (scanResult != null)
results.Add(scanResult);
// 释放编辑器未使用的资源
if (scanNumber >= unloadAssetLimit)
{
scanNumber = 0;
EditorUtility.UnloadUnusedAssetsImmediate(true);
}
}
EditorTools.ClearProgressBar();
return results;
}
/// <summary>
/// 通用修复快捷方法
/// </summary>
public static void FixAssets(List<ReportElement> fixAssetList, System.Action<ReportElement> fixFun, int unloadAssetLimit = int.MaxValue)
{
int scanNumber = 0;
int totalCount = fixAssetList.Count;
int progressCount = 0;
EditorTools.ClearProgressBar();
foreach (var scanResult in fixAssetList)
{
scanNumber++;
progressCount++;
EditorTools.DisplayProgressBar("修复中...", progressCount, totalCount);
fixFun.Invoke(scanResult);
// 释放编辑器未使用的资源
if (scanNumber >= unloadAssetLimit)
{
scanNumber = 0;
EditorUtility.UnloadUnusedAssetsImmediate(true);
}
}
EditorTools.ClearProgressBar();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 97a68688a58fbb24ba31cd80e808d315
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,198 @@
#if UNITY_2019_4_OR_NEWER
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using YooAsset.Editor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
[CreateAssetMenu(fileName = "TextureSchema", menuName = "YooAssetArt/Create TextureSchema")]
public class TextureSchema : ScannerSchema
{
/// <summary>
/// 图片最大宽度
/// </summary>
public int MaxWidth = 1024;
/// <summary>
/// 图片最大高度
/// </summary>
public int MaxHeight = 1024;
/// <summary>
/// 测试列表
/// </summary>
public List<string> TestStringValues = new List<string>();
/// <summary>
/// 获取用户指南信息
/// </summary>
public override string GetUserGuide()
{
return "规则介绍:检测图片的格式,尺寸";
}
/// <summary>
/// 运行生成扫描报告
/// </summary>
public override ScanReport RunScanner(AssetArtScanner scanner)
{
// 创建扫描报告
string name = "扫描所有纹理资产";
string desc = GetUserGuide();
var report = new ScanReport(name, desc);
report.AddHeader("资源路径", 600, 500, 1000).SetStretchable().SetSearchable().SetSortable().SetCounter().SetHeaderType(EHeaderType.AssetPath);
report.AddHeader("图片宽度", 100).SetSortable().SetHeaderType(EHeaderType.LongValue);
report.AddHeader("图片高度", 100).SetSortable().SetHeaderType(EHeaderType.LongValue);
report.AddHeader("内存大小", 120).SetSortable().SetUnits("bytes").SetHeaderType(EHeaderType.LongValue);
report.AddHeader("苹果格式", 100);
report.AddHeader("安卓格式", 100);
report.AddHeader("错误信息", 500).SetStretchable();
// 获取扫描资源集合
var searchDirectorys = scanner.Collectors.Select(c => { return c.CollectPath; });
string[] findAssets = EditorTools.FindAssets(EAssetSearchType.Texture, searchDirectorys.ToArray());
// 开始扫描资源集合
var results = SchemaTools.ScanAssets(findAssets, ScanAssetInternal);
report.ReportElements.AddRange(results);
return report;
}
private ReportElement ScanAssetInternal(string assetPath)
{
var importer = TextureTools.GetAssetImporter(assetPath);
if (importer == null)
return null;
// 加载纹理对象
var texture = AssetDatabase.LoadAssetAtPath<Texture>(assetPath);
var assetGUID = AssetDatabase.AssetPathToGUID(assetPath);
var iosFormat = TextureTools.GetPlatformIOSFormat(importer);
var androidFormat = TextureTools.GetPlatformAndroidFormat(importer);
var memorySize = TextureTools.GetStorageMemorySize(texture);
// 获取错误信息
string errorInfo = string.Empty;
{
// 苹果格式
if (iosFormat != TextureImporterFormat.ASTC_4x4)
{
errorInfo += " | ";
errorInfo += "苹果格式不对";
}
// 安卓格式
if (androidFormat != TextureImporterFormat.ASTC_4x4)
{
errorInfo += " | ";
errorInfo += "安卓格式不对";
}
// 多级纹理
if (importer.isReadable)
{
errorInfo += " | ";
errorInfo += "开启了可读写";
}
// 超大纹理
if (texture.width > MaxWidth || texture.height > MaxHeight)
{
errorInfo += " | ";
errorInfo += "超大纹理";
}
}
// 添加扫描信息
ReportElement result = new ReportElement(assetGUID);
result.AddScanInfo("资源路径", assetPath);
result.AddScanInfo("图片宽度", texture.width);
result.AddScanInfo("图片高度", texture.height);
result.AddScanInfo("内存大小", memorySize);
result.AddScanInfo("苹果格式", iosFormat.ToString());
result.AddScanInfo("安卓格式", androidFormat.ToString());
result.AddScanInfo("错误信息", errorInfo);
// 判断是否通过
result.Passes = string.IsNullOrEmpty(errorInfo);
return result;
}
/// <summary>
/// 修复扫描结果
/// </summary>
public override void FixResult(List<ReportElement> fixList)
{
SchemaTools.FixAssets(fixList, FixAssetInternal);
}
private void FixAssetInternal(ReportElement result)
{
var scanInfo = result.GetScanInfo("资源路径");
var assetPath = scanInfo.ScanInfo;
var importer = TextureTools.GetAssetImporter(assetPath);
if (importer == null)
return;
// 苹果格式
var iosPlatformSetting = TextureTools.GetPlatformIOSSettings(importer);
iosPlatformSetting.format = TextureImporterFormat.ASTC_4x4;
iosPlatformSetting.overridden = true;
// 安卓格式
var androidPlatformSetting = TextureTools.GetPlatformAndroidSettings(importer);
androidPlatformSetting.format = TextureImporterFormat.ASTC_4x4;
androidPlatformSetting.overridden = true;
// 可读写
importer.isReadable = false;
// 保存配置
importer.SetPlatformTextureSettings(iosPlatformSetting);
importer.SetPlatformTextureSettings(androidPlatformSetting);
importer.SaveAndReimport();
Debug.Log($"修复了 : {assetPath}");
}
/// <summary>
/// 创建检视面板对
/// </summary>
public override SchemaInspector CreateInspector()
{
var container = new VisualElement();
// 图片最大宽度
var maxWidthField = new IntegerField();
maxWidthField.label = "图片最大宽度";
maxWidthField.SetValueWithoutNotify(MaxWidth);
maxWidthField.RegisterValueChangedCallback((evt) =>
{
MaxWidth = evt.newValue;
});
container.Add(maxWidthField);
// 图片最大高度
var maxHeightField = new IntegerField();
maxHeightField.label = "图片最大高度";
maxHeightField.SetValueWithoutNotify(MaxHeight);
maxHeightField.RegisterValueChangedCallback((evt) =>
{
MaxHeight = evt.newValue;
});
container.Add(maxHeightField);
// 创建测试列表
#if UNITY_2021_3_OR_NEWER
ReorderableListView reorderableListView = new ReorderableListView();
reorderableListView.SourceData = TestStringValues;
reorderableListView.HeaderName = "测试列表";
container.Add(reorderableListView);
#endif
SchemaInspector inspector = new SchemaInspector(container);
return inspector;
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 01b3d3fde6899514396008e1e4f6693b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,104 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using YooAsset.Editor;
public class TextureTools
{
/// <summary>
/// POT尺寸检测
/// </summary>
public static bool IsPowerOfTwo(Texture tex)
{
if (Mathf.IsPowerOfTwo(tex.width) == false)
return false;
if (Mathf.IsPowerOfTwo(tex.height) == false)
return false;
return true;
}
/// <summary>
/// 获取纹理运行时内存大小
/// </summary>
public static long GetStorageMemorySize(Texture tex)
{
#if UNITY_2022_3_OR_NEWER
var assembly = typeof(AssetDatabase).Assembly;
var type = assembly.GetType("UnityEditor.TextureUtil");
long size = (long)EditorTools.InvokePublicStaticMethod(type, "GetStorageMemorySizeLong", tex);
return size;
#else
var assembly = typeof(AssetDatabase).Assembly;
var type = assembly.GetType("UnityEditor.TextureUtil");
int size = (int)EditorTools.InvokePublicStaticMethod(type, "GetStorageMemorySize", tex);
return size;
#endif
}
/// <summary>
/// 获取当前平台纹理的格式
/// </summary>
public static TextureFormat GetCurrentPlatformTextureFormat(Texture tex)
{
var assembly = typeof(AssetDatabase).Assembly;
var type = assembly.GetType("UnityEditor.TextureUtil");
TextureFormat format = (TextureFormat)EditorTools.InvokePublicStaticMethod(type, "GetTextureFormat", tex);
return format;
}
/// <summary>
/// 获取纹理的导入器
/// </summary>
public static TextureImporter GetAssetImporter(string assetPath)
{
TextureImporter importer = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (importer == null)
Debug.LogWarning($"Failed to load TextureImporter : {assetPath}");
return importer;
}
public static TextureImporterPlatformSettings GetPlatformPCSettings(TextureImporter importer)
{
TextureImporterPlatformSettings platformSetting = importer.GetPlatformTextureSettings("Standalone");
return platformSetting;
}
public static TextureImporterPlatformSettings GetPlatformIOSSettings(TextureImporter importer)
{
TextureImporterPlatformSettings platformSetting = importer.GetPlatformTextureSettings("iPhone");
return platformSetting;
}
public static TextureImporterPlatformSettings GetPlatformAndroidSettings(TextureImporter importer)
{
TextureImporterPlatformSettings platformSetting = importer.GetPlatformTextureSettings("Android");
return platformSetting;
}
public static TextureImporterFormat GetPlatformPCFormat(TextureImporter importer)
{
TextureImporterPlatformSettings platformSetting = GetPlatformPCSettings(importer);
var format = platformSetting.format;
if (format.ToString().StartsWith("Automatic"))
format = importer.GetAutomaticFormat("Standalone");
return format;
}
public static TextureImporterFormat GetPlatformIOSFormat(TextureImporter importer)
{
TextureImporterPlatformSettings platformSetting = GetPlatformIOSSettings(importer);
var format = platformSetting.format;
if (format.ToString().StartsWith("Automatic"))
format = importer.GetAutomaticFormat("iPhone");
return format;
}
public static TextureImporterFormat GetPlatformAndroidFormat(TextureImporter importer)
{
TextureImporterPlatformSettings platformSetting = GetPlatformAndroidSettings(importer);
var format = platformSetting.format;
if (format.ToString().StartsWith("Automatic"))
format = importer.GetAutomaticFormat("Android");
return format;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 58de59cc0b28bb1468c32bfa606d999d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: