#if UNITY_EDITOR using Sirenix.OdinInspector; using System.Collections.Generic; using UnityEditor; using UnityEngine; using static UnityEngine.EventSystems.EventTrigger; namespace UguiToolkit.Editor { [ExecuteAlways] public class EntityManager : MonoBehaviour, IManager { private PanelCache m_panelCache; private Transform entityRoot; private GameObject lastSelectionGo; private IEntity lastSelectionEntity; private List imageEntities; private List textEntities; private List selectionEntities; [LabelText("脱离选择控制"), ShowInInspector] private bool noSelection; private void OnEnable() { GlobalManager.Instance.showHierarchyOfEntityChanged += OnUpdateHierarchyOfEntityAllEntity; Selection.selectionChanged += OnSelectionChanged; } private void OnDisable() { GlobalManager.Instance.showHierarchyOfEntityChanged -= OnUpdateHierarchyOfEntityAllEntity; Selection.selectionChanged -= OnSelectionChanged; } private void Update() { // 检测是否到达可选实例矩形内部 if (selectionEntities != null && Selection.activeGameObject != null) { if (lastSelectionGo && lastSelectionGo == Selection.activeGameObject) { if (lastSelectionEntity != null && !lastSelectionEntity.IsInside(lastSelectionGo.transform)) { lastSelectionGo = null; lastSelectionEntity = null; } return; } bool isApplyTransform = false; foreach (var entity in selectionEntities) { var tf = Selection.activeGameObject.transform; if (entity.IsInside(tf)) { entity.ApplyTransform(tf); lastSelectionGo = Selection.activeGameObject; lastSelectionEntity = entity; Selection.activeGameObject = null; isApplyTransform = true; break; } } if (isApplyTransform) selectionEntities.Clear(); } } private void OnSelectionChanged() { if (noSelection) return; entityRoot.gameObject.SetActive(false); selectionEntities.Clear(); if (Selection.activeGameObject != null && m_panelCache != null) { var activeGameObject = Selection.activeGameObject; if (activeGameObject.transform.parent == entityRoot) return; if (activeGameObject.TryGetComponent(out var image)) { var rotScaleInfos = m_panelCache.rotScaleInfos; if (rotScaleInfos == null) return; if (image.sprite == null) return; var srcImgPath = AssetDatabase.GetAssetPath(image.sprite); if (!rotScaleInfos.TryGetValue(srcImgPath, out var rotScaleInfoItems)) return; entityRoot.gameObject.SetActive(true); bool isFind; foreach (var imgEntity in imageEntities) { isFind = false; foreach (var rotScale in rotScaleInfoItems) { var imgInfo = imgEntity.ElementInfo; if (imgInfo.imgPath == rotScale.imgPath) { imgEntity.SetTransform(rotScale.rotiation, rotScale.scale); imgEntity.ShowSelectionImg(true); selectionEntities.Add(imgEntity); isFind = true; break; } } imgEntity.gameObject.SetActive(isFind); } foreach (var textEntity in textEntities) { textEntity.gameObject.SetActive(false); } } else if (activeGameObject.TryGetComponent(out var text)) { } } } public void InitAllEntity(PanelCache panelCache) { this.m_panelCache = panelCache; // 创建所有实例 CreateAllEntity(); } private void OnUpdateHierarchyOfEntityAllEntity(bool show) { UpdateHierarchyOfEntity(show, entityRoot.gameObject); foreach (var entity in imageEntities) UpdateHierarchyOfEntity(show, entity.gameObject); foreach (var entity in textEntities) UpdateHierarchyOfEntity(show, entity.gameObject); } private void UpdateHierarchyOfEntity(in bool show, in GameObject entity) { EntityHelper.UpdateHierarchyAndSceneVisibilityOfEntity(show, entity.gameObject); } private void CreateAllEntity() { if (this.m_panelCache == null) return; var go = new GameObject("_entityRoot_", typeof(RectTransform)); UpdateHierarchyOfEntity(false, go); entityRoot = go.transform; entityRoot.SetParent(transform); entityRoot.localPosition = Vector3.zero; entityRoot.localRotation = Quaternion.identity; entityRoot.localScale = Vector3.one; imageEntities = new(m_panelCache.layoutInfo.Count); textEntities = new(m_panelCache.layoutInfo.Count); selectionEntities = new(m_panelCache.layoutInfo.Count); foreach (var elementInfo in m_panelCache.GetLayoutElementInfos()) { var imgInfo = elementInfo as LayoutInfo.ImageInfo; if (imgInfo != null) // Image { go = new GameObject("",typeof(RectTransform)); var entity = go.AddComponent(); entity.transform.SetParent(entityRoot); entity.transform.SetSiblingIndex(0); entity.SetData(imgInfo); entity.InitPreviewImage(); imageEntities.Add(entity); UpdateHierarchyOfEntity(false, entity.gameObject); continue; } var textInfo = elementInfo as LayoutInfo.TextInfo; if (textInfo != null) // Text { go = new GameObject("", typeof(RectTransform)); var entity = go.AddComponent(); entity.transform.SetParent(entityRoot); entity.transform.SetSiblingIndex(0); entity.SetData(textInfo); entity.InitPreviewText(); textEntities.Add(entity); UpdateHierarchyOfEntity(false, entity.gameObject); continue; } } entityRoot.gameObject.SetActive(false); } } } #endif