/// <summary> /// Sets the text of a UI text, while ensuring this will not fire a text changed event. /// </summary> private void SetText(object ui, string text, bool isTranslated, TranslationInfo info) { if (!info?.IsCurrentlySettingText ?? true) { try { UGUIHooks.TextChanged -= UguiTextEvents_OnTextChanged; NGUIHooks.TextChanged -= NGUITextEvents_TextChanged; TextMeshProHooks.TextChanged -= TextMeshProHooks_OnTextChanged; if (info != null) { info.IsCurrentlySettingText = true; } if (ui is Text) { ((Text)ui).text = text; } else if (ui is GUIContent) { ((GUIContent)ui).text = text; } else { // fallback to reflective approach var type = ui.GetType(); type.GetProperty(TextPropertyName)?.GetSetMethod()?.Invoke(ui, new[] { text }); } if (isTranslated) { info?.ResizeUI(ui); } else { info?.UnresizeUI(ui); } } finally { UGUIHooks.TextChanged += UguiTextEvents_OnTextChanged; NGUIHooks.TextChanged += NGUITextEvents_TextChanged; TextMeshProHooks.TextChanged += TextMeshProHooks_OnTextChanged; if (info != null) { info.IsCurrentlySettingText = false; } } } }
/// <summary> /// Sets the text of a UI text, while ensuring this will not fire a text changed event. /// </summary> private void SetText(object ui, string text, bool isTranslated, TranslationInfo info) { if (!info?.IsCurrentlySettingText ?? true) { try { // TODO: Disable ANY Hook _hooksEnabled = false; if (info != null) { info.IsCurrentlySettingText = true; } ui.SetText(text); if (isTranslated) { info?.ResizeUI(ui); } else { info?.UnresizeUI(ui); } } catch (NullReferenceException) { // This is likely happened due to a scene change. } catch (Exception e) { Logger.Current.Error(e, "An error occurred while setting text on a component."); } finally { _hooksEnabled = true; if (info != null) { info.IsCurrentlySettingText = false; } } } }
/// <summary> /// Sets the text of a UI text, while ensuring this will not fire a text changed event. /// </summary> private void SetText(object ui, string text, bool isTranslated, TranslationInfo info) { if (!info?.IsCurrentlySettingText ?? true) { try { // TODO: Disable ANY Hook _hooksEnabled = false; if (info != null) { info.IsCurrentlySettingText = true; } ui.SetText(text); if (isTranslated) { info?.ResizeUI(ui); } else { info?.UnresizeUI(ui); } } catch (Exception e) { Console.WriteLine("[XUnity.AutoTranslator][ERROR]: An error occurred while setting text on a component." + Environment.NewLine + e); } finally { _hooksEnabled = true; if (info != null) { info.IsCurrentlySettingText = false; } } } }