using System; using System.IO; using System.Collections; using System.Xml.Serialization; using UnityEngine; using UnityEngine.UI; namespace LzFramework { /// /// some common used method /// public class Common { /// /// Calculate string Length with font /// /// /// /// /// /// public static int CalculateLengthOfText(string message, Font font, int fontSize, FontStyle fontStyle) { font.RequestCharactersInTexture(message, fontSize, fontStyle); CharacterInfo characterInfo = new CharacterInfo(); char[] arr = message.ToCharArray(); int totalLength = 0; foreach (char c in arr) { font.GetCharacterInfo(c, out characterInfo, fontSize); totalLength += characterInfo.advance; } return totalLength; } /// /// Enable or Disable MaskableGraphic in self and children /// /// /// public static void EnaleGraphic(Transform trans, bool enable) { MaskableGraphic graphic = trans.GetComponent(); if (graphic) graphic.enabled = enable; foreach (Transform tran in trans) { EnaleGraphic(tran, enable); } } /// /// Enable or Disable Renderer in self and children /// /// /// public static void EnableRenderer(Transform trans, bool enable) { Renderer renderer = trans.GetComponent(); if (renderer) renderer.enabled = enable; foreach (Transform tran in trans) { EnableRenderer(tran, enable); } } /// /// Do After . /// Mark with /// /// /// /// /// public static MineCoroutine DoSomethingAfterTime(float time, Action something, string coroutineName = null) { return SingletonCreator.Create().CreateCoroutine(StartSomethingAfterTime(time, something), null, null, true, coroutineName); } /// /// Do Something After /// /// /// /// protected static IEnumerator StartSomethingAfterTime(float time, Action something) { yield return new WaitForSeconds(time); if (null != something) something(); } } /// /// Xml common used Method /// public class XmlCommon { /// /// Deserialize XML from string /// /// /// /// public static T FromString(string xml) { using (StringReader sr = new StringReader(xml)) { Type type = typeof(T); XmlSerializer xmldes = new XmlSerializer(type); return (T)xmldes.Deserialize(sr); } } /// /// Deserialize xml from file /// /// /// /// public static T FromFile(string path) { if (File.Exists(path)) { string xml = File.ReadAllText(path); return FromString(xml); } return default(T); } /// /// Serialize to xml string /// /// /// /// public static string ToXml(T t) { try { Type type = typeof(T); using (MemoryStream Stream = new MemoryStream()) { XmlSerializer xml = new XmlSerializer(type); xml.Serialize(Stream, t); Stream.Position = 0; using (StreamReader sr = new StreamReader(Stream)) { return sr.ReadToEnd(); } } } catch (Exception ex) { Debug.Log(ex); } return null; } /// /// Serialize to file /// /// /// /// public static void WriteXmlFile(T t, string path) { string xml = ToXml(t); if (string.IsNullOrEmpty(xml)) return; File.WriteAllText(path, xml); } } }