- Home /
TreeView and Touch Controls
Click here for a screencast that demonstrates the specific issue.
I'm a seasoned developer in other environments, but new to Unity. I've been handed an existing Unity project that was originally developed for desktop and asked to update it for mobile (ipad). I've been able to figure out most of what I need to do, but I'm stuck with one issue:
There is a TreeView listbox that I cannot get to respond correctly to dragging a touch. It always just selects the row that I've started the drag on, and does not scroll the list up and down.
I'm including a short screencast that demonstrates the issue.
I've already tried detecting touches and drags in Update() in TreeView, but I cannot figure out the right way to get the scrolling to match the movement of my finger on the screen; it scrolls either too fast or too slow.
I've already consulted the documentation for Touch Controls, and have successfully added it to the main part of the window (the 3D ear portion in the included screencast). I'm just stuck with getting it working on the TreeView.
And here is my TreeView.Update():
void Update()
{
//detect if we are dragging with a touch, and if so, scroll the listbox to match the drag.
if (Input.touchCount > 0) //we have at least 1 touch active.
{
Touch myTouch = Input.touches[0]; //grab the first finger.
float verticalChange = 0.0f;
if (myTouch.phase == TouchPhase.Moved) //we are dragging a finger across the device.
{
verticalChange = myTouch.deltaPosition.y;
Debug.Log("Touch MouseChange: " + verticalChange);
}
else if (myTouch.phase == TouchPhase.Stationary)
{
//we are still touching the interface, but are not moving our finger.
verticalChange = 0.0f;
}
//vertical change is the delta of how far the finger was dragged.
//we need to convert that to a value between 0 and 1 for the vertical scrollbar's value.
//How about we try something like converting the verticalChange of the drag to a percentage
//of the height of the scrollbar, then adding that to verticalScroll's position?
RectTransform rt = Panel.GetComponent<RectTransform>();
float percentChange = verticalChange/ rt.rect.height;
//Debug.Log(percentChange);
verticalScroll.value -= percentChange;
}
}
Thanks in advance for any help you can offer!
Bookmarking for later, screencast explaining the problem was great btw.