处理画报编辑位置刷新问题;添加大背景设置

main
liuchunguang 2026-02-25 20:39:21 +08:00
parent 06fe688c9d
commit 760b00ec8d
6 changed files with 2937 additions and 490 deletions

View File

@ -4737,6 +4737,7 @@ MonoBehaviour:
1280,\n \"height\": 720,\n \"backgroundWidth\": 789.1300048828125,\n
\"backgroundHeight\": 359.0,\n \"timestamp\": \"2026-01-15 19:46:48\"\n
}\n ]\n}"
mainBackgroundImage: {fileID: 5874448279716915522}
--- !u!114 &8934400169176409913
MonoBehaviour:
m_ObjectHideFlags: 0
@ -9233,7 +9234,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 30, y: 46}
m_AnchoredPosition: {x: 30.00003, y: 46}
m_SizeDelta: {x: 400, y: 60}
m_Pivot: {x: 0, y: 0}
--- !u!222 &2562512154822071990

View File

@ -21,6 +21,10 @@ namespace Framework.UI.Pictorial
[TextArea(5, 20)]
[SerializeField] private string pictorialDataJson;
[Header("大背景设置(可选)")]
[Tooltip("大背景 Image 组件UI_DrawMainPanel/Image_Bg\n如果不设置会自动向上查找 UI_DrawMainPanel 祖先节点下的 Image_Bg")]
[SerializeField] private Image mainBackgroundImage;
private PictorialData pictorialData;
private List<PictorialItem> items = new List<PictorialItem>();
@ -355,6 +359,37 @@ namespace Framework.UI.Pictorial
if (pictorialData == null)
return;
// 恢复大背景图片UI_DrawMainPanel/Image_Bg
if (!string.IsNullOrEmpty(pictorialData.mainBackgroundPath))
{
Image mainBgImage = GetMainBackgroundImage();
if (mainBgImage != null)
{
// 使用AssetManager加载大背景图片
try
{
Sprite mainBgSprite = await AssetManager.Instance.LoadAssetAsync<Sprite>(pictorialData.mainBackgroundPath);
if (mainBgSprite != null)
{
mainBgImage.sprite = mainBgSprite;
Debug.Log($"[画报系统] 重绘 - 大背景图片加载成功: {pictorialData.mainBackgroundPath}");
}
else
{
Debug.LogWarning($"[画报系统] 重绘 - 大背景图片加载失败: {pictorialData.mainBackgroundPath}");
}
}
catch (System.Exception e)
{
Debug.LogError($"[画报系统] 重绘 - 加载大背景图片异常: {pictorialData.mainBackgroundPath}, 错误: {e.Message}\n{e.StackTrace}");
}
}
else
{
Debug.LogWarning($"[画报系统] 重绘 - 未找到大背景 Image 组件,请在 Inspector 中设置或确保 UI_DrawMainPanel/Image_Bg 节点存在");
}
}
// 恢复背景图片到Item_ATemp_Bg_Second节点
if (!string.IsNullOrEmpty(pictorialData.backgroundPath))
{
@ -478,6 +513,37 @@ namespace Framework.UI.Pictorial
return;
}
// 恢复大背景图片UI_DrawMainPanel/Image_Bg
if (!string.IsNullOrEmpty(pictorialData.mainBackgroundPath))
{
Image mainBgImage = GetMainBackgroundImage();
if (mainBgImage != null)
{
// 使用AssetManager加载大背景图片
try
{
Sprite mainBgSprite = await AssetManager.Instance.LoadAssetAsync<Sprite>(pictorialData.mainBackgroundPath);
if (mainBgSprite != null)
{
mainBgImage.sprite = mainBgSprite;
Debug.Log($"[画报系统] 构建 - 大背景图片加载成功: {pictorialData.mainBackgroundPath}");
}
else
{
Debug.LogWarning($"[画报系统] 构建 - 大背景图片加载失败: {pictorialData.mainBackgroundPath}");
}
}
catch (System.Exception e)
{
Debug.LogError($"[画报系统] 构建 - 加载大背景图片异常: {pictorialData.mainBackgroundPath}, 错误: {e.Message}\n{e.StackTrace}");
}
}
else
{
Debug.LogWarning($"[画报系统] 构建 - 未找到大背景 Image 组件,请在 Inspector 中设置或确保 UI_DrawMainPanel/Image_Bg 节点存在");
}
}
// 恢复背景图片到Item_ATemp_Bg_Second节点
if (!string.IsNullOrEmpty(pictorialData.backgroundPath))
{
@ -889,6 +955,55 @@ namespace Framework.UI.Pictorial
ClearItems();
}
/// <summary>
/// 向上查找指定名称的祖先节点
/// </summary>
private Transform FindAncestorByName(Transform current, string ancestorName)
{
Transform parent = current.parent;
while (parent != null)
{
if (parent.name == ancestorName)
{
return parent;
}
parent = parent.parent;
}
return null;
}
/// <summary>
/// 获取大背景 Image 组件(优先使用手动设置的,如果没有则自动查找)
/// </summary>
private Image GetMainBackgroundImage()
{
// 如果已经手动设置了大背景,直接使用
if (mainBackgroundImage != null)
{
return mainBackgroundImage;
}
// 如果没有设置,尝试自动查找
Transform uiDrawMainPanel = FindAncestorByName(transform, "UI_DrawMainPanel");
if (uiDrawMainPanel != null)
{
Transform mainBgTransform = uiDrawMainPanel.Find("Image_Bg");
if (mainBgTransform != null)
{
Image image = mainBgTransform.GetComponent<Image>();
if (image != null)
{
// 缓存找到的组件,下次就不用再查找了
mainBackgroundImage = image;
Debug.Log($"[画报系统] 自动查找到大背景节点: {uiDrawMainPanel.name}/Image_Bg");
return image;
}
}
}
return null;
}
/// <summary>
/// 设置放大镜按钮点击回调
/// </summary>

