using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using Snxxz.UI; using UnityEngine.UI; [CustomEditor(typeof(TextUpdate))] public class LabelTextColorTool : Editor { [SerializeField] TextUpdate m_Target; [SerializeField] List allTexts = new List(); [SerializeField] GUIStyle m_LabelStyle; [SerializeField] bool m_ShowTextColor; [SerializeField] Color m_LabelColor=Color.black; [SerializeField] int m_LabelSize = 12; [SerializeField] Vector3 m_Offset = Vector3.zero; [SerializeField] bool m_HexColor = false; [SerializeField] Color32 m_UnShowColor = Color.white; void FindAllText() { allTexts.Clear(); FindChildText(m_Target.transform, ref allTexts); } private void OnEnable() { m_LabelStyle = new GUIStyle(); m_LabelStyle.fontSize = 12; m_LabelStyle.normal.textColor = Color.green; } void FindChildText(Transform _transform, ref List _allTexts) { if (_transform != null) { var text = _transform.GetComponent(); if (text != null) { _allTexts.Add(text); } for (int i = 0; i < _transform.childCount; i++) { FindChildText(_transform.GetChild(i), ref _allTexts); } } } public override void OnInspectorGUI() { base.OnInspectorGUI(); m_Target = target as TextUpdate; GUILayout.Space(20); if (GUILayout.Button("显示字色")) { if (m_Target != null) { FindAllText(); } } m_LabelColor = EditorGUILayout.ColorField("字色", m_LabelColor); m_UnShowColor = EditorGUILayout.ColorField("屏蔽字色", m_UnShowColor); m_LabelSize = EditorGUILayout.IntField("字体大小", m_LabelSize); m_Offset = EditorGUILayout.Vector3Field("偏移", m_Offset); m_HexColor = EditorGUILayout.Toggle("16进制", m_HexColor); } private void OnSceneGUI() { for (int i = 0; i < allTexts.Count; i++) { if (allTexts[i] == null) { allTexts.RemoveAt(i); i--; continue; } var _pos = allTexts[i].rectTransform.position; Color32 m_Color = allTexts[i].color; if (m_UnShowColor.Equals(m_Color)) { continue; } if (m_ShowTextColor) { allTexts[i].gameObject.SetActive(false); } else { allTexts[i].gameObject.SetActive(true); } m_LabelStyle.normal.textColor = m_LabelColor; m_LabelStyle.fontSize = m_LabelSize; _pos += m_Offset; Handles.Label(_pos,m_HexColor? GetHexColor(m_Color): m_Color.ToString(), m_LabelStyle); HandleUtility.Repaint(); } } public string GetHexColor(Color32 _color) { return StringUtility.Contact(_color.r.ToString("X2"), _color.g.ToString("X2"), _color.b.ToString("X2"), _color.a.ToString("X2")); } }