147 lines
5.2 KiB
C#
147 lines
5.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Logic.Editor
|
|
{
|
|
public static class UIScrollSensitivityTools
|
|
{
|
|
private const float DefaultScrollSensitivity = 20f;
|
|
private const string UIPrefabRoot = "Assets/BundleResources/Prefab/UI";
|
|
|
|
[MenuItem("Tools/UI/扫描低速 ScrollRect 滚轮速度")]
|
|
private static void ScanSlowScrollRects()
|
|
{
|
|
var slowScrollRects = FindSlowScrollRects(fix: false);
|
|
if (slowScrollRects.Count == 0)
|
|
{
|
|
Debug.Log($"[UIScrollSensitivityTools] No ScrollRect below {DefaultScrollSensitivity.ToString(CultureInfo.InvariantCulture)} found in {UIPrefabRoot}.");
|
|
return;
|
|
}
|
|
|
|
foreach (var item in slowScrollRects)
|
|
{
|
|
Debug.LogWarning($"[UIScrollSensitivityTools] {item.AssetPath} :: {item.HierarchyPath} = {item.ScrollSensitivity.ToString(CultureInfo.InvariantCulture)}");
|
|
}
|
|
|
|
Debug.LogWarning($"[UIScrollSensitivityTools] Found {slowScrollRects.Count} ScrollRect(s) below {DefaultScrollSensitivity.ToString(CultureInfo.InvariantCulture)}.");
|
|
}
|
|
|
|
[MenuItem("Tools/UI/修复低速 ScrollRect 滚轮速度")]
|
|
private static void FixSlowScrollRects()
|
|
{
|
|
var fixedScrollRects = FindSlowScrollRects(fix: true);
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
Debug.Log($"[UIScrollSensitivityTools] Fixed {fixedScrollRects.Count} ScrollRect(s) below {DefaultScrollSensitivity.ToString(CultureInfo.InvariantCulture)} in {UIPrefabRoot}.");
|
|
}
|
|
|
|
private static List<SlowScrollRectInfo> FindSlowScrollRects(bool fix)
|
|
{
|
|
var result = new List<SlowScrollRectInfo>();
|
|
var prefabGuids = AssetDatabase.FindAssets("t:Prefab", new[] { UIPrefabRoot });
|
|
|
|
foreach (var guid in prefabGuids)
|
|
{
|
|
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
|
var root = PrefabUtility.LoadPrefabContents(assetPath);
|
|
var changed = false;
|
|
|
|
try
|
|
{
|
|
foreach (var scrollRect in root.GetComponentsInChildren<ScrollRect>(true))
|
|
{
|
|
if (!NeedsFix(scrollRect.scrollSensitivity))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
result.Add(new SlowScrollRectInfo(assetPath, GetHierarchyPath(scrollRect.transform, root.transform), scrollRect.scrollSensitivity));
|
|
|
|
if (fix)
|
|
{
|
|
scrollRect.scrollSensitivity = DefaultScrollSensitivity;
|
|
EditorUtility.SetDirty(scrollRect);
|
|
changed = true;
|
|
}
|
|
}
|
|
|
|
if (changed)
|
|
{
|
|
PrefabUtility.SaveAsPrefabAsset(root, assetPath);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
PrefabUtility.UnloadPrefabContents(root);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private static bool NeedsFix(float scrollSensitivity)
|
|
{
|
|
return scrollSensitivity > 0f && scrollSensitivity < DefaultScrollSensitivity;
|
|
}
|
|
|
|
private static string GetHierarchyPath(Transform transform, Transform root)
|
|
{
|
|
var names = new List<string>();
|
|
var current = transform;
|
|
|
|
while (current != null)
|
|
{
|
|
names.Add(current.name);
|
|
if (current == root)
|
|
{
|
|
break;
|
|
}
|
|
|
|
current = current.parent;
|
|
}
|
|
|
|
names.Reverse();
|
|
return string.Join("/", names);
|
|
}
|
|
|
|
private readonly struct SlowScrollRectInfo
|
|
{
|
|
public readonly string AssetPath;
|
|
public readonly string HierarchyPath;
|
|
public readonly float ScrollSensitivity;
|
|
|
|
public SlowScrollRectInfo(string assetPath, string hierarchyPath, float scrollSensitivity)
|
|
{
|
|
AssetPath = assetPath;
|
|
HierarchyPath = hierarchyPath;
|
|
ScrollSensitivity = scrollSensitivity;
|
|
}
|
|
}
|
|
}
|
|
|
|
public class UIScrollSensitivityPostprocessor : AssetPostprocessor
|
|
{
|
|
private const float DefaultScrollSensitivity = 20f;
|
|
private const string UIPrefabRoot = "Assets/BundleResources/Prefab/UI/";
|
|
|
|
private void OnPostprocessPrefab(GameObject root)
|
|
{
|
|
if (!assetPath.StartsWith(UIPrefabRoot, System.StringComparison.Ordinal))
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var scrollRect in root.GetComponentsInChildren<ScrollRect>(true))
|
|
{
|
|
if (scrollRect.scrollSensitivity > 0f && scrollRect.scrollSensitivity < DefaultScrollSensitivity)
|
|
{
|
|
scrollRect.scrollSensitivity = DefaultScrollSensitivity;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|