ali*_*vgi 2 c# user-interface scrollrect unity-game-engine
我想检测向上或向下滚动。应该像下面的 Windows 窗体一样。
private void dgv_Scroll(object sender, ScrollEventArgs e)
{
if (e.OldValue > e.NewValue)
{
// here up
}
else
{
// here down
}
}
Run Code Online (Sandbox Code Playgroud)
如何检测 Unity3d 面板中的向上或向下滚动?
public void OnScrollValueChanged(float value)
{
if (?)
{
// here up
}
else
{
// here down
}
}
Run Code Online (Sandbox Code Playgroud)
有onValueChanged和。Scrollbar ScrollRect不知道您使用的是哪一个,但这里有一个注册活动的示例代码onValueChanged 。您可以在此处找到其他 UI 事件示例。将修改它以包含此答案中的示例。
您可能需要Scrollbar. 获取原始值,在开始时然后将其与滚动时的当前值进行比较。您可以用它来确定向上和向下。这假设 被direction设置为TopToBottom。
scrollBar.direction = Scrollbar.Direction.TopToBottom;
Run Code Online (Sandbox Code Playgroud)
滚动条:
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);
}
Run Code Online (Sandbox Code Playgroud)
滚动矩形:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13460 次 |
| 最近记录: |