Hello,
I have a problem with a custom class I found here: Changing caret style (inside a TextField)?
Here it is :
using System;
namespace UnityEngine.UIElements
{
public class BlinkingTextField : TextField
{
private readonly IVisualElementScheduledItem blink;
private long blinkInterval = 500;
private bool isBlinkEnabled = true;
private string blinkStyle = "cursor-transparent";
/// <summary>
/// Caret blink interval in ms.
/// </summary>
public long BlinkInterval
{
get => blinkInterval;
set
{
blinkInterval = value;
blink?.Every(blinkInterval);
}
}
/// <summary>
/// Caret uss style applied on blink.
/// </summary>
public string BlinkStyle
{
get => blinkStyle;
set => blinkStyle = value;
}
/// <summary>
/// If true, caret blinks.
/// </summary>
public bool BlinkEnable
{
get => isBlinkEnabled;
set
{
if (isBlinkEnabled == value)
return;
isBlinkEnabled = value;
if (!isBlinkEnabled)
{
if (IsFocused)
blink?.Pause();
if (ClassListContains(blinkStyle))
RemoveFromClassList(blinkStyle);
}
else if (IsFocused)
{
blink?.Resume();
}
}
}
/// <summary>
/// Returns true if active input.
/// </summary>
bool IsFocused => focusController?.focusedElement == this;
public BlinkingTextField()
{
RegisterCallback<FocusEvent>(OnFocus);
RegisterCallback<BlurEvent>(OnInputEnded);
blink = schedule.Execute(() =>
{
if (ClassListContains(blinkStyle))
RemoveFromClassList(blinkStyle);
else
AddToClassList(blinkStyle);
}).Every(blinkInterval);
blink.Pause();
}
private void OnFocus(FocusEvent evt)
{
if (!isBlinkEnabled)
return;
blink.Resume();
}
private void OnInputEnded(BlurEvent evt)
{
blink.Pause();
}
[Obsolete("Obsolete")]
public new class UxmlFactory : UxmlFactory<BlinkingTextField, BlinkingUxmlTraits> { }
[Scripting.Preserve]
[Obsolete("Obsolete")]
public class BlinkingUxmlTraits : UxmlTraits
{
private readonly UxmlLongAttributeDescription blinkInterval = new()
{ name = "blink-interval", use = UxmlAttributeDescription.Use.Optional, defaultValue = 500 };
private readonly UxmlBoolAttributeDescription blinkEnable = new()
{ name = "blink-enable", use = UxmlAttributeDescription.Use.Optional, defaultValue = true };
private readonly UxmlStringAttributeDescription blinkStyle = new()
{
name = "blink-style", use = UxmlAttributeDescription.Use.Optional, defaultValue = "cursor-transparent"
};
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((BlinkingTextField)ve).BlinkInterval = blinkInterval.GetValueFromBag(bag, cc);
((BlinkingTextField)ve).BlinkEnable = blinkEnable.GetValueFromBag(bag, cc);
((BlinkingTextField)ve).BlinkStyle = blinkStyle.GetValueFromBag(bag, cc);
}
}
}
}
I replaced this code in my .uxml which worked fine:
<engine:TextField label=βMail addressβ placeholder-text=β john@mail.com β keyboard-type=β EmailAddress β hide-placeholder-on-focus=β true β name=β MailAddressInput β class=β frama-text frama-input β style=β flex-wrap: nowrap; flex-direction: column; β>
<Bindings>
<engine:DataBinding property=β value β binding-mode=β TwoWay β data-source-path=β mailAddress β data-source-type=β LoginPanelManager, Assembly-CSharp β />
</Bindings>
</engine:TextField>
By:
<engine:BlinkingTextField label=βMail addressβ placeholder-text=β john@mail.com β keyboard-type=β EmailAddress β hide-placeholder-on-focus=β true β name=β MailAddressInput β class=β frama-text frama-input β style=β flex-wrap: nowrap; flex-direction: column; β>
<Bindings>
<engine:DataBinding property=β value β binding-mode=β TwoWay β data-source-path=β mailAddress β data-source-type=β LoginPanelManager, Assembly-CSharp β />
</Bindings>
</engine:BlinkingTextField>
The caret blinks well but I have this error popping up:
Element 'Bindings' is missing a UxmlElementAttribute and has no registered factory method. Please ensure that you have the correct namespace imported.
Does anyone know how to solve this?
Thanks!