gpt4 book ai didi

layout - Unity3D ScrollView滚动范围

转载 作者:行者123 更新时间:2023-12-02 22:35:22 25 4
gpt4 key购买 nike

我正在尝试创建一个 ScrollView ,但我不知道如何确定滚动的最大范围(或者更好的是,最大滚动位置)。

这是我尝试过但没有任何积极结果的做法:

void Update()
{
//get the amount of space that my text is taking
textSize = gs.CalcHeight(new GUIContent(text), (screenWidth/2));

if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(0).tapCount==1)
{
var touch = Input.touches[0];

//check if touch is within the scrollView area
if(touch.position.x >= (screenWidth/2) && touch.position.x <= (screenWidth) && touch.position.y >= 0 && touch.position.y <= (screenHeight))
{
if(touch.phase == TouchPhase.Moved)
{
scrollPosition[1] += touch.deltaPosition.y;
if(scrollPosition[1] < 0)
{
scrollPosition[1] = 0;
}

//I subtracted this cause i read that the scrollbars take 16px
if(scrollPosition[1] > (textSize-scrollViewRect.yMax-16)) //scrollViewRect is a Rect with the size of my scrollView
{
scrollPosition[1] = textSize-scrollViewRect.yMax-16;
}
}
}
}
}

void OnGUI()
{
screenWidth = camera.pixelWidth;
screenHeight = camera.pixelHeight;
GUILayout.BeginArea(new Rect(screenWidth/2, 0, screenWidth/2, screenHeight));

GUILayout.BeginScrollView(scrollPosition/*, GUILayout.Width (screenWidth/2), GUILayout.Height (screenHeight/2)*/, "box");

GUILayout.Label (new GUIContent(text), gs);

// End the scrollview
GUILayout.EndScrollView ();
scrollViewRect = GUILayoutUtility.GetLastRect();
GUILayout.EndArea();
}

这是假设 scrollView 位于屏幕的右半边。

你们能帮我解决这个问题吗?

提前致谢。

最佳答案

GUI.BeginScrollView 的 API 文档所示,您可以将 Vector2 分配给 BeginScrollView 的声明,以跟踪和更新框已滚动到的位置。

我举个例子:

using UnityEngine;
using System.Collections;

public class ScrollSize : MonoBehaviour {

private int screenBoxX = Screen.width;
private int screenBoxY = Screen.height;

private int scrollBoxX = Screen.width * 2;
private int scrollBoxY = Screen.height * 3;

public Vector2 scrollPosition = Vector2.zero;

void Awake()
{
Debug.Log ("box size is " + scrollBoxX + " " + scrollBoxY);
}

void OnGUI()
{
scrollPosition = GUI.BeginScrollView(new Rect(0, 0, screenBoxX, screenBoxY),
scrollPosition, new Rect(0, 0, scrollBoxX, scrollBoxY));

GUI.EndScrollView();

// showing 4 decimal places
Debug.Log ("Box scrolled @ " + scrollPosition.ToString("F4"));
}
}

为了解释这个例子,我们假设屏幕分辨率是 800 x 600。因此,屏幕上的可视滚动框宽度为 800,高度为 600,滚动框的内部区域为 1600 x 1800。

我们通过将 Gui.BeginScrollView() 指定为 Vector2 的值 scrollPosition 来创建滚动框。随着滚动框水平和垂直滚动,Vector2 将更新为滚动值。

如果滚动框滚动到最左上角的位置,则 scrollPosition 的值将为 0,0。

如果滚动框滚动到最右下角的位置,则 scrollPosition 的值将为 816、616,即屏幕上框的大小加上滚动条的粗细。

关于layout - Unity3D ScrollView滚动范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11546567/

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