com.soviby.unity.ui.ugui-to.../Assets/Editor/Manager/EntityManager.cs

702 lines
28 KiB
C#
Raw Normal View History

2024-10-23 17:30:59 +00:00
#if UNITY_EDITOR
2024-10-29 17:51:06 +00:00
using Sirenix.OdinInspector;
2024-12-03 05:05:13 +00:00
using System;
2024-10-28 16:31:38 +00:00
using System.Collections.Generic;
2024-12-02 17:47:50 +00:00
using System.Globalization;
using System.IO;
2024-12-03 05:05:13 +00:00
using TMPro;
2024-10-29 17:51:06 +00:00
using UnityEditor;
using UnityEditor.SceneManagement;
2024-10-23 17:30:59 +00:00
using UnityEngine;
2024-12-02 17:47:50 +00:00
using UnityEngine.UI;
2024-12-06 10:46:00 +00:00
using UguiToolkit.Editor.Windows;
2024-12-09 18:39:04 +00:00
using System.Threading.Tasks;
using Unity.Mathematics;
2024-12-17 03:07:00 +00:00
using Securify.ShellLink.Flags;
2024-10-23 17:30:59 +00:00
namespace UguiToolkit.Editor
{
2024-10-28 16:31:38 +00:00
[ExecuteAlways]
2024-10-23 17:30:59 +00:00
public class EntityManager : MonoBehaviour, IManager
{
2024-12-17 03:07:00 +00:00
private const string m_backgroundFileName= "__background__";
private string m_BackgroundFileAllName= m_backgroundFileName + ".png";
2024-10-23 17:30:59 +00:00
private PanelCache m_panelCache;
private Transform m_entityRoot;
2024-12-02 17:47:50 +00:00
private Transform m_background;
2024-12-17 03:07:00 +00:00
private GameObject m_lastApplyDataGo; // 上一次应用的游戏对象
private IEntity m_lastApplyDataEntity; // 上一次应用的entity
private GameObject m_curSelectionEntityGo; // 当前选中的entity游戏对象
private GameObject m_curSelectionGo; // 当前选中的游戏对象
private SelectionObjCache m_curSelectionObjCache = new (); // 当前选中的游戏对象的缓存数据
private Color m_curSelectionObjLabelColor = Color.yellow;
2024-10-23 17:30:59 +00:00
private List<ImageEntity> m_imageEntities;
2024-12-01 16:33:27 +00:00
private List<IEntity> m_textEntities;
2024-12-06 10:46:00 +00:00
private List<PrefabEntity> m_prefabEntities;
private List<IEntity> m_selectionEntities;
2024-10-28 16:31:38 +00:00
2024-12-06 10:46:00 +00:00
[LabelText("脱离选择控制"), ShowInInspector, SerializeField]
private bool m_noSelection;
private StageManager m_stageManager;
2024-12-06 10:46:00 +00:00
2024-11-28 10:34:17 +00:00
private bool m_useTMP;
2024-12-06 10:46:00 +00:00
private EditWindow editWindow;
2024-12-10 10:14:38 +00:00
private bool isCalcRotationScaleRunning = false;
2024-12-06 10:46:00 +00:00
2024-12-09 18:39:04 +00:00
private List<OpenCVForUnity.CoreModule.Mat> targetImages;
private List<string> targetPaths;
2024-12-06 10:46:00 +00:00
public Transform Background => m_background;
2024-10-29 17:51:06 +00:00
private void OnEnable()
2024-10-28 16:31:38 +00:00
{
2024-11-28 10:34:17 +00:00
var globalMng = GlobalManager.Instance;
m_useTMP = globalMng.setting.useTMP;
2024-10-29 17:51:06 +00:00
Selection.selectionChanged += OnSelectionChanged;
m_stageManager = StageManager.Instance;
2024-12-06 10:46:00 +00:00
editWindow = EditWindow.Window;
if (editWindow != null)
{
editWindow.showHierarchyOfEntityChanged += OnUpdateHierarchyOfEntityAllEntity;
editWindow.showBackgroundChanged += OnUpdateBackgroundShow;
editWindow.createAllTextEntity += CreateAllTextEntity;
2024-12-10 10:14:38 +00:00
editWindow.createAllPrefabEntity += CreateAllPrefabEntity;
editWindow.effectLastApplyTransform += EffectLastApplyTransform;
2024-12-17 03:07:00 +00:00
editWindow.recoverDataSelectionObjCache += OnRecoverDataSelectionObjCache;
2024-12-06 10:46:00 +00:00
}
2024-12-10 10:14:38 +00:00
EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
2024-12-17 03:07:00 +00:00
PanelHelper.jumpOfGameCameraByCurPos += EffectLastApplyTransform;
PanelHelper.recoverDataSelectionObjCache += RecoverDataSelectionObjCache;
2024-12-10 10:14:38 +00:00
ImageUtils.SetDebugMode(true);
2024-10-29 17:51:06 +00:00
}
private void OnDisable()
{
Selection.selectionChanged -= OnSelectionChanged;
2024-12-06 10:46:00 +00:00
if (editWindow != null)
{
editWindow.showHierarchyOfEntityChanged -= OnUpdateHierarchyOfEntityAllEntity;
editWindow.showBackgroundChanged -= OnUpdateBackgroundShow;
editWindow.createAllTextEntity -= CreateAllTextEntity;
2024-12-10 10:14:38 +00:00
editWindow.createAllPrefabEntity -= CreateAllPrefabEntity;
editWindow.effectLastApplyTransform -= EffectLastApplyTransform;
2024-12-17 03:07:00 +00:00
editWindow.recoverDataSelectionObjCache -= OnRecoverDataSelectionObjCache;
2024-12-06 10:46:00 +00:00
}
2024-12-10 10:14:38 +00:00
EditorApplication.hierarchyWindowItemOnGUI -= HandleHierarchyWindowItemOnGUI;
2024-12-17 03:07:00 +00:00
PanelHelper.jumpOfGameCameraByCurPos -= EffectLastApplyTransform;
PanelHelper.recoverDataSelectionObjCache -= RecoverDataSelectionObjCache;
2024-12-10 10:14:38 +00:00
ImageUtils.SetDebugMode(false);
if (m_stageManager.PrefabAsset || m_panelCache != null)
GlobalManager.Instance.SaveCache(m_stageManager.PrefabAsset, m_panelCache);
2024-10-29 17:51:06 +00:00
}
private void Update()
{
// 检测是否到达可选实例矩形内部
2024-12-17 03:07:00 +00:00
if (m_selectionEntities != null && m_curSelectionEntityGo)
2024-10-29 17:51:06 +00:00
{
2024-12-17 03:07:00 +00:00
if (m_lastApplyDataGo && m_lastApplyDataGo == m_curSelectionEntityGo)
2024-10-29 17:51:06 +00:00
{
2024-12-17 03:07:00 +00:00
if (m_lastApplyDataEntity != null && !m_lastApplyDataEntity.IsInside(m_lastApplyDataGo.transform))
2024-10-29 17:51:06 +00:00
{
2024-12-17 03:07:00 +00:00
m_lastApplyDataGo = null;
m_lastApplyDataEntity = null;
2024-10-29 17:51:06 +00:00
}
return;
}
foreach (var entity in m_selectionEntities)
2024-10-29 17:51:06 +00:00
{
2024-12-17 03:07:00 +00:00
var tf = m_curSelectionEntityGo.transform;
2024-10-29 17:51:06 +00:00
if (entity.IsInside(tf))
{
entity.ApplyTransform(tf);
2024-12-06 10:46:00 +00:00
if (PrefabEntity.IsPrefab(tf.gameObject))
{
2024-12-10 10:14:38 +00:00
entity.ApplyData(tf);
2024-12-06 10:46:00 +00:00
}
2024-12-09 11:18:10 +00:00
else if (tf.TryGetComponent<UnityEngine.UI.Image>(out var image))
2024-11-06 17:45:57 +00:00
{
entity.ApplyData(image);
}
else if (tf.TryGetComponent<UnityEngine.UI.Text>(out var text))
{
entity.ApplyData(text);
}
2024-12-02 14:14:39 +00:00
else if (tf.TryGetComponent<TMPro.TextMeshProUGUI>(out var temp))
{
entity.ApplyData(temp);
}
2024-10-29 17:51:06 +00:00
2024-12-17 03:07:00 +00:00
m_lastApplyDataGo = m_curSelectionEntityGo;
m_lastApplyDataEntity = entity;
2024-10-29 17:51:06 +00:00
Selection.activeGameObject = null;
break;
}
}
}
2024-12-06 10:46:00 +00:00
}
2024-12-17 03:07:00 +00:00
private void UpdateSelectionObjLabelColor()
{
m_curSelectionObjLabelColor = m_curSelectionObjLabelColor == Color.yellow ? Color.blue : Color.yellow;
}
private void OnRecoverDataSelectionObjCache()
{
if (m_curSelectionObjCache.Transform)
m_curSelectionObjCache.RecoverData();
}
private void RecoverDataSelectionObjCache(Transform parent)
{
if (m_curSelectionObjCache.Transform && m_curSelectionObjCache.Transform == parent)
{
m_curSelectionObjCache.RecoverData();
}
}
2024-12-10 10:14:38 +00:00
public void EffectLastApplyTransform(Transform parent)
2024-12-06 10:46:00 +00:00
{
2024-12-17 03:07:00 +00:00
if (m_lastApplyDataEntity != null)
2024-12-06 10:46:00 +00:00
{
2024-12-17 03:07:00 +00:00
m_lastApplyDataEntity.ApplyTransformByParent(parent, m_lastApplyDataGo ? m_lastApplyDataGo.transform: null);
if (m_curSelectionObjCache.Transform)
{
m_curSelectionObjCache.SetData(m_curSelectionObjCache.Transform);
UpdateSelectionObjLabelColor();
}
2024-12-06 10:46:00 +00:00
}
}
2024-12-10 10:14:38 +00:00
private void HandleHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
2024-12-06 10:46:00 +00:00
{
2024-12-10 10:14:38 +00:00
if (!m_stageManager) return;
var go = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (!go) return;
2024-12-17 03:07:00 +00:00
var goLabel = new GUIContent(go.name);
var goSize = EditorStyles.linkLabel.CalcSize(goLabel) + new Vector2(20, 0);
if (go == m_lastApplyDataGo)
2024-12-06 10:46:00 +00:00
{
2024-12-17 03:07:00 +00:00
var label = new GUIContent(" <---------锚点");
2024-12-10 10:14:38 +00:00
var size = EditorStyles.linkLabel.CalcSize(label) + new Vector2(10, 0);
var offsetRect =
new Rect(selectionRect.position + new Vector2(goSize.x, 0), size);
EditorGUI.LabelField(offsetRect, label, new GUIStyle()
2024-12-06 10:46:00 +00:00
{
2024-12-10 10:14:38 +00:00
normal = new GUIStyleState() { textColor = Color.green },
});
2024-10-29 17:51:06 +00:00
}
2024-12-17 03:07:00 +00:00
if (go.transform == m_curSelectionObjCache.Transform)
{
var label = new GUIContent(" <---------复原子节点");
var size = EditorStyles.linkLabel.CalcSize(label) + new Vector2(10, 0);
var offsetRect =
new Rect(selectionRect.position + new Vector2(goSize.x, 0), size);
EditorGUI.LabelField(offsetRect, label, new GUIStyle()
{
normal = new GUIStyleState() { textColor = m_curSelectionObjLabelColor },
});
}
}
[Button("TestSliceTexture")]
private void TestSliceTexture(string imagePath, string outputPath)
{
ImageUtils.SliceTexture(imagePath, outputPath);
2024-10-28 16:31:38 +00:00
}
2024-12-03 05:05:13 +00:00
private void CreateAllTextEntity()
{
var root = m_stageManager.PrefabContentsRoot;
var textsTf = root.transform.Find("__texts__");
if (textsTf) DestroyImmediate(textsTf.gameObject);
if (m_textEntities == null) return;
var textsGo = new GameObject("__texts__", typeof(RectTransform));
textsGo.transform.parent = root.transform;
Type textType = m_useTMP ? typeof(TextMeshProUGUI) : typeof(Text);
foreach (var textEntity in m_textEntities)
{
var newText = new GameObject(textEntity.gameObject.name, typeof(RectTransform), textType);
newText.transform.parent = textsGo.transform;
textEntity.ApplyData(newText.GetComponent(textType));
textEntity.ApplyTransform(newText.transform);
}
}
2024-12-10 10:14:38 +00:00
private void CreateAllPrefabEntity()
{
var root = m_stageManager.PrefabContentsRoot;
var prefabsTf = root.transform.Find("__prefabs__");
if (prefabsTf) DestroyImmediate(prefabsTf.gameObject);
if (m_textEntities == null) return;
var prefabsGo = new GameObject("__prefabs__", typeof(RectTransform));
prefabsGo.transform.parent = root.transform;
foreach (var prefabEntity in m_prefabEntities)
{
var asset = prefabEntity.GetPrefabAsset(PrefabEntity.GetCommonDirPath());
if (asset != null)
{
var go = GameObject.Instantiate(asset, prefabsGo.transform);
go.name = asset.name;
prefabEntity.ApplyData(go.transform);
}
}
}
2024-12-02 17:47:50 +00:00
private void InitBackground()
{
if (m_background) DestroyImmediate(m_background.gameObject);
2024-12-17 03:07:00 +00:00
var go = new GameObject(m_backgroundFileName, typeof(RectTransform));
2024-12-02 17:47:50 +00:00
UpdateHierarchyOfEntity(false, go);
m_background = go.transform;
m_background.SetParent(transform);
m_background.localPosition = Vector3.zero;
m_background.localRotation = Quaternion.identity;
m_background.localScale = Vector3.one;
var canvas = m_background.gameObject.AddComponent<Canvas>();
canvas.pixelPerfect = true;
canvas.overrideSorting = true;
canvas.sortingOrder = -100;
var imgGo = new GameObject("_", typeof(RectTransform));
var imgTf = imgGo.transform;
imgTf.SetParent(m_background);
imgTf.localPosition = Vector3.zero;
imgTf.localRotation = Quaternion.identity;
imgTf.localScale = Vector3.one;
var img = imgGo.AddComponent<Image>();
2024-12-17 03:07:00 +00:00
var imgPath = System.IO.Path.Join(m_panelCache.TargetImgDirPath, m_BackgroundFileAllName);
2024-12-02 17:47:50 +00:00
if (System.IO.File.Exists(imgPath))
{
byte[] fileData = System.IO.File.ReadAllBytes(imgPath);
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(fileData); // 加载图片数据到Texture2D
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
img.sprite = sprite;
2024-12-06 10:46:00 +00:00
img.color = new Color(1, 1, 1, 0.2f);
2024-12-02 17:47:50 +00:00
img.SetNativeSize();
}
2024-12-06 10:46:00 +00:00
UpdateHierarchyOfEntity(false, imgGo);
}
2024-12-17 03:07:00 +00:00
private void UpdatePanelCache(string srcImgPath, bool isSliceTexture = false)
2024-12-09 18:39:04 +00:00
{
float distanceDifference = GlobalManager.Instance.setting.distanceDifference;
2024-12-10 10:14:38 +00:00
isCalcRotationScaleRunning = true;
2024-12-09 18:39:04 +00:00
List<RotScaleInfoItem> rotScaleItemList = new();
2024-12-10 10:14:38 +00:00
Debug.Log("GetRotationScaleAsync: " + srcImgPath);
_ = ImageUtils.ProcessFolderAsync(targetImages, srcImgPath, distanceDifference, (index, result) =>
2024-12-09 18:39:04 +00:00
{
if (!m_stageManager) return;
rotScaleItemList.Add(new()
{
imgPath = ImageUtils.FormatImgFilePath(targetPaths[index]),
rotiation = (float)result.Item1,
scale = new float2((float)result.Item2.Item1, (float)result.Item2.Item1),
2024-12-10 10:14:38 +00:00
similarityCalc = result.Item3
2024-12-09 18:39:04 +00:00
});
}, () =>
{
2024-12-10 10:14:38 +00:00
Debug.Log("End GetRotationScaleAsync: " + srcImgPath);
isCalcRotationScaleRunning = false;
2024-12-09 18:39:04 +00:00
if (!m_stageManager) return;
m_panelCache.AddRotScaleInfo(srcImgPath, rotScaleItemList);
if (rotScaleItemList.Count > 0)
OnSelectionChanged();
2024-12-17 03:07:00 +00:00
}, isSliceTexture: isSliceTexture);
2024-12-09 18:39:04 +00:00
}
2024-10-29 17:51:06 +00:00
private void OnSelectionChanged()
2024-10-28 16:31:38 +00:00
{
2024-12-06 10:46:00 +00:00
if (m_noSelection || !Selection.activeGameObject || Selection.gameObjects.Length > 1) return;
2024-12-17 03:07:00 +00:00
if (Selection.activeGameObject == m_curSelectionEntityGo || !Selection.activeGameObject.activeSelf) return;
2024-12-02 14:14:39 +00:00
2024-12-06 10:46:00 +00:00
var activeGameObject = Selection.activeGameObject;
if (activeGameObject.transform.parent == m_entityRoot || activeGameObject.transform.parent == m_background)
return;
2024-12-17 03:07:00 +00:00
m_curSelectionGo = activeGameObject;
m_curSelectionObjCache.SetData(activeGameObject.transform);
UpdateSelectionObjLabelColor();
m_curSelectionEntityGo = null;
m_selectionEntities.Clear();
2024-11-06 17:45:57 +00:00
m_entityRoot.gameObject.SetActive(false);
2024-10-29 17:51:06 +00:00
2024-12-06 10:46:00 +00:00
2024-10-29 17:51:06 +00:00
if (Selection.activeGameObject != null && m_panelCache != null)
{
2024-12-06 10:46:00 +00:00
if (PrefabEntity.IsCommonPrefab(activeGameObject))
2024-10-29 17:51:06 +00:00
{
2024-11-06 17:45:57 +00:00
bool IsInside = false;
2024-12-06 10:46:00 +00:00
m_entityRoot.gameObject.SetActive(true);
foreach (var prefabEntity in m_prefabEntities)
2024-10-29 17:51:06 +00:00
{
2024-12-06 10:46:00 +00:00
prefabEntity.ShowSelectionImg(true);
prefabEntity.gameObject.SetActive(true);
2024-12-10 10:14:38 +00:00
prefabEntity.SetOriginalMatrix(activeGameObject.transform);
2024-10-29 17:51:06 +00:00
2024-12-06 10:46:00 +00:00
m_selectionEntities.Add(prefabEntity);
2024-11-06 17:45:57 +00:00
2024-12-06 10:46:00 +00:00
if (!IsInside && prefabEntity.IsInside(activeGameObject.transform)) IsInside = true;
2024-10-29 17:51:06 +00:00
}
2024-11-06 17:45:57 +00:00
if (IsInside)
{
2024-12-17 03:07:00 +00:00
if (m_lastApplyDataGo && m_lastApplyDataGo == activeGameObject)
2024-11-06 17:45:57 +00:00
{
2024-12-17 03:07:00 +00:00
m_curSelectionEntityGo = activeGameObject;
2024-11-06 17:45:57 +00:00
}
}
2024-12-06 10:46:00 +00:00
else
{
2024-12-17 03:07:00 +00:00
m_curSelectionEntityGo = activeGameObject;
2024-11-06 17:45:57 +00:00
}
2024-12-06 10:46:00 +00:00
foreach (var imgEntity in m_imageEntities)
{
imgEntity.gameObject.SetActive(false);
}
foreach (var textEntity in m_textEntities)
2024-10-29 17:51:06 +00:00
{
textEntity.gameObject.SetActive(false);
}
}
2024-12-06 10:46:00 +00:00
else if (activeGameObject.TryGetComponent<Image>(out var image))
{
2024-12-09 18:39:04 +00:00
if (image.sprite)
2024-12-06 10:46:00 +00:00
{
2024-12-09 18:39:04 +00:00
var srcImgPath = AssetDatabase.GetAssetPath(image.sprite);
if (!string.IsNullOrEmpty(srcImgPath) && m_panelCache.HaveRotScaleInfo(srcImgPath, out var rotScaleInfoItems))
2024-12-06 10:46:00 +00:00
{
2024-12-09 18:39:04 +00:00
m_entityRoot.gameObject.SetActive(true);
2024-12-10 10:14:38 +00:00
2024-12-09 18:39:04 +00:00
bool isFind;
bool IsInside = false;
foreach (var imgEntity in m_imageEntities)
2024-12-06 10:46:00 +00:00
{
2024-12-09 18:39:04 +00:00
isFind = false;
foreach (var rotScale in rotScaleInfoItems)
2024-12-06 10:46:00 +00:00
{
2024-12-09 18:39:04 +00:00
var imgInfo = imgEntity.ElementInfo;
if ((imgInfo.HaveSlice && imgInfo.imgSlicePath == rotScale.imgPath) || imgInfo.imgPath == rotScale.imgPath)
2024-12-06 10:46:00 +00:00
{
2024-12-09 18:39:04 +00:00
if (rotScale.similarityCalc)
{
imgEntity.SetTransform(0, new Unity.Mathematics.float2(1, 1), true);
}
else
{
imgEntity.SetTransform(rotScale.rotiation, rotScale.scale, false);
}
2024-12-10 10:14:38 +00:00
imgEntity.InternalApplyTransform(imgEntity.transform);
2024-12-09 18:39:04 +00:00
imgEntity.ShowSelectionImg(true);
2024-12-10 10:14:38 +00:00
imgEntity.SetOriginalMatrix(activeGameObject.transform);
2024-12-09 18:39:04 +00:00
m_selectionEntities.Add(imgEntity);
if (!IsInside && imgEntity.IsInside(activeGameObject.transform)) IsInside = true;
isFind = true;
break;
2024-12-06 10:46:00 +00:00
}
}
2024-12-09 18:39:04 +00:00
imgEntity.gameObject.SetActive(isFind);
2024-12-06 10:46:00 +00:00
}
2024-12-09 18:39:04 +00:00
if (IsInside)
{
2024-12-17 03:07:00 +00:00
if (m_lastApplyDataGo && m_lastApplyDataGo == activeGameObject)
2024-12-09 18:39:04 +00:00
{
2024-12-17 03:07:00 +00:00
m_curSelectionEntityGo = activeGameObject;
2024-12-09 18:39:04 +00:00
}
}
else
2024-12-06 10:46:00 +00:00
{
2024-12-17 03:07:00 +00:00
m_curSelectionEntityGo = activeGameObject;
2024-12-06 10:46:00 +00:00
}
2024-12-09 18:39:04 +00:00
foreach (var textEntity in m_textEntities)
{
textEntity.gameObject.SetActive(false);
}
foreach (var prefabEntity in m_prefabEntities)
{
prefabEntity.gameObject.SetActive(false);
}
2024-12-06 10:46:00 +00:00
}
}
}
else if (activeGameObject.TryGetComponent<UnityEngine.UI.Text>(out var _)
2024-12-02 14:14:39 +00:00
|| activeGameObject.TryGetComponent<TMPro.TextMeshProUGUI>(out var _))
2024-10-29 17:51:06 +00:00
{
2024-11-06 17:45:57 +00:00
bool IsInside = false;
m_entityRoot.gameObject.SetActive(true);
foreach (var textEntity in m_textEntities)
{
textEntity.ShowSelectionImg(true);
textEntity.gameObject.SetActive(true);
2024-12-10 10:14:38 +00:00
textEntity.SetOriginalMatrix(activeGameObject.transform);
2024-11-06 17:45:57 +00:00
m_selectionEntities.Add(textEntity);
if (!IsInside && textEntity.IsInside(activeGameObject.transform)) IsInside = true;
}
2024-10-29 17:51:06 +00:00
2024-11-06 17:45:57 +00:00
if (IsInside)
{
2024-12-17 03:07:00 +00:00
if (m_lastApplyDataGo && m_lastApplyDataGo == activeGameObject)
2024-11-06 17:45:57 +00:00
{
2024-12-17 03:07:00 +00:00
m_curSelectionEntityGo = activeGameObject;
2024-11-06 17:45:57 +00:00
}
}
else
{
2024-12-17 03:07:00 +00:00
m_curSelectionEntityGo = activeGameObject;
2024-11-06 17:45:57 +00:00
}
foreach (var imgEntity in m_imageEntities)
{
imgEntity.gameObject.SetActive(false);
}
2024-12-06 10:46:00 +00:00
foreach (var prefabEntity in m_prefabEntities)
{
prefabEntity.gameObject.SetActive(false);
}
}
}
if (activeGameObject.transform.parent != m_entityRoot && activeGameObject.transform.parent != m_background)
{
if (activeGameObject.TryGetComponent<Image>(out var image) && image.IsActive() && image.sprite)
{
2024-12-17 03:07:00 +00:00
var sprite = image.sprite;
var srcImgPath = AssetDatabase.GetAssetPath(sprite);
2024-12-09 18:39:04 +00:00
if (!string.IsNullOrEmpty(srcImgPath) && !m_panelCache.HaveRotScaleInfo(srcImgPath) &&
2024-12-10 10:14:38 +00:00
!isCalcRotationScaleRunning)
2024-12-06 10:46:00 +00:00
{
2024-12-17 03:07:00 +00:00
UpdatePanelCache(srcImgPath, sprite.border != Vector4.zero);
2024-12-09 18:39:04 +00:00
//var srcImgDirPath = System.IO.Path.GetDirectoryName(srcImgPath);
//AddCheckImgDirPath(srcImgDirPath);
2024-12-06 10:46:00 +00:00
}
2024-10-29 17:51:06 +00:00
}
}
2024-10-28 16:31:38 +00:00
}
2024-12-09 18:39:04 +00:00
public void Init(PanelCache panelCache)
2024-10-23 17:30:59 +00:00
{
this.m_panelCache = panelCache;
2024-12-03 05:05:13 +00:00
this.m_stageManager = StageManager.Instance;
2024-10-23 17:30:59 +00:00
// 创建所有实例
CreateAllEntity();
2024-12-02 17:47:50 +00:00
InitBackground();
2024-12-09 18:39:04 +00:00
2024-12-17 03:07:00 +00:00
HashSet<string> slicePaths = new HashSet<string>();
foreach (var entity in m_imageEntities)
{
var elementInfo = entity.ElementInfo;
if (elementInfo.HaveSlice)
{
var imageName = Path.GetFileName(elementInfo.imgPath);
slicePaths.Add(imageName);
}
}
2024-12-09 18:39:04 +00:00
targetImages = new ();
targetPaths = new ();
2024-12-17 03:07:00 +00:00
ImageUtils.LoadPngImagesFromFolder(panelCache.TargetImgDirPath, targetImages, targetPaths,
(imagePath) => {
var imageName = Path.GetFileName(imagePath);
if (m_BackgroundFileAllName == imageName) return true;
if (slicePaths.Contains(imageName)) return true;
return false;
});
2024-10-23 17:30:59 +00:00
}
2024-12-03 05:05:13 +00:00
private void OnUpdateBackgroundShow(bool show)
{
if (m_background)
{
m_background.gameObject.SetActive(show);
}
}
2024-10-29 17:51:06 +00:00
private void OnUpdateHierarchyOfEntityAllEntity(bool show)
2024-10-28 16:31:38 +00:00
{
UpdateHierarchyOfEntity(show, m_entityRoot.gameObject);
2024-12-02 17:47:50 +00:00
UpdateHierarchyOfEntity(show, m_background.gameObject);
2024-12-10 10:14:38 +00:00
foreach (Transform entity in m_background.transform) UpdateHierarchyOfEntity(show, entity.gameObject);
foreach (var entity in m_imageEntities) UpdateHierarchyOfEntity(show, entity.gameObject);
foreach (var entity in m_textEntities) UpdateHierarchyOfEntity(show, entity.gameObject);
2024-12-10 10:14:38 +00:00
foreach (var entity in m_prefabEntities) UpdateHierarchyOfEntity(show, entity.gameObject);
2024-10-28 16:31:38 +00:00
}
private void UpdateHierarchyOfEntity(in bool show, in GameObject entity)
{
2024-10-29 17:51:06 +00:00
EntityHelper.UpdateHierarchyAndSceneVisibilityOfEntity(show, entity.gameObject);
2024-10-28 16:31:38 +00:00
}
2024-10-23 17:30:59 +00:00
private void CreateAllEntity()
{
if (this.m_panelCache == null) return;
2024-12-06 10:46:00 +00:00
var go = new GameObject("__entityRoot__", typeof(RectTransform));
2024-10-28 16:31:38 +00:00
UpdateHierarchyOfEntity(false, go);
m_entityRoot = go.transform;
m_entityRoot.SetParent(transform);
m_entityRoot.localPosition = Vector3.zero;
m_entityRoot.localRotation = Quaternion.identity;
m_entityRoot.localScale = Vector3.one;
2024-10-28 16:31:38 +00:00
2024-12-02 14:14:39 +00:00
var canvas = m_entityRoot.gameObject.AddComponent<Canvas>();
canvas.pixelPerfect = true;
canvas.overrideSorting = true;
canvas.sortingOrder = 200;
m_imageEntities = new(m_panelCache.layoutInfo.Count);
m_textEntities = new(m_panelCache.layoutInfo.Count);
m_selectionEntities = new(m_panelCache.layoutInfo.Count);
2024-12-06 10:46:00 +00:00
m_prefabEntities = new(m_panelCache.layoutInfo.Count);
2024-10-28 16:31:38 +00:00
foreach (var elementInfo in m_panelCache.GetLayoutElementInfos<LayoutInfo.ElementInfo>())
2024-10-23 17:30:59 +00:00
{
2024-10-28 16:31:38 +00:00
var imgInfo = elementInfo as LayoutInfo.ImageInfo;
if (imgInfo != null) // Image
{
2024-11-28 10:34:17 +00:00
go = new GameObject(imgInfo.name, typeof(RectTransform));
2024-10-28 16:31:38 +00:00
var entity = go.AddComponent<ImageEntity>();
entity.transform.SetParent(m_entityRoot);
2024-10-28 16:31:38 +00:00
entity.transform.SetSiblingIndex(0);
2024-10-23 17:30:59 +00:00
2024-10-28 16:31:38 +00:00
entity.SetData(imgInfo);
2024-12-01 16:33:27 +00:00
entity.InitPreview();
2024-10-28 16:31:38 +00:00
m_imageEntities.Add(entity);
2024-10-28 16:31:38 +00:00
UpdateHierarchyOfEntity(false, entity.gameObject);
2024-12-06 10:46:00 +00:00
continue;
}
2024-12-01 16:33:27 +00:00
2024-12-06 10:46:00 +00:00
var prefabInfo = elementInfo as LayoutInfo.PrefabInfo;
if (prefabInfo != null) // 预制体
{
go = new GameObject(prefabInfo.name, typeof(RectTransform));
var entity = go.AddComponent<PrefabEntity>();
entity.transform.SetParent(m_entityRoot);
entity.transform.SetSiblingIndex(0);
entity.SetData(prefabInfo);
entity.InitPreview();
m_prefabEntities.Add(entity);
UpdateHierarchyOfEntity(false, entity.gameObject);
2024-10-28 16:31:38 +00:00
continue;
}
2024-12-06 10:46:00 +00:00
2024-10-28 16:31:38 +00:00
var textInfo = elementInfo as LayoutInfo.TextInfo;
if (textInfo != null) // Text
{
2024-11-28 10:34:17 +00:00
if (m_useTMP)
{
go = new GameObject(textInfo.text, typeof(RectTransform));
var entity = go.AddComponent<TextMeshProEntity>();
entity.transform.SetParent(m_entityRoot);
entity.transform.SetSiblingIndex(0);
2024-10-28 16:31:38 +00:00
2024-11-28 10:34:17 +00:00
entity.SetData(textInfo);
2024-12-01 16:33:27 +00:00
entity.InitPreview();
2024-10-23 17:30:59 +00:00
2024-12-01 16:33:27 +00:00
m_textEntities.Add(entity);
2024-11-28 10:34:17 +00:00
UpdateHierarchyOfEntity(false, entity.gameObject);
continue;
}
else
{
go = new GameObject(textInfo.text, typeof(RectTransform));
var entity = go.AddComponent<TextEntity>();
entity.transform.SetParent(m_entityRoot);
entity.transform.SetSiblingIndex(0);
2024-10-28 16:31:38 +00:00
2024-11-28 10:34:17 +00:00
entity.SetData(textInfo);
2024-12-01 16:33:27 +00:00
entity.InitPreview();
2024-11-28 10:34:17 +00:00
m_textEntities.Add(entity);
UpdateHierarchyOfEntity(false, entity.gameObject);
continue;
}
}
2024-10-28 16:31:38 +00:00
}
2024-10-29 17:51:06 +00:00
m_entityRoot.gameObject.SetActive(false);
2024-10-23 17:30:59 +00:00
}
2024-12-17 03:07:00 +00:00
class SelectionObjCache
{
Transform m_transform;
List<RectTransform> childrens = new ();
List<Vector3> originalPositions = new();
public Transform Transform => m_transform;
public void SetData(Transform tf)
{
ClearData();
m_transform = tf;
for (int i = 0; i < tf.childCount; i++)
{
var rt = tf.GetChild(i) as RectTransform;
childrens.Add(rt);
originalPositions.Add(rt.position);
}
}
public void ClearData()
{
childrens.Clear();
originalPositions.Clear();
originalPositions.Clear();
m_transform = null;
}
public void RecoverData()
{
// 恢复子节点的全局位置和大小
for (int i = 0; i < childrens.Count; i++)
{
Undo.RecordObject(childrens[i], "RecoverData");
childrens[i].position = originalPositions[i];
}
}
}
2024-10-23 17:30:59 +00:00
}
}
#endif