【问题标题】:Unity3D ScrollView scroll rangeunity3D ScrollView 滚动范围
【发布时间】:2012-07-18 16:58:12
【问题描述】:

我正在尝试创建一个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 在屏幕的右半边完成的。

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

提前致谢。

【问题讨论】:

  • 如果你有固定的项目,它可以被计算出来,例如绘制一个框内的标签列表,然后计算项目的数量 * 框的高度。我以前用过这个技巧。

标签: layout position scrollview unity3d


【解决方案1】:

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,即屏幕上框的大小加上滚动条的粗细。

【讨论】:

  • 我遇到的问题不是滚动工作。问题是我不知道我的文本占用了多少空间,因为它可以改变,所以我尝试使用 CalcHeight 函数来获取它。而且我没有直接将 Vector2 变量分配给 BeginScrollView,因为我想让整个文本可滚动......我的意思是,我不想让文本只能通过滚动条滚动。
  • 那么问题根本就不清楚,恐怕。您重复发布不完整代码(缺少变量)的事实使您更难以理解您想要的内容。我假设您想要一个 ScrollView 来扩展 ScrollView 的内部区域,因为添加了更多文本?
  • 如您所见,我没有重复发帖,帖子是由版主编辑的。变量文本是从数据库中检索的,并且在用户进入该场景后不会更改。问题是我不知道滚动位置可以取的最大值。所以正确知道用户可以滚动的方式比它应该滚动的多,或者换句话说,如果用户向下滚动很多并尝试再次向上滚动,则需要一些时间才能看到文本真正向上滚动。
【解决方案2】:

我知道这是 3 年后的事了,但也许它也会对其他人有所帮助...

我发现将scrollPosition 设置为一个非常高的数字会强制滚动条到底部:

scrollPosition.y = 999.9f;

然后你可以这样读取位置:

scrollPosition = GUILayout.BeginScrollView(scrollPosition, 
                                           GUILayout.Width(370),  
                                           GUILayout.Height(135)); 

它将被设置为最大值。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2011-09-21
    • 1970-01-01
    • 2023-01-10
    • 2014-02-13
    • 2018-05-11
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多