You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
5.6 KiB
177 lines
5.6 KiB
using System;
|
|
using System.IO;
|
|
using System.Collections;
|
|
using System.Xml.Serialization;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace LzFramework
|
|
{
|
|
/// <summary>
|
|
/// some common used method
|
|
/// </summary>
|
|
public class Common
|
|
{
|
|
/// <summary>
|
|
/// Calculate string Length with font
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="font"></param>
|
|
/// <param name="fontSize"></param>
|
|
/// <param name="fontStyle"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enable or Disable MaskableGraphic in self and children
|
|
/// </summary>
|
|
/// <param name="trans"></param>
|
|
/// <param name="show"></param>
|
|
public static void EnaleGraphic(Transform trans, bool enable)
|
|
{
|
|
MaskableGraphic graphic = trans.GetComponent<MaskableGraphic>();
|
|
if (graphic)
|
|
graphic.enabled = enable;
|
|
foreach (Transform tran in trans)
|
|
{
|
|
EnaleGraphic(tran, enable);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enable or Disable Renderer in self and children
|
|
/// </summary>
|
|
/// <param name="trans"></param>
|
|
/// <param name="enable"></param>
|
|
public static void EnableRenderer(Transform trans, bool enable)
|
|
{
|
|
Renderer renderer = trans.GetComponent<Renderer>();
|
|
if (renderer)
|
|
renderer.enabled = enable;
|
|
foreach (Transform tran in trans)
|
|
{
|
|
EnableRenderer(tran, enable);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Do <paramref name="something"/> After <paramref name="time"/>.
|
|
/// Mark with <paramref name="coroutineName"/>
|
|
/// </summary>
|
|
/// <param name="time"></param>
|
|
/// <param name="something"></param>
|
|
/// <param name="coroutineName"></param>
|
|
/// <returns></returns>
|
|
public static MineCoroutine DoSomethingAfterTime(float time, Action something, string coroutineName = null)
|
|
{
|
|
return SingletonCreator.Create<CoroutineManager>().CreateCoroutine(StartSomethingAfterTime(time, something), null, null, true, coroutineName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Do Something After <paramref name="time"/>
|
|
/// </summary>
|
|
/// <param name="time"></param>
|
|
/// <param name="something"></param>
|
|
/// <returns></returns>
|
|
protected static IEnumerator StartSomethingAfterTime(float time, Action something)
|
|
{
|
|
yield return new WaitForSeconds(time);
|
|
if (null != something)
|
|
something();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Xml common used Method
|
|
/// </summary>
|
|
public class XmlCommon
|
|
{
|
|
/// <summary>
|
|
/// Deserialize XML from string
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="xml"></param>
|
|
/// <returns></returns>
|
|
public static T FromString<T>(string xml)
|
|
{
|
|
using (StringReader sr = new StringReader(xml))
|
|
{
|
|
Type type = typeof(T);
|
|
XmlSerializer xmldes = new XmlSerializer(type);
|
|
return (T)xmldes.Deserialize(sr);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deserialize xml from file
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static T FromFile<T>(string path)
|
|
{
|
|
if (File.Exists(path))
|
|
{
|
|
string xml = File.ReadAllText(path);
|
|
return FromString<T>(xml);
|
|
}
|
|
return default(T);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serialize <paramref name="t"/> to xml string
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="t"></param>
|
|
/// <returns></returns>
|
|
public static string ToXml<T>(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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serialize <paramref name="t"/> to file <paramref name="path"/>
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="t"></param>
|
|
/// <param name="path"></param>
|
|
public static void WriteXmlFile<T>(T t, string path)
|
|
{
|
|
string xml = ToXml(t);
|
|
if (string.IsNullOrEmpty(xml))
|
|
return;
|
|
File.WriteAllText(path, xml);
|
|
}
|
|
}
|
|
}
|