C# 垂直标准化位置不精确或计算错误

C# 垂直标准化位置不精确或计算错误,c#,unity3d,scroll,C#,Unity3d,Scroll,我希望用户能够在滚动视图中浏览选项,而中间的选项保持居中。当我返回verticalnormalizedposition时,它会更新,但不够精确。。。我的意思是,当我在选项中往下看时,“居中”位置会越来越低,最终会在滚动视图之外。。。 scrollview中允许的最大选项为5。所以我开始更新scrollview,当它在选项2下面时。试着把它放在中心。。。 选项的高度是30, scrollview的高度是150, 结果租金的高度为(25个选项*30)750。希望有人能帮我解决这个问题 if (Inp

我希望用户能够在滚动视图中浏览选项,而中间的选项保持居中。当我返回verticalnormalizedposition时,它会更新,但不够精确。。。我的意思是,当我在选项中往下看时,“居中”位置会越来越低,最终会在滚动视图之外。。。 scrollview中允许的最大选项为5。所以我开始更新scrollview,当它在选项2下面时。试着把它放在中心。。。 选项的高度是30, scrollview的高度是150, 结果租金的高度为(25个选项*30)750。希望有人能帮我解决这个问题

if (Input.GetKey(KeyCode.DownArrow))
        {
            GameObject currentSelected = EventSystem.current.currentSelectedGameObject;
            int i = OptionObjects.IndexOf(currentSelected);
            if (i > 1 && i < OptionObjects.Count - 2)
            {
                StartCoroutine(CalculateScrollPos(i));
            }
            else if (i < 3)
            {
                Mask.GetComponent<ScrollRect>().verticalNormalizedPosition = 1f;
            }
            else if (i > OptionObjects.Count - 3)
            {
                Mask.GetComponent<ScrollRect>().verticalNormalizedPosition = 0f;
            }
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            GameObject currentSelected = EventSystem.current.currentSelectedGameObject;
            int i = OptionObjects.IndexOf(currentSelected);
            if (i > 1 && i < OptionObjects.Count - 2)
            {
                StartCoroutine(CalculateScrollPos(i));
            }
            else if (i < 3)
            {
                Mask.GetComponent<ScrollRect>().verticalNormalizedPosition = 1f;
            }
            else if (i > OptionObjects.Count - 3)
            {
                Mask.GetComponent<ScrollRect>().verticalNormalizedPosition = 0f;
            }
        }
if(Input.GetKey(KeyCode.DownArrow))
{
GameObject currentSelected=EventSystem.current.currentSelectedGameObject;
int i=OptionObject.IndexOf(当前选定);
if(i>1&&iOptionObjects.Count-3)
{
Mask.GetComponent().verticalNormalizedPosition=0f;
}
}
if(Input.GetKey(KeyCode.UpArrow))
{
GameObject currentSelected=EventSystem.current.currentSelectedGameObject;
int i=OptionObject.IndexOf(当前选定);
if(i>1&&iOptionObjects.Count-3)
{
Mask.GetComponent().verticalNormalizedPosition=0f;
}
}
计算时:

IEnumerator CalculateScrollPos(int value)
{
    float r = ((resultParent.sizeDelta.y - 30f) - ((value-2) * (resultParent.sizeDelta.y / resultParent.childCount))) / (resultParent.sizeDelta.y - 30f);
    yield return new WaitForEndOfFrame();
    Mask.GetComponent<ScrollRect>().verticalScrollbar.value = r;
}
IEnumerator计算滚动位置(int值)
{
浮点r=((resultParent.sizeDelta.y-30f)-(value-2)*(resultParent.sizeDelta.y/resultParent.childCount))/(resultParent.sizeDelta.y-30f);
返回新的WaitForEndOfFrame();
Mask.GetComponent().verticalScrollbar.value=r;
}

[SOLVED]因此,在计算新的verticalnormalizedposition时,我必须从结果租金高度中减去scrollview的高度。代码看起来像:
IEnumerator CalculateScrollPos(int值){float r=((resultParent.sizeDelta.y-Mask.GetComponent().sizeDelta.y)-(value-2)*(resultParent.sizeDelta.y/resultParent.childCount))/(resultParent.sizeDelta.y-Mask.GetComponent().sizeDelta.y);返回新的WaitForEndOfFrame();Mask.GetComponent().verticalScrollbar.value=r;yield break;}
注释不用于回答。你可以在下面提交答案。有关更多信息,请参阅![已解决]因此,在计算新的verticalnormalizedposition时,我必须从结果租金高度中减去scrollview的高度。代码看起来像:
IEnumerator CalculateScrollPos(int值){float r=((resultParent.sizeDelta.y-Mask.GetComponent().sizeDelta.y)-(value-2)*(resultParent.sizeDelta.y/resultParent.childCount))/(resultParent.sizeDelta.y-Mask.GetComponent().sizeDelta.y);返回新的WaitForEndOfFrame();Mask.GetComponent().verticalScrollbar.value=r;yield break;}
注释不用于回答。你可以在下面提交答案。有关更多信息,请参阅!