unity3D技术分享

unityScrollRect滚动到指定索引位置处

2024-11-08  本文已影响0人  好怕怕
GIF.gif
public class Test : MonoBehaviour
{

    public int SelectIndex = 0;
    public ScrollRect Scroll;
    public ToggleGroup Group;


    [ContextMenu("跳转")]
    public void Test1()
    {
        Group.SetIndex(SelectIndex);
        Scroll.MoveToIndex(Group.Count,SelectIndex);
    }
}

扩展函数

    public static void MoveToIndex(this ScrollRect scrollRect, int count, int index)
    {
        float normalizedPosition = (float)(index) / (count-1);
        if (scrollRect.horizontal)
        {
            scrollRect.horizontalNormalizedPosition = normalizedPosition;
        }
        else
        {
            scrollRect.verticalNormalizedPosition = 1 - normalizedPosition;
        }
    }
image.png
上一篇 下一篇

猜你喜欢

热点阅读