// Update is called once per frame void Update() { if (add_characters) { add_characters = false; font.TryAddCharacters("我是个好人,还不行吗"); } if (clear_font) { clear_font = false; font.ClearFontAssetData(); } }
private void ResetFontAssetData(TMP_FontAsset font) { Dictionary <uint, int> charRefDictionary; if (!m_FontCharacterLookupDictionary.TryGetValue(font, out charRefDictionary)) { return; } m_CharTryAddCharacters.Clear(); foreach (var kv in charRefDictionary) { m_CharTryAddCharacters.Add((char)kv.Key); } HashSet <char> nonexistentCharacters; if (!m_CharNonexistentCharacters.TryGetValue(font, out nonexistentCharacters)) { nonexistentCharacters = new HashSet <char>(); m_CharNonexistentCharacters.Add(font, nonexistentCharacters); } // 重设的话,要把所有用到的文本组件刷一下,不然会有残留影像 font.ClearFontAssetData(); m_CharMissingCharacters.Clear(); bool ret = font.TryAddCharacters(m_CharTryAddCharacters, m_CharMissingCharacters); MissingCharactersToNonexistent(font, nonexistentCharacters, charRefDictionary); foreach (var kv in m_TextOldCharacterDictionary) { if (kv.Key) { kv.Key.SetVerticesDirty(); } } }
private void AddMissingCharacters(TMP_FontAsset font) { font = GetNextDynamicFontAsset(font); if (font == null) { return; } Dictionary <uint, int> charRefDictionary; if (!m_FontCharacterLookupDictionary.TryGetValue(font, out charRefDictionary)) { charRefDictionary = new Dictionary <uint, int>(); m_FontCharacterLookupDictionary.Add(font, charRefDictionary); } HashSet <char> nonexistentCharacters; if (!m_CharNonexistentCharacters.TryGetValue(font, out nonexistentCharacters)) { nonexistentCharacters = new HashSet <char>(); m_CharNonexistentCharacters.Add(font, nonexistentCharacters); } m_CharTryAddCharacters.Clear(); m_CharMissingCharacters2.Clear(); foreach (var charMissingCharacter in m_CharMissingCharacters) { // 如果已经被使用过,则使用计数+1 int count; if (charRefDictionary.TryGetValue((uint)charMissingCharacter, out count)) { count++; charRefDictionary[charMissingCharacter] = count; continue; } // 无法生成的字符,不计算 if (nonexistentCharacters.Contains(charMissingCharacter)) { m_CharMissingCharacters2.Add(charMissingCharacter); continue; } count = 1; charRefDictionary.Add(charMissingCharacter, count); // 优化,如果此字符已经动态生成,就不再放到生成串里 if (!(font.characterLookupTable != null && font.HasCharacter(charMissingCharacter))) { m_CharTryAddCharacters.Add(charMissingCharacter); } } if (m_CharTryAddCharacters.Count > 0 && font.characterLookupTable != null) { m_CharMissingCharacters.Clear(); bool ret = font.TryAddCharacters(m_CharTryAddCharacters, m_CharMissingCharacters); MissingCharactersToNonexistent(font, nonexistentCharacters, charRefDictionary); foreach (var charMissingCharacter in m_CharMissingCharacters) { m_CharMissingCharacters2.Add(charMissingCharacter); } if (!ret) { ResetFontAssetData(font); } m_CharMissingCharacters.Clear(); foreach (var charMissingCharacter in m_CharMissingCharacters2) { m_CharMissingCharacters.Add(charMissingCharacter); } } // 判断下一级回调 if (m_CharMissingCharacters.Count > 0) { AddMissingCharacters(font); } }