AddressableImageLoaderEditor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.AddressableAssets;
  5. using UnityEngine.ResourceManagement.AsyncOperations;
  6. namespace AdaptUI
  7. {
  8. [CustomEditor(typeof(AddressableImageLoader))]
  9. public class AddressableImageLoaderEditor : Editor
  10. {
  11. private Vector2 lastGameViewSize;
  12. private void OnEnable()
  13. {
  14. //EditorApplication.update += CheckGameViewSizeChange;
  15. }
  16. private void OnDisable()
  17. {
  18. //EditorApplication.update -= CheckGameViewSizeChange;
  19. }
  20. private void CheckGameViewSizeChange()
  21. {
  22. Vector2 currentSize = GameViewSizeHelper.GetGameViewSize();
  23. if (currentSize != lastGameViewSize) // 仅在尺寸变化时触发
  24. {
  25. lastGameViewSize = currentSize;
  26. //Debug.Log($"📌 [Editor] Game 视图分辨率变化: {currentSize.x} x {currentSize.y}");
  27. AddressableImageLoader loader = (AddressableImageLoader)target;
  28. LoadImageInEditor(loader);
  29. // loader.LoadImage(); // 重新加载 UI 资源
  30. }
  31. }
  32. public override void OnInspectorGUI()
  33. {
  34. DrawDefaultInspector(); // 默认 Inspector UI
  35. AddressableImageLoader loader = (AddressableImageLoader)target;
  36. if (GUILayout.Button("🔄 刷新 UI(Editor 模式)"))
  37. {
  38. LoadImageInEditor(loader);
  39. }
  40. }
  41. public static void LoadImageInEditor(AddressableImageLoader loader)
  42. {
  43. if (!Application.isPlaying) // 仅在 Editor 模式下执行
  44. {
  45. string label = GetDeviceLabel(loader);
  46. string key = loader.resourceKey;
  47. //Debug.Log($"📌 [Editor] 加载 Addressable 资源: Key = {key}, Label = {label}");
  48. Addressables.LoadAssetsAsync<Sprite>(new[] { key, label }, null, Addressables.MergeMode.Intersection)
  49. .Completed += handle =>
  50. {
  51. if (handle.Status == AsyncOperationStatus.Succeeded && handle.Result.Count > 0)
  52. {
  53. Sprite sprite = handle.Result[0];
  54. Image image = loader.GetComponent<Image>();
  55. if (image != null)
  56. {
  57. Undo.RecordObject(image, "Change Sprite"); // 支持 Undo
  58. image.sprite = sprite;
  59. Debug.Log($"✅ [Editor] UI 资源已更新: {sprite.name}");
  60. EditorUtility.SetDirty(image);
  61. }
  62. }
  63. else
  64. {
  65. Debug.LogWarning($"⚠️ [Editor] 未找到匹配的 Addressable 资源! Key={key}, Label={label}");
  66. }
  67. };
  68. }
  69. }
  70. // private static string GetDeviceLabel(AddressableImageLoader loader)
  71. // {
  72. // if (loader.selectedDevice == AddressableImageLoader.DeviceType.iPad) return "iPad";
  73. // if (loader.selectedDevice == AddressableImageLoader.DeviceType.iPhone) return "iPhone";
  74. // Vector2 gameViewSize = GameViewSizeHelper.GetGameViewSize();
  75. // float aspectRatio = gameViewSize.x / gameViewSize.y;
  76. // Debug.Log($"📌 [Editor] 计算 Game 视图分辨率: width = {gameViewSize.x}, height = {gameViewSize.y}, aspectRatio = {aspectRatio}");
  77. // return aspectRatio < 1.4f ? "iPad" : "iPhone"; // 修正判定逻辑
  78. // }
  79. private static string GetDeviceLabel(AddressableImageLoader loader)
  80. {
  81. if (loader.selectedDevice == AddressableImageLoader.DeviceType.iPad) return "iPad";
  82. if (loader.selectedDevice == AddressableImageLoader.DeviceType.iPhone) return "iPhone";
  83. DeviceTypeHelper.DeviceType detectedType = DeviceTypeHelper.DetectDeviceType();
  84. //Debug.Log($"📌 [Editor] 设备检测结果: {detectedType}");
  85. return detectedType == DeviceTypeHelper.DeviceType.iPad ? "iPad" : "iPhone";
  86. }
  87. }
  88. }