gpt4 book ai didi

c# - 如何检测 Unity3d 面板中的向上或向下滚动?

转载 作者:太空宇宙 更新时间:2023-11-03 22:58:34 25 4
gpt4 key购买 nike

我想检测向上或向下滚动。应该像下面的 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
}
}

最佳答案

ScrollbaronValueChangedScrollRect .不知道您使用的是哪一个,但这是注册到 onValueChanged 事件的示例代码。您可以找到其他 UI 事件示例 here .将对其进行修改以包含此答案中的样本。

您可能需要 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);
}

滚动矩形:

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);
}

关于c# - 如何检测 Unity3d 面板中的向上或向下滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44187844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com