美文网首页unity3D技术分享
unityScrollRect滚动到指定索引位置处

unityScrollRect滚动到指定索引位置处

作者: 好怕怕 | 来源:发表于2024-11-08 11:13 被阅读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

相关文章

网友评论

    本文标题:unityScrollRect滚动到指定索引位置处

    本文链接:https://www.haomeiwen.com/subject/uoefjjtx.html