using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using CommonUI.Display.Text; using UnityEngine; using UnityEngine.UI; namespace CommonUnity3D.UGUI { public class UIFactory { static UIFactory() { new UIFactory(); } public static UIFactory Instance { get; private set; } private UGUIAttributedStringDecoder mTextDecoder = new UGUIAttributedStringDecoder(); private Font mDefaultFont; private int mDefaultFontBestFitMin; private int mDefaultFontBestFitMax; private Vector2 mDefaultCaretSize = new Vector2(2, 16); private TextGenerator mDefaultTextGenerator; protected UIFactory() { Instance = this; mDefaultTextGenerator = new TextGenerator(); mDefaultFontBestFitMin = 12; mDefaultFontBestFitMax = 22; } public virtual Font DefaultFont { get { if (mDefaultFont == null) { Debug.LogWarning("UIFactory DefaultFont is null, create new Font();"); mDefaultFont = new Font(); } return mDefaultFont; } protected set { mDefaultFont = value; } } public virtual TextGenerator DefaultTextGenerator { get { return mDefaultTextGenerator; } } public int DefaultFontBestFitMin { get { return mDefaultFontBestFitMin; } set { mDefaultFontBestFitMin = value; } } public int DefaultFontBestFitMax { get { return mDefaultFontBestFitMax; } set { mDefaultFontBestFitMax = value; } } public Vector2 DefaultCaretSize { get { return mDefaultCaretSize; } set { mDefaultCaretSize = value; } } virtual public AttributedString DecodeAttributedString(XmlDocument doc, TextAttribute defaultTA = null) { return mTextDecoder.CreateFromXML(doc, defaultTA); } virtual public AttributedString DecodeAttributedString(string doc, TextAttribute defaultTA = null) { return mTextDecoder.CreateFromXML(doc, defaultTA); } virtual public UGUIRichTextLayer CreateRichTextLayer(DisplayNode parent, bool use_bitmap) { return new UGUIRichTextLayer(parent, use_bitmap); } } public class UGUIAttributedStringDecoder : AttributedStringDecoder { public override AttributedString CreateFromXML(string text, TextAttribute defaultTA = null) { return base.CreateFromXML(text, defaultTA); } protected override void DecodeAttribute(XmlElement node, XmlAttribute x_attr, TextAttribute attr) { base.DecodeAttribute(node, x_attr, attr); } #region TextConvert public const string UGUI_COLOR = "= 0) { int end = text.IndexOf('>', color_begin); if (end >= 0) { int fs = color_begin + prefix.Length; string value = text.Substring(fs, end - fs); replace(ref prefix, ref value); string field = prefix + value; index = color_begin + field.Length + 1; text = text.Substring(0, color_begin) + field + text.Substring(end); return true; } } return false; } static private void ReplaceColor(ref string prefix, ref string value) { prefix = "= 0; --i) { if (value == color_map[i][0]) { value = "\"" + color_map[i][1] + "\""; return; } } } } static private void ReplaceSize(ref string prefix, ref string value) { prefix = "" + text + ""; } #endregion } }