123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using HDJ.Framework.Utils;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- using UnityEngine.UI;
- public class UITextStyleManager
- {
- private const string FilePathDir = "Assets/Resources/Config/";
- private const string FileName = "UITextStyleConfig";
- public static Dictionary<string, Dictionary<SystemLanguage, TextStyleData>> styleDataDic = new Dictionary<string, Dictionary<SystemLanguage, TextStyleData>>();
- public static void Init()
- {
- styleDataDic = LoadData();
- }
- public static Dictionary<string, Dictionary<SystemLanguage, TextStyleData>> LoadData()
- {
- string text = "";
- if (Application.isPlaying)
- {
- text = AssetsPoolManager.ReadTextFile(FileName);
- }
- else
- {
- text = FileUtils.LoadTextFileByPath(FilePathDir + FileName + ".txt");
- }
- if (!string.IsNullOrEmpty(text))
- {
- return JsonUtils.FromJson<Dictionary<string, Dictionary<SystemLanguage, TextStyleData>>>(text);
- }
- else
- {
- return new Dictionary<string, Dictionary<SystemLanguage, TextStyleData>>();
- }
- }
- public static void SaveData(Dictionary<string, Dictionary<SystemLanguage, TextStyleData>> styleDataDic)
- {
- string text = JsonUtils.ToJson(styleDataDic);
- FileUtils.CreateTextFile(FilePathDir + FileName + ".txt", text);
- }
- public static bool ContainsData(string name, SystemLanguage language)
- {
- Dictionary<SystemLanguage, TextStyleData> data = null;
- if (styleDataDic.ContainsKey(name))
- data = styleDataDic[name];
- else
- return false;
- if (data.ContainsKey(language))
- {
- return true;
- }
- else
- return false;
- }
- public static TextStyleData GetTextStyleData(string name,SystemLanguage language)
- {
- Dictionary<SystemLanguage, TextStyleData> data = null;
- if (styleDataDic.ContainsKey(name))
- data = styleDataDic[name];
- else
- {
- Debug.LogError("no TextStyleData name:" + name);
- return null;
- }
- if (data.ContainsKey(language))
- {
- return data[language];
- }
- else
- {
- Debug.LogError("no TextStyleData language:" + language);
- return null;
- }
- }
- public static void SetText(Text text, string name, SystemLanguage language)
- {
- if(ContainsData(name,language))
- {
- TextStyleData data = GetTextStyleData(name, language);
- if (!ResourcesConfigManager.GetIsExitRes(data.fontName))
- {
- Debug.LogError("dont find font :" + data.fontName);
- }
- else
- text.font = AssetsPoolManager.Load<Font>(data.fontName);
- text.fontSize = data.fontSize;
- text.fontStyle = data.fontStyle;
- text.resizeTextForBestFit = data.bestFit;
- text.resizeTextMinSize = data.minSize;
- text.resizeTextMaxSize = data.maxSize;
- text.alignment = data.alignment;
- text.supportRichText = data.richText;
- text.horizontalOverflow = data.horizontalOverflow;
- text.verticalOverflow = data.verticalOverflow;
- text.lineSpacing = data.lineSpacing;
- }
- }
- public static void SetText(Text text, TextStyleData data)
- {
- if (!ResourcesConfigManager.GetIsExitRes(data.fontName))
- {
- Debug.LogError("dont find font :" + data.fontName);
- }
- else
- text.font = AssetsPoolManager.Load<Font>(data.fontName);
- text.fontSize = data.fontSize;
- text.fontStyle = data.fontStyle;
- text.resizeTextForBestFit = data.bestFit;
- text.resizeTextMinSize = data.minSize;
- text.resizeTextMaxSize = data.maxSize;
- text.alignment = data.alignment;
- text.supportRichText = data.richText;
- text.horizontalOverflow = data.horizontalOverflow;
- text.verticalOverflow = data.verticalOverflow;
- text.lineSpacing = data.lineSpacing;
- }
- public static TextStyleData GetTextStyleDataFromText(Text text)
- {
- TextStyleData data = new TextStyleData();
- data.fontName = text.font.name;
- data.fontSize=text.fontSize ;
- data.fontStyle=text.fontStyle ;
- data.bestFit=text.resizeTextForBestFit ;
- data.minSize=text.resizeTextMinSize ;
- data.maxSize=text.resizeTextMaxSize ;
- data.alignment=text.alignment ;
- data.richText=text.supportRichText ;
- data.horizontalOverflow=text.horizontalOverflow ;
- data.verticalOverflow=text.verticalOverflow ;
- data.lineSpacing=text.lineSpacing ;
- return data;
- }
- }
- public class TextStyleData
- {
- public string fontName { get; set; }
- //
- // 摘要:
- // Font size.
- public int fontSize { get; set; }
- //
- // 摘要:
- // Font Style.
- public FontStyle fontStyle { get; set; }
- //
- // 摘要:
- // Is best fit used.
- public bool bestFit { get; set; }
- //
- // 摘要:
- // Minimum text size.
- public int minSize { get; set; }
- //
- // 摘要:
- // Maximum text size.
- public int maxSize { get; set; }
- //
- // 摘要:
- // How is the text aligned.
- public TextAnchor alignment { get; set; }
- //
- // 摘要:
- // Use the extents of glyph geometry to perform horizontal alignment rather than
- // glyph metrics.
- public bool alignByGeometry { get; set; }
- //
- // 摘要:
- // Should RichText be used?
- public bool richText { get; set; }
- //
- // 摘要:
- // Horizontal overflow mode.
- public HorizontalWrapMode horizontalOverflow { get; set; }
- //
- // 摘要:
- // Vertical overflow mode.
- public VerticalWrapMode verticalOverflow { get; set; }
- //
- // 摘要:
- // Line spacing.
- public float lineSpacing { get; set; }
- }
|