首次提交
This commit is contained in:
89
Assets/Scripts/Editor/ImageViewerWindow.cs
Normal file
89
Assets/Scripts/Editor/ImageViewerWindow.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
public class SimpleImageViewerWindow : EditorWindow
|
||||
{
|
||||
private Texture2D image;
|
||||
|
||||
private string imagePath = "Assets/ResRaw/ZH/1.png";
|
||||
|
||||
private float setWidth = 750f;
|
||||
private float setHeight = 1334f;
|
||||
|
||||
[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;
|
||||
}
|
||||
|
||||
// 计算图片的宽高比
|
||||
float imageAspect = setWidth / setHeight;
|
||||
|
||||
// 获取窗口的可用区域(考虑一些边距)
|
||||
float availableWidth = position.width;
|
||||
float availableHeight = position.height;
|
||||
|
||||
// 计算自适应尺寸(保持宽高比)
|
||||
float displayWidth = availableWidth;
|
||||
float displayHeight = availableWidth / imageAspect;
|
||||
|
||||
// 如果高度超过可用高度,则根据高度重新计算
|
||||
if (displayHeight > availableHeight)
|
||||
{
|
||||
displayHeight = availableHeight;
|
||||
displayWidth = availableHeight * imageAspect;
|
||||
}
|
||||
|
||||
// 计算居中位置
|
||||
float horizontalSpace = (position.width - displayWidth) / 2;
|
||||
float verticalSpace = (position.height - displayHeight) / 2;
|
||||
|
||||
horizontalSpace = Mathf.Max(0, horizontalSpace);
|
||||
verticalSpace = Mathf.Max(0, verticalSpace);
|
||||
|
||||
// 使用垂直和水平布局来实现居中
|
||||
GUILayout.BeginVertical();
|
||||
{
|
||||
// 顶部空白区域
|
||||
GUILayout.Space(verticalSpace);
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
{
|
||||
// 左侧空白区域
|
||||
GUILayout.Space(horizontalSpace);
|
||||
|
||||
// 绘制图片
|
||||
Rect imageRect = GUILayoutUtility.GetRect(displayWidth, displayHeight);
|
||||
imageRect.width = displayWidth;
|
||||
imageRect.height = displayHeight;
|
||||
GUI.DrawTexture(imageRect, image);
|
||||
|
||||
// 右侧空白区域
|
||||
GUILayout.Space(horizontalSpace);
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
// 底部空白区域 - 使用 FlexibleSpace 来确保图片在垂直方向居中
|
||||
GUILayout.FlexibleSpace();
|
||||
}
|
||||
GUILayout.EndVertical();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user