- Home /
How do I effect click-and-hold to a child Button of list-item in a ListView?
I'm relatively new to Unity and I'm trying to build an in-game reorderable list using the UI Toolkit features.
I'm populating a ListView element with a Visual Tree Asset that is a composite of several Label and Button elements.
I would like to be able to click-and-hold a button (perhaps on the left-side) in the list-item and then drag the list-item, up or down, to the position I want in the list.
I was able to successfully bind the clickable.clicked function to a list-item child Button using the following code:
Action<VisualElement, int> bindItem = (e, i) => {
var reorder_button = e.Q<Button>("reorder_button");
if (reorder_button != null) reorder_button.clickable.clicked += () => ReorderClicked(e);
};
where the function called is:
void ReorderClicked(VisualElement ve)
{
var title = ve.Q<Label>("title");
Debug.Log("Clicked " + title.text);
};
This works as expected but how would I expand on this to accommodate click-and-hold or a mouse-down / mouse-up event?
Unity has a way to handle dragging events: https://docs.unity3d.com/2018.1/Documentation/ScriptReference/EventSystems.IDragHandler.html check that page. You can declare events to handle when you beggin the drag, handle the frames while you're dragging, and also once you finish dragging. I would grab the positions of each buttons plus the offset of the sizes, height and width. You can get the position of the mouse pointer once you release the mouse on the OnEndDrag() function from the Drag Handler. Then you check the position, if it's between two other buttons, at the beggining or at the end of the list. If you need any more clarification, just ask. I'm kinda new to unity as well, and I know it's tough sometimes to find info, but I will share anything I know so far.
Your answer
Follow this Question
Related Questions
images assigned to gameobjects by tag 2 Answers
Make image follow your cursor 1 Answer
Multiple Cars not working 1 Answer
[4.6] Dynamic Images in UI 1 Answer
Make GUILayout horizontal first, then vertical? (C#) 3 Answers