View File

@ -16,7 +16,13 @@ namespace Framework.UI.Pictorial
public string pictorialName;
/// <summary>
/// 背景图片路径
/// 大背景图片路径UI_DrawMainPanel/Image_Bg的sprite
/// 如果为空则使用原有的sprite
/// </summary>
public string mainBackgroundPath;
/// <summary>
/// 背景图片路径Image_CharacterBg的背景
/// </summary>
public string backgroundPath;
@ -72,6 +78,12 @@ namespace Framework.UI.Pictorial
/// </summary>
public ButtonMagnifierData magnifierButtonData;
/// <summary>
/// 层次顺序(在父节点下的索引,用于控制渲染顺序)
/// 数值越小越在后面渲染Unity的siblingIndex
/// </summary>
public int siblingIndex;
/// <summary>
/// 不同分辨率下的位置信息
/// </summary>

File diff suppressed because it is too large Load Diff

View File

@ -52,12 +52,16 @@ namespace PictorialEditor
private GameObject backgroundImageObject; // 背景图片节点
private RectTransform backgroundRect;
private Image backgroundImage;
private GameObject mainBackgroundObject; // 大背景节点UI_DrawMainPanel/Image_Bg
private Image mainBackgroundImage; // 大背景图片组件
private Sprite originalMainBackgroundSprite; // 原始大背景图片(用于没有配置时恢复)
private GameObject templateItemObject; // 画报元素模板
private List<GameObject> itemObjects = new List<GameObject>();
private Vector2 scrollPos;
private string pictorialName = "NewPictorial";
private string savePath = "Assets/Resources/Pictorial/";
private Sprite selectedMainBackgroundSprite; // 大背景图片Sprite
private Texture2D selectedBackgroundTexture;
private Texture2D selectedItemTexture;
private string selectedCharacterId = ""; // 当前选中的角色ID
@ -72,6 +76,9 @@ namespace PictorialEditor
private const string EDITOR_SCENE_PATH = "Assets/Editor/Pictorial/PictorialEditor.unity";
private const string CANVAS_PATH = "Canvas";
// 注意:大背景路径是相对路径,在编辑器场景中可能不存在
// 运行时会通过FindAncestorByName向上查找UI_DrawMainPanel然后找Image_Bg
private const string MAIN_BACKGROUND_PATH = "Canvas/UI_DrawMainPanel/Image_Bg"; // 大背景路径(可选)
private const string BACKGROUND_PATH = "Canvas/UI_DrawMainPanel/UI_DrawMainPanel/Mid/Image_Right/Image_CharacterBg";
private const string BACKGROUND_IMAGE_PATH = "Canvas/UI_DrawMainPanel/UI_DrawMainPanel/Mid/Image_Right/Image_CharacterBg/Item_ATemp_Bg_Second";
private const string TEMPLATE_ITEM_PATH = "Canvas/UI_DrawMainPanel/UI_DrawMainPanel/Mid/Image_Right/Image_CharacterBg/CharatorItem";
@ -180,6 +187,64 @@ namespace PictorialEditor
InitializeBackgroundObject();
}
/// <summary>
/// 获取Sprite的完整资源路径包括子资源名称
/// </summary>
private string GetSpriteAssetPath(Sprite sprite)
{
if (sprite == null)
return null;
string assetPath = AssetDatabase.GetAssetPath(sprite);
// 如果Sprite是从图集或多个Sprite的图片中获取的需要包含子资源名称
// 格式:"Assets/xxx.png[SpriteName]"
var obj = AssetDatabase.LoadMainAssetAtPath(assetPath);
// 如果主资源不是这个Sprite说明这是一个子资源
if (obj != sprite)
{
return assetPath + "[" + sprite.name + "]";
}
return assetPath;
}
/// <summary>
/// 从资源路径加载Sprite支持子资源格式
/// </summary>
private Sprite LoadSpriteFromPath(string path)
{
if (string.IsNullOrEmpty(path))
return null;
// 检查是否是子资源格式:"Assets/xxx.png[SpriteName]"
if (path.Contains("[") && path.EndsWith("]"))
{
int bracketIndex = path.IndexOf('[');
string assetPath = path.Substring(0, bracketIndex);
string spriteName = path.Substring(bracketIndex + 1, path.Length - bracketIndex - 2);
// 加载所有子资源
var sprites = AssetDatabase.LoadAllAssetsAtPath(assetPath);
foreach (var obj in sprites)
{
if (obj is Sprite sprite && sprite.name == spriteName)
{
return sprite;
}
}
Debug.LogWarning($"[画报编辑器] 未找到子资源Sprite: {spriteName} 在 {assetPath}");
return null;
}
else
{
// 普通资源路径
return AssetDatabase.LoadAssetAtPath<Sprite>(path);
}
}
/// <summary>
/// 初始化背景对象
/// </summary>
@ -210,6 +275,29 @@ namespace PictorialEditor
return;
}
// 查找大背景节点(可选)
GameObject mainBgObj = GameObject.Find(MAIN_BACKGROUND_PATH);
Image mainBgImage = null;
if (mainBgObj != null)
{
mainBgImage = mainBgObj.GetComponent<Image>();
if (mainBgImage == null)
{
Debug.LogWarning($"[画报编辑器] 大背景节点上没有Image组件: {MAIN_BACKGROUND_PATH}");
}
else
{
// 保存原始大背景图片
originalMainBackgroundSprite = mainBgImage.sprite;
Debug.Log($"[画报编辑器] 大背景节点初始化成功");
}
}
else
{
Debug.LogWarning($"[画报编辑器] 未找到大背景节点: {MAIN_BACKGROUND_PATH},大背景功能将不可用");
}
// 清除背景对象下的所有子对象(除了背景图片和模板元素)
List<GameObject> childrenToDelete = new List<GameObject>();
for (int i = 0; i < bgObj.transform.childCount; i++)
@ -237,6 +325,8 @@ namespace PictorialEditor
backgroundImageObject = bgImageObj;
backgroundRect = bgObj.GetComponent<RectTransform>();
backgroundImage = bgImageObj.GetComponent<Image>();
mainBackgroundObject = mainBgObj;
mainBackgroundImage = mainBgImage;
templateItemObject = templateObj;
// 清空之前的画报数据
@ -255,27 +345,68 @@ namespace PictorialEditor
pictorialName = EditorGUILayout.TextField("画报名称", pictorialName);
// 使用Unity内置的Sprite选择器
// 大背景图片选择(可选)
EditorGUILayout.Space(5);
EditorGUILayout.LabelField("大背景设置(可选)", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
Sprite selectedMainBgSprite = EditorGUILayout.ObjectField(
"大背景图片Image_Bg",
selectedMainBackgroundSprite,
typeof(Sprite),
false
) as Sprite;
// 如果大背景发生变化,立即应用
if (EditorGUI.EndChangeCheck())
{
selectedMainBackgroundSprite = selectedMainBgSprite;
ApplyMainBackground();
}
EditorGUILayout.HelpBox(
"如果不设置大背景将使用场景中原有的Image_Bg背景图片。",
MessageType.Info
);
EditorGUILayout.Space(5);
// 画报元素容器背景图片选择
EditorGUILayout.LabelField("画报元素容器背景", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
Sprite selectedSprite = EditorGUILayout.ObjectField(
"选择背景图片",
"背景图片Image_CharacterBg",
selectedBackgroundTexture != null ? AssetDatabase.LoadAssetAtPath<Sprite>(AssetDatabase.GetAssetPath(selectedBackgroundTexture)) : null,
typeof(Sprite),
false
) as Sprite;
// 将Sprite转换为Texture2D
if (selectedSprite != null)
// 如果普通背景发生变化,立即应用
if (EditorGUI.EndChangeCheck())
{
string path = AssetDatabase.GetAssetPath(selectedSprite);
selectedBackgroundTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
}
else if (selectedSprite == null && selectedBackgroundTexture != null)
{
selectedBackgroundTexture = null;
// 将Sprite转换为Texture2D
if (selectedSprite != null)
{
string path = AssetDatabase.GetAssetPath(selectedSprite);
selectedBackgroundTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
}
else
{
selectedBackgroundTexture = null;
}
ApplyBackground();
}
EditorGUILayout.Space(10);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("新建画报", GUILayout.Height(40)))
{
NewPictorial();
}
if (GUILayout.Button("创建画报背景", GUILayout.Height(40)))
{
CreatePictorial();
@ -295,27 +426,193 @@ namespace PictorialEditor
private void DrawPictorialManagement()
{
EditorGUILayout.LabelField("2. 画报元素管理", EditorStyles.boldLabel);
EditorGUILayout.LabelField($"当前画报: {pictorialData.pictorialName}");
EditorGUILayout.LabelField($"元素数量: {pictorialData.items.Count}");
EditorGUILayout.LabelField($"分辨率配置: {pictorialData.resolutionConfigs.Count}");
// 显示提示信息
EditorGUILayout.HelpBox(
"层次顺序说明:\n" +
"- 在Hierarchy视图中可以直接拖拽元素调整渲染顺序\n" +
"- 调整后,点击‘保存当前分辨率数据’即可保存层次关系\n" +
"- 层次关系会同步应用到所有分辨率配置",
MessageType.Info
);
EditorGUILayout.Space();
// 添加画报元素
DrawAddItemSection();
EditorGUILayout.Space();
// 分辨率配置
DrawResolutionSection();
EditorGUILayout.Space();
// 保存/导出
DrawSaveSection();
}
/// <summary>
/// 新建画报(清空当前状态,进入新画报编辑流程)
/// </summary>
private void NewPictorial()
{
if (pictorialData != null)
{
bool confirm = EditorUtility.DisplayDialog(
"确认新建画报",
"当前已有画报数据,新建画报将清空所有元素。\n\n是否继续",
"确定",
"取消"
);
if (!confirm)
return;
}
// 检查背景对象是否存在
if (backgroundObject == null)
{
EditorUtility.DisplayDialog("错误", "请先打开画报编辑器场景", "确定");
LoadPictorialEditorScene();
return;
}
// 清理背景对象下的所有子对象(清除所有元素)
List<GameObject> childrenToDelete = new List<GameObject>();
for (int i = 0; i < backgroundObject.transform.childCount; i++)
{
GameObject child = backgroundObject.transform.GetChild(i).gameObject;
// 保留背景图片和模板元素
if (child != backgroundImageObject && child != templateItemObject)
{
childrenToDelete.Add(child);
}
}
foreach (var child in childrenToDelete)
{
DestroyImmediate(child);
}
itemObjects.Clear();
// 重置画报数据
pictorialData = null;
selectedMainBackgroundSprite = null;
selectedBackgroundTexture = null;
pictorialName = "NewPictorial";
// 恢复原始背景
if (mainBackgroundImage != null && originalMainBackgroundSprite != null)
{
mainBackgroundImage.sprite = originalMainBackgroundSprite;
}
if (backgroundImage != null)
{
backgroundImage.sprite = null;
}
Debug.Log($"[画报编辑器] 已清空画报数据,进入新建画报流程");
EditorUtility.DisplayDialog("成功", "已清空画报数据,可以开始新建画报。", "确定");
}
/// <summary>
/// 应用大背景设置
/// </summary>
private void ApplyMainBackground()
{
if (mainBackgroundImage == null)
{
Debug.LogWarning($"[画报编辑器] 大背景 Image 组件为空,无法应用大背景");
return;
}
if (selectedMainBackgroundSprite != null)
{
mainBackgroundImage.sprite = selectedMainBackgroundSprite;
Debug.Log($"[画报编辑器] 大背景已应用: {selectedMainBackgroundSprite.name}");
// 如果已有画报数据,同步更新到数据中
if (pictorialData != null)
{
pictorialData.mainBackgroundPath = GetSpriteAssetPath(selectedMainBackgroundSprite);
Debug.Log($"[画报编辑器] 已同步大背景路径到画报数据: {pictorialData.mainBackgroundPath}");
}
}
else
{
// 恢复为原始背景
if (originalMainBackgroundSprite != null)
{
mainBackgroundImage.sprite = originalMainBackgroundSprite;
Debug.Log($"[画报编辑器] 恢复为原始大背景");
// 如果已有画报数据,清空大背景路径
if (pictorialData != null)
{
pictorialData.mainBackgroundPath = null;
}
}
}
SceneView.RepaintAll();
}
/// <summary>
/// 应用普通背景设置
/// </summary>
private void ApplyBackground()
{
if (backgroundImage == null)
{
Debug.LogWarning($"[画报编辑器] 背景 Image 组件为空,无法应用背景");
return;
}
if (selectedBackgroundTexture != null)
{
string spritePath = AssetDatabase.GetAssetPath(selectedBackgroundTexture);
Sprite backgroundSprite = AssetDatabase.LoadAssetAtPath<Sprite>(spritePath);
if (backgroundSprite != null)
{
backgroundImage.sprite = backgroundSprite;
Debug.Log($"[画报编辑器] 普通背景已应用: {backgroundSprite.name}");
// 如果已有画报数据,同步更新到数据中
if (pictorialData != null)
{
pictorialData.backgroundPath = spritePath;
pictorialData.backgroundSize = new Vector2(selectedBackgroundTexture.width, selectedBackgroundTexture.height);
Debug.Log($"[画报编辑器] 已同步普通背景路径到画报数据: {pictorialData.backgroundPath}");
}
}
else
{
Debug.LogError($"[画报编辑器] 无法加载背景图片请确保图片是Sprite类型: {spritePath}");
}
}
else
{
backgroundImage.sprite = null;
Debug.Log($"[画报编辑器] 清空普通背景");
// 如果已有画报数据,清空背景路径
if (pictorialData != null)
{
pictorialData.backgroundPath = null;
}
}
SceneView.RepaintAll();
}
/// <summary>
/// 添加元素区域
/// </summary>
@ -460,6 +757,24 @@ namespace PictorialEditor
}
}
/// <summary>
/// 强制刷新Canvas布局
/// </summary>
private void ForceRefreshCanvasLayout()
{
if (backgroundRect == null)
return;
// 强制刷新Canvas布局
Canvas canvas = backgroundRect.GetComponentInParent<Canvas>();
if (canvas != null)
{
Canvas.ForceUpdateCanvases();
UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(backgroundRect);
Debug.Log($"[画报编辑器] 强制刷新Canvas布局完成");
}
}
/// <summary>
/// 检查分辨率变化并自动恢复元素位置
/// </summary>
@ -484,14 +799,54 @@ namespace PictorialEditor
{
Debug.Log($"[画报编辑器] 检测到屏幕分辨率变化: {lastScreenResolution.x}x{lastScreenResolution.y} -> {currentScreenResolution.x}x{currentScreenResolution.y}");
// 强制刷新Canvas布局确保背景尺寸更新
ForceRefreshCanvasLayout();
// 重新获取刷新后的背景尺寸
currentBackgroundSize = backgroundRect.rect.size;
// 查找是否有匹配的配置
var matchedConfig = FindMatchingResolutionConfig(currentScreenResolution.x, currentScreenResolution.y);
if (matchedConfig != null)
{
Debug.Log($"[画报编辑器] 匹配到已保存的分辨率配置: 屏幕={matchedConfig.width}x{matchedConfig.height}, 背景={matchedConfig.backgroundWidth}x{matchedConfig.backgroundHeight}");
// 分辨率变化后,强制在接下来的几帧中频繁检查背景尺寸
forceCheck = true;
// 检查背景尺寸是否匹配配置中的背景尺寸
bool backgroundMatches = Mathf.Approximately(currentBackgroundSize.x, matchedConfig.backgroundWidth) &&
Mathf.Approximately(currentBackgroundSize.y, matchedConfig.backgroundHeight);
if (backgroundMatches)
{
Debug.Log($"[画报编辑器] 背景尺寸已匹配,立即恢复元素位置");
// 背景尺寸已经匹配,直接恢复位置
RestoreItemsPositionInEditor((int)matchedConfig.backgroundWidth, (int)matchedConfig.backgroundHeight);
// 更新最后一次的背景尺寸
lastBackgroundSize = currentBackgroundSize;
// 刷新Scene视图和Game视图
SceneView.RepaintAll();
Repaint();
}
else
{
Debug.Log($"[画报编辑器] 背景尺寸未匹配,等待背景尺寸更新。当前背景={currentBackgroundSize.x}x{currentBackgroundSize.y}, 目标背景={matchedConfig.backgroundWidth}x{matchedConfig.backgroundHeight}");
// 分辨率变化后,强制在接下来的几帧中频繁检查背景尺寸
forceCheck = true;
}
}
else
{
Debug.Log($"[画报编辑器] 未找到匹配的分辨率配置,使用插值计算位置");
// 没有匹配的配置,使用插值
UpdateAllItemsPositionInEditor();
// 更新最后一次的背景尺寸
lastBackgroundSize = currentBackgroundSize;
// 刷新Scene视图和Game视图
SceneView.RepaintAll();
Repaint();
}
}
@ -571,8 +926,8 @@ namespace PictorialEditor
if (itemObj == null)
continue;
// 查找匹配的位置数据
var posData = itemData.positions.Find(p =>
// 查找匹配的位置数据使用FindLast获取最新的数据
var posData = itemData.positions.FindLast(p =>
Mathf.Approximately(p.resolutionWidth, bgWidth) &&
Mathf.Approximately(p.resolutionHeight, bgHeight));
@ -842,15 +1197,39 @@ namespace PictorialEditor
itemObjects.Clear();
// 创建新的画报数据
string mainBgPath = GetSpriteAssetPath(selectedMainBackgroundSprite);
pictorialData = new PictorialData
{
pictorialName = pictorialName,
mainBackgroundPath = mainBgPath,
backgroundPath = AssetDatabase.GetAssetPath(selectedBackgroundTexture),
backgroundSize = new Vector2(selectedBackgroundTexture.width, selectedBackgroundTexture.height),
items = new List<PictorialItemData>(),
resolutionConfigs = new List<ResolutionConfig>()
};
Debug.Log($"[画报编辑器] 保存的大背景路径: {(string.IsNullOrEmpty(mainBgPath) ? "" : mainBgPath)}");
// 设置大背景图片
if (mainBackgroundImage != null)
{
if (selectedMainBackgroundSprite != null)
{
mainBackgroundImage.sprite = selectedMainBackgroundSprite;
Debug.Log($"[画报编辑器] 大背景图片设置成功: {selectedMainBackgroundSprite.name}, 路径: {mainBgPath}");
}
else
{
// 没有设置大背景,恢复为原始背景
mainBackgroundImage.sprite = originalMainBackgroundSprite;
Debug.Log($"[画报编辑器] 未设置大背景,使用原始背景图片");
}
}
else
{
Debug.LogWarning($"[画报编辑器] 大背景 Image 组件为空,跳过大背景设置");
}
// 设置背景图片
string spritePath = AssetDatabase.GetAssetPath(selectedBackgroundTexture);
Sprite backgroundSprite = AssetDatabase.LoadAssetAtPath<Sprite>(spritePath);
@ -943,28 +1322,82 @@ namespace PictorialEditor
}
itemObjects.Clear();
// 加载背景图片
if (!string.IsNullOrEmpty(pictorialData.backgroundPath))
// 加载背景图片
if (mainBackgroundImage != null)
{
Sprite backgroundSprite = AssetDatabase.LoadAssetAtPath<Sprite>(pictorialData.backgroundPath);
if (backgroundSprite != null)
if (!string.IsNullOrEmpty(pictorialData.mainBackgroundPath))
{
backgroundImage.sprite = backgroundSprite;
Debug.Log($"[画报编辑器] 背景图片加载成功: {pictorialData.backgroundPath}");
Sprite mainBgSprite = LoadSpriteFromPath(pictorialData.mainBackgroundPath);
if (mainBgSprite != null)
{
mainBackgroundImage.sprite = mainBgSprite;
// 同步更新编辑器变量,以便用户可以再次创建画报
selectedMainBackgroundSprite = mainBgSprite;
Debug.Log($"[画报编辑器] 大背景图片加载成功: {pictorialData.mainBackgroundPath}");
}
else
{
mainBackgroundImage.sprite = originalMainBackgroundSprite;
selectedMainBackgroundSprite = null;
Debug.LogWarning($"[画报编辑器] 无法加载大背景图片,使用原始背景: {pictorialData.mainBackgroundPath}");
}
}
else
{
backgroundImage.sprite = null;
Debug.LogWarning($"[画报编辑器] 无法加载背景图片: {pictorialData.backgroundPath}");
// 没有设置大背景,恢复为原始背景
mainBackgroundImage.sprite = originalMainBackgroundSprite;
selectedMainBackgroundSprite = null;
Debug.Log($"[画报编辑器] 未配置大背景,使用原始背景图片");
}
}
else
{
Debug.LogWarning($"[画报编辑器] 大背景 Image 组件为空,跳过大背景设置");
selectedMainBackgroundSprite = null;
}
// 恢复所有元素
// 加载背景图片
if (backgroundImage != null)
{
if (!string.IsNullOrEmpty(pictorialData.backgroundPath))
{
Sprite backgroundSprite = AssetDatabase.LoadAssetAtPath<Sprite>(pictorialData.backgroundPath);
if (backgroundSprite != null)
{
backgroundImage.sprite = backgroundSprite;
// 同步更新编辑器变量,以便用户可以再次创建画报
selectedBackgroundTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(pictorialData.backgroundPath);
Debug.Log($"[画报编辑器] 背景图片加载成功: {pictorialData.backgroundPath}");
}
else
{
backgroundImage.sprite = null;
selectedBackgroundTexture = null;
Debug.LogWarning($"[画报编辑器] 无法加载背景图片: {pictorialData.backgroundPath}");
}
}
else
{
selectedBackgroundTexture = null;
}
}
else
{
Debug.LogWarning($"[画报编辑器] 背景 Image 组件为空,跳过背景设置");
selectedBackgroundTexture = null;
}
// 恢复所有元素按照siblingIndex顺序加载确保层次关系正确
int successCount = 0;
int failCount = 0;
foreach (var itemData in pictorialData.items)
// 先按siblingIndex排序确保加载顺序正确
var sortedItems = pictorialData.items.OrderBy(item => item.siblingIndex).ToList();
Debug.Log($"[画报编辑器] 开始按层次顺序恢复元素,总数: {sortedItems.Count}");
foreach (var itemData in sortedItems)
{
if (RestorePictorialItem(itemData))
{
@ -1120,6 +1553,10 @@ namespace PictorialEditor
var itemComponent = itemObj.AddComponent<PictorialItemIdentifier>();
itemComponent.itemId = itemData.itemId;
// 设置层次顺序(在父节点下的位置)
itemObj.transform.SetSiblingIndex(itemData.siblingIndex);
Debug.Log($"[画报编辑器] 设置元素层次 [{itemData.itemName}]: siblingIndex={itemData.siblingIndex}");
itemObjects.Add(itemObj);
return true;
@ -1212,6 +1649,11 @@ namespace PictorialEditor
var itemComponent = itemObj.AddComponent<PictorialItemIdentifier>();
itemComponent.itemId = itemId;
// 初始化siblingIndex新元素默认位于最后
int currentSiblingIndex = itemObj.transform.GetSiblingIndex();
itemData.siblingIndex = currentSiblingIndex;
Debug.Log($"[画报编辑器] 初始化元素层次 [{itemName}]: siblingIndex={currentSiblingIndex}");
itemObjects.Add(itemObj);
// 选中新创建的对象
@ -1271,6 +1713,32 @@ namespace PictorialEditor
// 保存所有元素在当前背景尺寸下的位置
int savedCount = 0;
// 第一步根据当前场景中的顺序更新所有元素的siblingIndex
Debug.Log($"[画报编辑器] 开始更新元素层次顺序");
foreach (var itemData in pictorialData.items)
{
// 查找对应的GameObject
GameObject itemObj = itemObjects.FirstOrDefault(obj =>
{
var identifier = obj.GetComponent<PictorialItemIdentifier>();
return identifier != null && identifier.itemId == itemData.itemId;
});
if (itemObj != null)
{
// 获取当前在父节点下的层次顺序
int currentSiblingIndex = itemObj.transform.GetSiblingIndex();
itemData.siblingIndex = currentSiblingIndex;
Debug.Log($"[画报编辑器] 更新元素层次 [{itemData.itemName}]: siblingIndex={currentSiblingIndex}");
}
}
// 第二步按照siblingIndex对pictorialData.items进行排序确保数据顺序与场景一致
pictorialData.items = pictorialData.items.OrderBy(item => item.siblingIndex).ToList();
Debug.Log($"[画报编辑器] 已按层次顺序重新排列元素数据");
// 第三步:保存位置数据
foreach (var itemData in pictorialData.items)
{
// 查找对应的GameObject
@ -1288,10 +1756,22 @@ namespace PictorialEditor
RectTransform itemRect = itemObj.GetComponent<RectTransform>();
// 检查是否已存在该背景尺寸的位置数据
var existingPos = itemData.positions
.FirstOrDefault(p => Mathf.Approximately(p.resolutionWidth, bgWidth) && Mathf.Approximately(p.resolutionHeight, bgHeight));
// 检查是否已存在该背景尺寸的位置数据(清理所有重复的,只保留最新的)
var existingPositions = itemData.positions
.Where(p => Mathf.Approximately(p.resolutionWidth, bgWidth) && Mathf.Approximately(p.resolutionHeight, bgHeight))
.ToList();
if (existingPositions.Count > 0)
{
// 删除所有匹配的旧数据
foreach (var oldPos in existingPositions)
{
itemData.positions.Remove(oldPos);
}
Debug.Log($"[画报编辑器] 清理了 {existingPositions.Count} 个重复的分辨率数据 [{itemData.itemName}]: {(int)bgWidth}x{(int)bgHeight}");
}
// 添加新的位置数据
ItemPositionData posData = new ItemPositionData
{
resolutionWidth = (int)bgWidth,
@ -1302,12 +1782,6 @@ namespace PictorialEditor
scale = itemRect.localScale
};
if (existingPos != null)
{
// 更新现有数据
itemData.positions.Remove(existingPos);
}
itemData.positions.Add(posData);
// 保存ButtonMagnifiers按钮位置如果有characterId
@ -1338,7 +1812,7 @@ namespace PictorialEditor
EditorUtility.DisplayDialog(
"保存成功",
$"已保存分辨率数据\n屏幕分辨率: {screenWidth}x{screenHeight}\n背景尺寸: {(int)bgWidth}x{(int)bgHeight}\n元素数量: {savedCount}",
$"已保存分辨率数据\n屏幕分辨率: {screenWidth}x{screenHeight}\n背景尺寸: {(int)bgWidth}x{(int)bgHeight}\n元素数量: {savedCount}\n\n层次关系已同步更新",
"确定"
);