/************************************************/ /* */ /* Copyright (c) 2018 - 2021 monitor1394 */ /* https://github.com/monitor1394 */ /* */ /************************************************/ using System.Text; using System; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; #if dUI_TextMeshPro using TMPro; #endif #if UNITY_EDITOR using UnityEditor; #endif namespace XCharts { public static class ChartHelper { private static StringBuilder s_Builder = new StringBuilder(); private static Vector3 s_DefaultIngoreDataVector3 = Vector3.zero; public static StringBuilder sb { get { return s_Builder; } } public static Vector3 ignoreVector3 { get { return s_DefaultIngoreDataVector3; } } public static bool IsIngore(Vector3 pos) { return pos == s_DefaultIngoreDataVector3; } public static string Cancat(string str1, string str2) { s_Builder.Length = 0; s_Builder.Append(str1).Append(str2); return s_Builder.ToString(); } public static string Cancat(string str1, int i) { s_Builder.Length = 0; s_Builder.Append(str1).Append(ChartCached.IntToStr(i)); return s_Builder.ToString(); } public static void SetActive(GameObject gameObject, bool active) { SetActive(gameObject.transform, active); } public static void SetActive(Image image, bool active) { if (image == null) return; SetActive(image.gameObject, active); } public static void SetActive(Text text, bool active) { if (text == null) return; SetActive(text.gameObject, active); } /// /// 通过设置scale实现是否显示,优化性能,减少GC /// /// /// public static void SetActive(Transform transform, bool active) { if (active) transform.localScale = Vector3.one; else transform.localScale = Vector3.zero; } public static void HideAllObject(GameObject obj, string match = null) { HideAllObject(obj.transform, match); } public static void HideAllObject(Transform parent, string match = null) { ActiveAllObject(parent, false, match); } public static void ActiveAllObject(Transform parent, bool active, string match = null) { for (int i = 0; i < parent.childCount; i++) { if (match == null) SetActive(parent.GetChild(i), active); else { var go = parent.GetChild(i); if (go.name.StartsWith(match)) { SetActive(go, active); } } } } public static void DestroyAllChildren(Transform parent) { if (parent == null) return; var childCount = parent.childCount; for (int i = childCount - 1; i >= 0; i--) { var go = parent.GetChild(i); if (go != null) { GameObject.DestroyImmediate(go.gameObject); } } } public static void DestoryGameObject(Transform parent, string childName) { if (parent == null) return; var go = parent.Find(childName); if (go != null) { GameObject.DestroyImmediate(go.gameObject); } } public static void DestoryGameObject(GameObject go) { if (go != null) GameObject.DestroyImmediate(go); } public static string GetFullName(Transform transform) { string name = transform.name; Transform obj = transform; while (obj.transform.parent) { name = obj.transform.parent.name + "/" + name; obj = obj.transform.parent; } return name; } public static void RemoveComponent(GameObject gameObject) { var component = gameObject.GetComponent(); if (component != null) { #if UNITY_EDITOR if (!Application.isPlaying) GameObject.DestroyImmediate(component as GameObject); else GameObject.Destroy(component as GameObject); #else GameObject.Destroy(component as GameObject); #endif } } public static T GetOrAddComponent(Transform transform) where T : Component { return GetOrAddComponent(transform.gameObject); } public static T GetOrAddComponent(GameObject gameObject) where T : Component { if (gameObject.GetComponent() == null) { return gameObject.AddComponent(); } else { return gameObject.GetComponent(); } } public static GameObject AddObject(string name, Transform parent, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta, int replaceIndex = -1) { GameObject obj; if (parent.Find(name)) { obj = parent.Find(name).gameObject; SetActive(obj, true); obj.transform.localPosition = Vector3.zero; obj.transform.localScale = Vector3.one; } else if (replaceIndex >= 0 && replaceIndex < parent.childCount) { obj = parent.GetChild(replaceIndex).gameObject; if (!obj.name.Equals(name)) obj.name = name; SetActive(obj, true); } else { obj = new GameObject(); obj.name = name; obj.transform.SetParent(parent); obj.transform.localScale = Vector3.one; obj.transform.localPosition = Vector3.zero; } RectTransform rect = GetOrAddComponent(obj); rect.localPosition = Vector3.zero; rect.sizeDelta = sizeDelta; rect.anchorMin = anchorMin; rect.anchorMax = anchorMax; rect.pivot = pivot; rect.anchoredPosition3D = Vector3.zero; return obj; } public static void UpdateRectTransform(GameObject obj, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta) { if (obj == null) return; RectTransform rect = GetOrAddComponent(obj); rect.sizeDelta = sizeDelta; rect.anchorMin = anchorMin; rect.anchorMax = anchorMax; rect.pivot = pivot; } public static ChartText AddTextObject(string name, Transform parent, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta, TextStyle textStyle, ComponentTheme theme) { GameObject txtObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta); txtObj.transform.localEulerAngles = new Vector3(0, 0, textStyle.rotate); var chartText = new ChartText(); #if dUI_TextMeshPro RemoveComponent(txtObj); chartText.tmpText = GetOrAddComponent(txtObj); chartText.tmpText.font = textStyle.tmpFont == null ? theme.tmpFont : textStyle.tmpFont; chartText.tmpText.fontStyle = textStyle.tmpFontStyle; chartText.tmpText.alignment = textStyle.tmpAlignment; chartText.tmpText.richText = true; chartText.tmpText.raycastTarget = false; chartText.tmpText.enableWordWrapping = false; #else chartText.text = GetOrAddComponent(txtObj); chartText.text.font = textStyle.font == null ? theme.font : textStyle.font; chartText.text.fontStyle = textStyle.fontStyle; chartText.text.alignment = textStyle.alignment; chartText.text.horizontalOverflow = HorizontalWrapMode.Overflow; chartText.text.verticalOverflow = VerticalWrapMode.Overflow; chartText.text.supportRichText = true; chartText.text.raycastTarget = false; #endif chartText.SetColor(textStyle.GetColor(theme.textColor)); chartText.SetFontSize(textStyle.fontSize > 0 ? textStyle.fontSize : theme.fontSize); chartText.SetText("Text"); chartText.SetLineSpacing(textStyle.lineSpacing); RectTransform rect = GetOrAddComponent(txtObj); rect.localPosition = Vector3.zero; rect.sizeDelta = sizeDelta; rect.anchorMin = anchorMin; rect.anchorMax = anchorMax; rect.pivot = pivot; return chartText; } public static Text AddTextObject(string name, Transform parent, Font font, Color color, TextAnchor anchor, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta, int fontSize = 14, float rotate = 0, FontStyle fontStyle = FontStyle.Normal, float lineSpacing = 1) { GameObject txtObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta); var txt = GetOrAddComponent(txtObj); txt.font = font; txt.fontSize = fontSize; txt.fontStyle = fontStyle; txt.text = "Text"; txt.alignment = anchor; txt.horizontalOverflow = HorizontalWrapMode.Overflow; txt.verticalOverflow = VerticalWrapMode.Overflow; txt.color = color; txt.lineSpacing = lineSpacing; txt.raycastTarget = false; txtObj.transform.localEulerAngles = new Vector3(0, 0, rotate); RectTransform rect = GetOrAddComponent(txtObj); rect.localPosition = Vector3.zero; rect.sizeDelta = sizeDelta; rect.anchorMin = anchorMin; rect.anchorMax = anchorMax; rect.pivot = pivot; return txt; } #if dUI_TextMeshPro public static TextMeshProUGUI AddTMPTextObject(string name, Transform parent, TMP_FontAsset font, Color color, TextAlignmentOptions anchor, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta, int fontSize = 14, float rotate = 0, FontStyles fontStyle = FontStyles.Normal, float lineSpacing = 1) { GameObject txtObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta); var txt = GetOrAddComponent(txtObj); txt.font = font; txt.fontSize = fontSize; txt.fontStyle = fontStyle; txt.text = "Text"; txt.alignment = anchor; txt.color = color; txt.lineSpacing = lineSpacing; txt.raycastTarget = false; txt.enableWordWrapping = false; txtObj.transform.localEulerAngles = new Vector3(0, 0, rotate); RectTransform rect = GetOrAddComponent(txtObj); rect.localPosition = Vector3.zero; rect.sizeDelta = sizeDelta; rect.anchorMin = anchorMin; rect.anchorMax = anchorMax; rect.pivot = pivot; return txt; } #endif public static Button AddButtonObject(string name, Transform parent, Font font, int fontSize, Color color, TextAnchor anchor, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta, float lineSpacing) { GameObject btnObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta); GetOrAddComponent(btnObj); GetOrAddComponent