icon预览和自动生成
This commit is contained in:
@@ -4,12 +4,12 @@ using UnityEditor;
|
||||
public class SimpleImageViewerWindow : EditorWindow
|
||||
{
|
||||
private Texture2D image;
|
||||
private string imagePath = "Assets/ResRaw/zh.png";
|
||||
|
||||
// 手动设置显示尺寸
|
||||
private float displayWidth = 389f;
|
||||
private float displayHeight = 690f;
|
||||
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()
|
||||
{
|
||||
@@ -33,40 +33,57 @@ public class SimpleImageViewerWindow : EditorWindow
|
||||
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();
|
||||
|
||||
// 计算图片的宽高比
|
||||
float imageAspect = setWidth / setHeight;
|
||||
|
||||
// 获取窗口的可用区域(考虑一些边距)
|
||||
float availableWidth = position.width;
|
||||
float availableHeight = position.height;
|
||||
|
||||
// 计算自适应尺寸(保持宽高比)
|
||||
float displayWidth = availableWidth;
|
||||
float displayHeight = availableWidth / imageAspect;
|
||||
|
||||
// 如果高度超过可用高度,则根据高度重新计算
|
||||
if (displayHeight > availableHeight)
|
||||
{
|
||||
// 左侧空白区域
|
||||
GUILayout.Space(horizontalSpace);
|
||||
|
||||
// 绘制图片
|
||||
Rect imageRect = GUILayoutUtility.GetRect(displayWidth, displayHeight);
|
||||
imageRect.width = displayWidth;
|
||||
imageRect.height = displayHeight;
|
||||
GUI.DrawTexture(imageRect, image, ScaleMode.StretchToFill);
|
||||
|
||||
// 右侧空白区域
|
||||
GUILayout.Space(horizontalSpace);
|
||||
displayHeight = availableHeight;
|
||||
displayWidth = availableHeight * imageAspect;
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
// // 显示图片信息
|
||||
// EditorGUILayout.Space();
|
||||
// EditorGUILayout.LabelField("图片信息", EditorStyles.boldLabel);
|
||||
// EditorGUILayout.LabelField($"路径: {imagePath}");
|
||||
// EditorGUILayout.LabelField($"Unity纹理尺寸: {image.width} x {image.height}");
|
||||
// EditorGUILayout.LabelField($"显示尺寸: {displayWidth} x {displayHeight}");
|
||||
|
||||
// 计算居中位置
|
||||
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