| | |
| | | { |
| | | font = FontUtility.preferred; |
| | | } |
| | | |
| | | // 添加 null 检查 |
| | | if (font == null || rectTransform == null || cachedTextGeneratorForLayout == null || string.IsNullOrEmpty(m_OutputText)) |
| | | { |
| | | return 0f; |
| | | } |
| | | |
| | | var settings = GetGenerationSettings(Vector2.zero); |
| | | return cachedTextGeneratorForLayout.GetPreferredWidth(m_OutputText, settings) / pixelsPerUnit; |
| | | |
| | | float width = 0f; |
| | | try |
| | | { |
| | | width = cachedTextGeneratorForLayout.GetPreferredWidth(m_OutputText, settings) / pixelsPerUnit; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Debug.LogError($"GetPreferredWidth failed: {ex.Message}"); |
| | | width = 0f; |
| | | } |
| | | return width; |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | font = FontUtility.preferred; |
| | | } |
| | | |
| | | // 添加 null 检查 |
| | | if (font == null || rectTransform == null || cachedTextGeneratorForLayout == null || string.IsNullOrEmpty(m_OutputText)) |
| | | { |
| | | return 0f; |
| | | } |
| | | |
| | | var settings = GetGenerationSettings(new Vector2(rectTransform.rect.size.x, 0.0f)); |
| | | float _height = cachedTextGeneratorForLayout.GetPreferredHeight(m_OutputText, settings) / pixelsPerUnit; |
| | | return _height; |
| | | |
| | | float height = 0f; |
| | | try |
| | | { |
| | | height = cachedTextGeneratorForLayout.GetPreferredHeight(m_OutputText, settings) / pixelsPerUnit; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Debug.LogError($"GetPreferredHeight failed: {ex.Message}"); |
| | | height = 0f; |
| | | } |
| | | return height; |
| | | } |
| | | } |
| | | #endregion |
| | |
| | | |
| | | private Dictionary<int, Match> matchDics = new Dictionary<int, Match>(); |
| | | |
| | | // 字符宽度缓存,避免重复计算相同字符 |
| | | private Dictionary<string, float> charWidthCache = new Dictionary<string, float>(); |
| | | |
| | | private bool IsModifySize(int _index,out int _size) |
| | | { |
| | | _size = 0; |
| | |
| | | { |
| | | font = FontUtility.preferred; |
| | | } |
| | | |
| | | // 添加 null 检查 |
| | | if (font == null || rectTransform == null || cachedTextGeneratorForLayout == null) |
| | | { |
| | | Debug.LogWarning("SetFitterSize: font, rectTransform or cachedTextGeneratorForLayout is null"); |
| | | return; |
| | | } |
| | | |
| | | var settings = GetGenerationSettings(Vector2.zero); |
| | | |
| | | float cache = 0; |
| | |
| | | float ratio = GetResolutionRatio(); |
| | | |
| | | matchDics.Clear(); |
| | | charWidthCache.Clear(); |
| | | |
| | | foreach (Match match in ImgAnalysis.Unity_Img_Regex.Matches(fitterText)) |
| | | { |
| | |
| | | } |
| | | else |
| | | { |
| | | cache = cachedTextGeneratorForLayout.GetPreferredWidth(match.Value, settings) * ratio; |
| | | cache = GetCharWidthCached(match.Value, settings, ratio); |
| | | |
| | | if (width + cache > (rectTransform.rect.width - 5)) |
| | | { |
| | | CacluHrefAndImgIndex(Mathf.Max(0, i - 1)); |
| | |
| | | else |
| | | { |
| | | var _size = 0; |
| | | var _cacheFontSize = fontSize; |
| | | // 不再修改 fontSize,使用临时 TextGenerator 计算不同字号的宽度 |
| | | if (_modifySize && IsModifySize(i, out _size)) |
| | | { |
| | | fontSize = _size; |
| | | settings = GetGenerationSettings(Vector2.zero); |
| | | cache = cachedTextGeneratorForLayout.GetPreferredWidth(fitterText[i].ToString(), settings) * ratio; |
| | | fontSize = _cacheFontSize; |
| | | settings = GetGenerationSettings(Vector2.zero); |
| | | cache = GetCharWidthWithCustomSize(fitterText[i], _size, ratio); |
| | | } |
| | | else |
| | | { |
| | | cache = cachedTextGeneratorForLayout.GetPreferredWidth(fitterText[i].ToString(), settings) * ratio; |
| | | cache = GetCharWidthCached(fitterText[i].ToString(), settings, ratio); |
| | | } |
| | | |
| | | if (width + cache > (rectTransform.rect.width - 5)) |
| | | { |
| | | CacluHrefAndImgIndex(Mathf.Max(0, i - 1)); |
| | |
| | | m_OutputText = textBuilder.ToString(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取字符宽度(带缓存) |
| | | /// </summary> |
| | | private float GetCharWidthCached(string charStr, TextGenerationSettings settings, float ratio) |
| | | { |
| | | string cacheKey = $"{fontSize}_{charStr}"; |
| | | |
| | | if (charWidthCache.ContainsKey(cacheKey)) |
| | | { |
| | | return charWidthCache[cacheKey]; |
| | | } |
| | | |
| | | float width = 0; |
| | | try |
| | | { |
| | | width = cachedTextGeneratorForLayout.GetPreferredWidth(charStr, settings) * ratio; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Debug.LogError($"GetPreferredWidth failed for '{charStr}': {ex.Message}"); |
| | | width = 0; |
| | | } |
| | | |
| | | charWidthCache[cacheKey] = width; |
| | | return width; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 使用自定义字号计算字符宽度(不修改原始 Text 组件的 fontSize) |
| | | /// </summary> |
| | | private float GetCharWidthWithCustomSize(char c, int customFontSize, float ratio) |
| | | { |
| | | string charStr = c.ToString(); |
| | | string cacheKey = $"{customFontSize}_{charStr}"; |
| | | |
| | | if (charWidthCache.ContainsKey(cacheKey)) |
| | | { |
| | | return charWidthCache[cacheKey]; |
| | | } |
| | | |
| | | float width = 0; |
| | | try |
| | | { |
| | | // 创建临时的 TextGenerationSettings,使用自定义字号 |
| | | TextGenerationSettings tempSettings = GetGenerationSettings(Vector2.zero); |
| | | tempSettings.fontSize = customFontSize; |
| | | |
| | | width = cachedTextGeneratorForLayout.GetPreferredWidth(charStr, tempSettings) * ratio; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Debug.LogError($"GetPreferredWidth failed for '{charStr}' with fontSize {customFontSize}: {ex.Message}"); |
| | | width = 0; |
| | | } |
| | | |
| | | charWidthCache[cacheKey] = width; |
| | | return width; |
| | | } |
| | | |
| | | private void CacluHrefAndImgIndex(int index) |
| | | { |
| | | for (int i = 0; i < m_ImgList.Count; i++) |