using System.Text; using System; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace XCharts { public static class ChartHelper { public static float CRICLE_SMOOTHNESS = 2f; private static UIVertex[] vertex = new UIVertex[4]; private static StringBuilder s_Builder = new StringBuilder(); 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); } /// /// 通过设置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) { for (int i = 0; i < parent.childCount; i++) { if (match == null) SetActive(parent.GetChild(i), false); //parent.GetChild(i).gameObject.SetActive(false); else { var go = parent.GetChild(i); if (go.name.StartsWith(match)) { SetActive(go, false); //go.gameObject.SetActive(false); } } } } public static void DestoryAllChilds(Transform parent) { while (parent.childCount > 0) { var go = parent.GetChild(0); if (go.childCount > 0) DestoryAllChilds(go); else GameObject.DestroyImmediate(go.gameObject); } } public static string GetFullName(Transform transform) { string name = transform.name; Transform obj = transform; while (obj.transform.parent) { name += "/" + obj.transform.parent.name; obj = obj.transform.parent; } return name; } 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) { gameObject.AddComponent(); } return gameObject.GetComponent(); } public static GameObject AddObject(string name, Transform parent, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta) { GameObject obj; if (parent.Find(name)) { obj = parent.Find(name).gameObject; obj.SetActive(true); obj.transform.localPosition = Vector3.zero; obj.transform.localScale = Vector3.one; } 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; return obj; } 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) { GameObject txtObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta); Text txt = GetOrAddComponent(txtObj); txt.font = font; txt.fontSize = fontSize; txt.text = "Text"; txt.alignment = anchor; txt.horizontalOverflow = HorizontalWrapMode.Overflow; txt.verticalOverflow = VerticalWrapMode.Overflow; txt.color = color; if (rotate > 0) { 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 txtObj.GetComponent(); } public static Button AddButtonObject(string name, Transform parent, Font font, int fontSize, Color color, TextAnchor anchor, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta) { GameObject btnObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta); GetOrAddComponent(btnObj); GetOrAddComponent