- Home /
How to reset the position of the caret in the inputfield after the panel has been dragged?
So I have an inputfield that is the child of a panel and the caret inside the inputfield displays properly when the panel is not dragged. After I drag the panel, the caret disappears and never comes back even when I right click on the inputfield again. I can enter text on the inputfield just fine but the only thing missing is the caret. How can I fix this and why does this happen? If it helps, here is the code for dragging a panel.
using UnityEngine;
using UnityEngine.EventSystems;
public class DragPanel : MonoBehaviour, IDragHandler
{
private RectTransform dragObjectInternal;
[SerializeField]
private RectTransform dragAreaInternal;
private Vector2 originalLocalPointerPosition;
private Vector3 originalPanelLocalPosition;
private void Start()
{
dragObjectInternal = GetComponent<RectTransform>();
}
public void OnBeginDrag(PointerEventData data)
{
originalPanelLocalPosition = dragObjectInternal.localPosition;
RectTransformUtility.ScreenPointToLocalPointInRectangle(dragAreaInternal, data.position, data.pressEventCamera, out originalLocalPointerPosition);
}
public void OnDrag(PointerEventData data)
{
Vector2 localPointerPosition;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(dragAreaInternal, data.position, data.pressEventCamera, out localPointerPosition))
{
Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
dragObjectInternal.localPosition = originalPanelLocalPosition + offsetToOriginal;
}
ClampToArea();
}
// Clamp panel to dragArea
private void ClampToArea()
{
Vector3 pos = dragObjectInternal.localPosition;
Vector3 minPosition = dragAreaInternal.rect.min - dragObjectInternal.rect.min;
Vector3 maxPosition = dragAreaInternal.rect.max - dragObjectInternal.rect.max;
pos.x = Mathf.Clamp(dragObjectInternal.localPosition.x, minPosition.x, maxPosition.x);
pos.y = Mathf.Clamp(dragObjectInternal.localPosition.y, minPosition.y, maxPosition.y);
dragObjectInternal.localPosition = pos;
}
}
Well, I'm not sure what might be your problem. However most parts of the new UI system is open source. Here's where the cursor is actually generated. This is actually called from OnFillVBO which is called from UpdateGeometry which is called from Rebuild which is actually called from the Canvas updater when the input field is "dirty". I can't really see where it might go wrong.
$$anonymous$$aybe it's related to this for loop which effectively does nothing since UIVertex is a struct so the addition has no effect on the information stored in the array. Though if the text renders properly, the cursor should also. It's possible, if you have a cursor blink rate set that the blinking coroutine might be ter$$anonymous$$ated maybe because "isFocused" turned false for a moment? It's hard to tell from just looking at the code ^^.
Here is what I mean.
In this image, the panel is in its original panel with the caret in the inputfield clearly visible.
In this image, I drag the panel to some arbitrary location, right click on the inputfield and the caret doesn't appear.
That is the problem I'm having.
Though interestingly enough if I place the panel on the top or bottom left side of the screen and I right click, the caret appears. Anywhere else it doesn't for some reason.
Edit: If I set the right value in the caret transform to 5 ins$$anonymous$$d of 10, I can move the panel anywhere, right click on the inputfield and the caret appears. However if I set it 5 before moving the panel and then right click on the inputfield, the caret doesn't appear. But if I move it afterwards it works just fine.