public static void DownloadAndBakeAllUsedLocalizationCharactersIntoFonts()
        {
            var charSet = new HashSet <char>();

            LocalizationTable[] allTables = UnityEngine.Object.FindObjectsOfType <LocalizationConfiguration>().SelectMany(config => config.TableSources).Select(s => s.LoadTable()).ToArray();
            foreach (var table in allTables)
            {
                foreach (var culture in table.AllCultures)
                {
                    var textMap = table.GetTextMapFor(culture);
                    foreach (string translatedText in textMap.Values)
                    {
                        charSet.UnionWith(translatedText);
                    }
                }
            }

            string charactersString = new String(charSet.ToArray());

            var bakedFontConfigurations = AssetDatabaseUtil.AllAssetsOfType <BakedLocalizedFontConfiguration>();

            if (bakedFontConfigurations.Count <= 0)
            {
                Debug.LogWarning("Cannot bake used localization characters without any BakedLocalizedFontConfigurations in the project!");
                return;
            }

            foreach (var config in bakedFontConfigurations)
            {
                // No need to bake a font that already contains all the characters
                if (TMP_FontAssetUtil.DoesFontContainAllCharacters(config.OutputFontAsset, charactersString))
                {
                    continue;
                }

                string outputFilePath = AssetDatabase.GetAssetPath(config.OutputFontAsset);
                TMPFontAssetBaker.Bake(config.Font, config.useAutoSizing, config.FontSize, config.CharacterPadding, config.FontPackingMode, config.AtlasWidth, config.AtlasHeight, config.FontStyle, config.FontStyleMod, config.FontRenderMode, charactersString, outputFilePath);
            }
        }
Example #2
0
        private void SetText(string text)
        {
                        #if TMPRO
            if (tmpText_ != null)
            {
                tmpText_.enabled = true;
                tmpText_.text    = text;
                if (downgradedText_ != null)
                {
                    downgradedText_.enabled = false;
                }

                // if TMP_Text can't render the text correctly, we must
                // downgrade it to regular Unity font (dynamic rendering)
                if (!TMP_FontAssetUtil.DoesFontContainAllCharacters(tmpText_.font, text))
                {
                    tmpText_.enabled = false;
                    if (downgradedText_ == null)
                    {
                        var downgradedTextObj = new GameObject("DowngradedText");
                        downgradedTextObj.transform.SetParent(tmpText_.transform, worldPositionStays: false);

                        var downgradedRectTransform = downgradedTextObj.AddComponent <RectTransform>();
                        downgradedRectTransform.anchorMin = Vector2.zero;
                        downgradedRectTransform.anchorMax = Vector2.one;

                        downgradedRectTransform.offsetMax = Vector2.zero;
                        downgradedRectTransform.offsetMin = Vector2.zero;

                        downgradedText_      = downgradedTextObj.AddComponent <Text>();
                        downgradedText_.font = LocalizationConfiguration.DowngradedFont;
                        tmpText_.CopyPropertiesTo(downgradedText_);

                        var contentSizeFitter = tmpText_.GetComponent <ContentSizeFitter>();
                        if (contentSizeFitter != null)
                        {
                            HorizontalOrVerticalLayoutGroup layoutGroup = tmpText_.gameObject.AddComponent <HorizontalLayoutGroup>();
                            layoutGroup.childControlHeight     = true;
                            layoutGroup.childControlWidth      = true;
                            layoutGroup.childForceExpandHeight = true;
                            layoutGroup.childForceExpandWidth  = true;

                            var downgradedContentSizeFitter = downgradedText_.gameObject.AddComponent <ContentSizeFitter>();
                            downgradedContentSizeFitter.horizontalFit = contentSizeFitter.horizontalFit;
                            downgradedContentSizeFitter.verticalFit   = contentSizeFitter.verticalFit;
                        }
                    }

                    downgradedText_.text    = text;
                    downgradedText_.enabled = true;
                }

                return;
            }
                        #endif

            if (unityText_ != null)
            {
                unityText_.text = text;
            }
        }