适配 tmp

This commit is contained in:
biaosong 2024-11-28 18:34:17 +08:00
parent 653ecc4f29
commit 125df0ba8a
6 changed files with 148 additions and 21 deletions

View File

@ -0,0 +1,48 @@
#if UNITY_EDITOR
using Sirenix.OdinInspector;
using TMPro;
using Unity.Mathematics;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
namespace UguiToolkit.Editor
{
public class TextMeshProEntity : BaseEntity<TextMeshProUGUI, LayoutInfo.TextInfo>
{
private TextMeshProUGUI m_previewText;
public override void ApplyTransform(Transform tf)
{
var position = ElementInfo.Position;
tf.position = StageManager.Instance.PrefabContentsRoot.transform.TransformPoint(new Vector3(position.x, position.y, 0));
tf.rotation = Quaternion.identity;
}
protected override void OnApplyData(TextMeshProUGUI ui)
{
ui.text = ElementInfo.text;
ui.fontSize = (int)ElementInfo.size;
ui.color = ElementInfo.color;
ui.alignment = TextAlignmentOptions.Center;
var rectTransform = ui.rectTransform;
rectTransform.sizeDelta = new Vector2(ElementInfo.w + 10, ElementInfo.h + 10);
}
public void InitPreviewText()
{
if (ElementInfo == null) return;
if (!TryGetComponent<TextMeshProUGUI>(out m_previewText))
{
m_previewText = gameObject.AddComponent<TextMeshProUGUI>();
}
OnApplyData(m_previewText);
ApplyTransform(transform);
}
}
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9389a2a202d11d34b9c74a3c895450cb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -20,6 +20,7 @@ namespace UguiToolkit.Editor
private List<ImageEntity> m_imageEntities;
private List<TextEntity> m_textEntities;
private List<TextMeshProEntity> m_tmpEntities;
private List<IEntity> m_selectionEntities;
[LabelText("脱离选择控制"), ShowInInspector]
@ -28,12 +29,16 @@ namespace UguiToolkit.Editor
private StageManager m_stageManager;
private HashSet<string> m_checkImgPaths;
private float m_lastCheckTime;
private bool m_useTMP;
private const float m_checkInterval = 2f;
private void OnEnable()
{
GlobalManager.Instance.showHierarchyOfEntityChanged += OnUpdateHierarchyOfEntityAllEntity;
var globalMng = GlobalManager.Instance;
globalMng.showHierarchyOfEntityChanged += OnUpdateHierarchyOfEntityAllEntity;
m_useTMP = globalMng.setting.useTMP;
Selection.selectionChanged += OnSelectionChanged;
m_stageManager = StageManager.Instance;
@ -221,6 +226,10 @@ namespace UguiToolkit.Editor
{
textEntity.gameObject.SetActive(false);
}
foreach (var textEntity in m_tmpEntities)
{
textEntity.gameObject.SetActive(false);
}
}
else if (activeGameObject.TryGetComponent<UnityEngine.UI.Text>(out var text))
{
@ -252,6 +261,45 @@ namespace UguiToolkit.Editor
{
imgEntity.gameObject.SetActive(false);
}
foreach (var textEntity in m_tmpEntities)
{
textEntity.gameObject.SetActive(false);
}
}
else if (activeGameObject.TryGetComponent<TMPro.TextMeshProUGUI>(out var tmp))
{
bool IsInside = false;
m_entityRoot.gameObject.SetActive(true);
foreach (var tmpEntity in m_tmpEntities)
{
tmpEntity.ShowSelectionImg(true);
tmpEntity.gameObject.SetActive(true);
m_selectionEntities.Add(tmpEntity);
if (!IsInside && tmpEntity.IsInside(activeGameObject.transform)) IsInside = true;
}
if (IsInside)
{
if (m_lastSelectionGo && m_lastSelectionGo == activeGameObject)
{
m_curSelectionGo = activeGameObject;
}
}
else
{
m_curSelectionGo = activeGameObject;
}
foreach (var imgEntity in m_imageEntities)
{
imgEntity.gameObject.SetActive(false);
}
foreach (var textEntity in m_textEntities)
{
textEntity.gameObject.SetActive(false);
}
}
}
}
@ -270,6 +318,7 @@ namespace UguiToolkit.Editor
UpdateHierarchyOfEntity(show, m_entityRoot.gameObject);
foreach (var entity in m_imageEntities) UpdateHierarchyOfEntity(show, entity.gameObject);
foreach (var entity in m_textEntities) UpdateHierarchyOfEntity(show, entity.gameObject);
foreach (var entity in m_tmpEntities) UpdateHierarchyOfEntity(show, entity.gameObject);
}
private void UpdateHierarchyOfEntity(in bool show, in GameObject entity)
@ -290,6 +339,7 @@ namespace UguiToolkit.Editor
m_imageEntities = new(m_panelCache.layoutInfo.Count);
m_textEntities = new(m_panelCache.layoutInfo.Count);
m_tmpEntities = new(m_panelCache.layoutInfo.Count);
m_selectionEntities = new(m_panelCache.layoutInfo.Count);
foreach (var elementInfo in m_panelCache.GetLayoutElementInfos<LayoutInfo.ElementInfo>())
@ -297,7 +347,7 @@ namespace UguiToolkit.Editor
var imgInfo = elementInfo as LayoutInfo.ImageInfo;
if (imgInfo != null) // Image
{
go = new GameObject("",typeof(RectTransform));
go = new GameObject(imgInfo.name, typeof(RectTransform));
var entity = go.AddComponent<ImageEntity>();
entity.transform.SetParent(m_entityRoot);
entity.transform.SetSiblingIndex(0);
@ -312,7 +362,23 @@ namespace UguiToolkit.Editor
var textInfo = elementInfo as LayoutInfo.TextInfo;
if (textInfo != null) // Text
{
go = new GameObject("", typeof(RectTransform));
if (m_useTMP)
{
go = new GameObject(textInfo.text, typeof(RectTransform));
var entity = go.AddComponent<TextMeshProEntity>();
entity.transform.SetParent(m_entityRoot);
entity.transform.SetSiblingIndex(0);
entity.SetData(textInfo);
entity.InitPreviewText();
m_tmpEntities.Add(entity);
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);
@ -324,7 +390,7 @@ namespace UguiToolkit.Editor
UpdateHierarchyOfEntity(false, entity.gameObject);
continue;
}
}
}
m_entityRoot.gameObject.SetActive(false);

View File

@ -10,6 +10,7 @@ namespace UguiToolkit.Editor
public string prefabForUIDirPath;
public float distanceDifference = 0.2f;
public bool useTMP;
}
}
#endif

View File

@ -2,7 +2,8 @@
"name": "UguiToolkit.Editor",
"rootNamespace": "",
"references": [
"GUID:d8b63aba1907145bea998dd612889d6b"
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:6055be8ebefd69e48b49212b09b47b2f"
],
"includePlatforms": [
"Editor",

View File

@ -1,7 +1,7 @@
{
"name": "com.soviby.unity.ui.ugui-toolkit",
"displayName": "UguiToolkit",
"description": "Ugui拼接手",
"name": "com.txcombo.c1.ugui-toolkit",
"displayName": "C1UguiToolkit",
"description": "Ugui拼接助手",
"version": "0.0.1",
"unity": "2022.3",
"author": "Soviby",