[英]How to detect scrolling up or down in panel for Unity3d?
我想检测向上或向下滚动。 应该像下面的Windows窗体。
private void dgv_Scroll(object sender, ScrollEventArgs e)
{
if (e.OldValue > e.NewValue)
{
// here up
}
else
{
// here down
}
}
如何在Unity3d的面板中检测向上或向下滚动?
public void OnScrollValueChanged(float value)
{
if (?)
{
// here up
}
else
{
// here down
}
}
还有就是onValueChanged
用于Scrollbar
和ScrollRect
。 不知道你正在使用哪一个,但这里是一个注册到onValueChanged
事件的示例代码。 您可以在此处找到其他UI事件示例。 将修改它以包括来自此答案的样本。
您可能需要Scrollbar
。 获取原始值,在开始时将其与滚动时的当前值进行比较。 您可以使用它来确定向上和向下。 这假设direction
设置为TopToBottom
。
scrollBar.direction = Scrollbar.Direction.TopToBottom;
滚动条:
public Scrollbar scrollBar;
float lastValue = 0;
void OnEnable()
{
//Subscribe to the Scrollbar event
scrollBar.onValueChanged.AddListener(scrollbarCallBack);
lastValue = scrollBar.value;
}
//Will be called when Scrollbar changes
void scrollbarCallBack(float value)
{
if (lastValue > value)
{
UnityEngine.Debug.Log("Scrolling UP: " + value);
}
else
{
UnityEngine.Debug.Log("Scrolling DOWN: " + value);
}
lastValue = value;
}
void OnDisable()
{
//Un-Subscribe To Scrollbar Event
scrollBar.onValueChanged.RemoveListener(scrollbarCallBack);
}
scrollRect的:
public ScrollRect scrollRect;
void OnEnable()
{
//Subscribe to the ScrollRect event
scrollRect.onValueChanged.AddListener(scrollRectCallBack);
}
//Will be called when ScrollRect changes
void scrollRectCallBack(Vector2 value)
{
Debug.Log("ScrollRect Changed: " + value);
}
void OnDisable()
{
//Un-Subscribe To ScrollRect Event
scrollRect.onValueChanged.RemoveListener(scrollRectCallBack);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.