Files
Fishing2/Assets/Scripts/Editor/ImageViewerWindow.cs
2025-09-08 17:53:51 +08:00

72 lines
2.3 KiB
C#

using UnityEngine;
using UnityEditor;
public class SimpleImageViewerWindow : EditorWindow
{
private Texture2D image;
private string imagePath = "Assets/ResRaw/zh.png";
// 手动设置显示尺寸
private float displayWidth = 389f;
private float displayHeight = 690f;
[MenuItem("NBC/ZH/Simple Image Viewer")]
public static void ShowWindow()
{
GetWindow<SimpleImageViewerWindow>("Game");
}
private void OnEnable()
{
// 加载图片
image = AssetDatabase.LoadAssetAtPath<Texture2D>(imagePath);
if (image == null)
{
Debug.LogError($"无法加载图片: {imagePath}");
}
}
private void OnGUI()
{
if (image == null)
{
EditorGUILayout.HelpBox($"图片加载失败: {imagePath}", MessageType.Error);
return;
}
// // 显示尺寸设置
// EditorGUILayout.LabelField("显示尺寸设置", EditorStyles.boldLabel);
// displayWidth = EditorGUILayout.FloatField("宽度", displayWidth);
// displayHeight = EditorGUILayout.FloatField("高度", displayHeight);
// EditorGUILayout.Space();
// 计算居中所需的水平间距
float horizontalSpace = (position.width - displayWidth) / 2;
horizontalSpace = Mathf.Max(0, horizontalSpace);
// 创建水平布局来实现居中
GUILayout.BeginHorizontal();
{
// 左侧空白区域
GUILayout.Space(horizontalSpace);
// 绘制图片
Rect imageRect = GUILayoutUtility.GetRect(displayWidth, displayHeight);
imageRect.width = displayWidth;
imageRect.height = displayHeight;
GUI.DrawTexture(imageRect, image, ScaleMode.StretchToFill);
// 右侧空白区域
GUILayout.Space(horizontalSpace);
}
GUILayout.EndHorizontal();
// // 显示图片信息
// EditorGUILayout.Space();
// EditorGUILayout.LabelField("图片信息", EditorStyles.boldLabel);
// EditorGUILayout.LabelField($"路径: {imagePath}");
// EditorGUILayout.LabelField($"Unity纹理尺寸: {image.width} x {image.height}");
// EditorGUILayout.LabelField($"显示尺寸: {displayWidth} x {displayHeight}");
}
}